src/Type/TypeCollection.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Type;
  3. use App\Form\Setting\AbstractSettingType;
  4. use Flagception\Manager\FeatureManagerInterface;
  5. class TypeCollection
  6. {
  7.     public const GROUP_GENERAL 'General';
  8.     public const GROUP_VACANCY 'Vacancy';
  9.     public const GROUP_ATS 'ATS';
  10.     public const GROUP_MODULES 'Modules';
  11.     public const GROUP_EXTERNAL_PLUGINGS 'External plugins';
  12.     /**
  13.      * @var AbstractSettingType[]
  14.      */
  15.     private $types;
  16.     private array $groupedTypes = [];
  17.     /**
  18.      * @var FeatureManagerInterface
  19.      */
  20.     private $manager;
  21.     public function __construct(FeatureManagerInterface $manager)
  22.     {
  23.         $this->types = [];
  24.         $this->manager $manager;
  25.     }
  26.     public function addType(
  27.         AbstractSettingType $type,
  28.         string $name,
  29.         ?string $feature null,
  30.         ?string $template null,
  31.         string $group self::GROUP_GENERAL
  32.     ) {
  33.         if ($feature && !$this->manager->isActive($feature)) {
  34.             return;
  35.         }
  36.         $this->validateGroupName($group);
  37.         $this->types[$name] = [
  38.             'type' => $type,
  39.             'template' => $template,
  40.         ];
  41.         if (!\array_key_exists($group$this->groupedTypes)) {
  42.             $this->groupedTypes[$group] = [];
  43.         }
  44.         $this->groupedTypes[$group][$name] = $type;
  45.     }
  46.     /**
  47.      * @param $name
  48.      *
  49.      * @return AbstractSettingType
  50.      */
  51.     public function getTypeByName($name): ?AbstractSettingType
  52.     {
  53.         if (\array_key_exists($name$this->types)) {
  54.             return $this->types[$name]['type'];
  55.         }
  56.         return null;
  57.     }
  58.     public function getTemplateByName(string $name): ?string
  59.     {
  60.         if (\array_key_exists($name$this->types)) {
  61.             return $this->types[$name]['template'];
  62.         }
  63.         return null;
  64.     }
  65.     /**
  66.      * @return AbstractSettingType[]|array
  67.      */
  68.     public function getTypes(): array
  69.     {
  70.         return $this->types;
  71.     }
  72.     public function getTypesByGroup(string $group): array
  73.     {
  74.         $this->validateGroupName($group);
  75.         return $this->groupedTypes[$group] ?? [];
  76.     }
  77.     private function validateGroupName(string $group)
  78.     {
  79.         if (!\in_array($group, [
  80.             self::GROUP_GENERAL,
  81.             self::GROUP_ATS,
  82.             self::GROUP_VACANCY,
  83.             self::GROUP_MODULES,
  84.             self::GROUP_EXTERNAL_PLUGINGS,
  85.         ], true)) {
  86.             throw new \InvalidArgumentException('Setting group does not exist');
  87.         }
  88.     }
  89. }