vendor/pimcore/pimcore/lib/Targeting/ConditionFactory.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Targeting;
  16. use Pimcore\Event\Targeting\BuildConditionEvent;
  17. use Pimcore\Event\TargetingEvents;
  18. use Pimcore\Targeting\Condition\ConditionInterface;
  19. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  20. class ConditionFactory implements ConditionFactoryInterface
  21. {
  22.     /**
  23.      * @var EventDispatcherInterface
  24.      */
  25.     private $eventDispatcher;
  26.     /**
  27.      * @var string[]
  28.      */
  29.     private $conditions = [];
  30.     /**
  31.      * @var string[]
  32.      */
  33.     private $blacklistedKeys = ['type''operator''bracketLeft''bracketRight'];
  34.     public function __construct(
  35.         EventDispatcherInterface $eventDispatcher,
  36.         array $conditions
  37.     ) {
  38.         $this->eventDispatcher $eventDispatcher;
  39.         $this->conditions $conditions;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function build(array $config): ConditionInterface
  45.     {
  46.         /** @var string|null $type */
  47.         $type $config['type'] ?? null;
  48.         if (empty($type)) {
  49.             throw new \InvalidArgumentException('Invalid condition: Type is not set');
  50.         }
  51.         if (!isset($this->conditions[$type])) {
  52.             throw new \InvalidArgumentException(sprintf(
  53.                 'Invalid condition: Condition with type "%s" is not registered',
  54.                 $type
  55.             ));
  56.         }
  57.         $typeConfig array_filter($config, function ($v$k) {
  58.             return !in_array($k$this->blacklistedKeys);
  59.         }, ARRAY_FILTER_USE_BOTH);
  60.         $event = new BuildConditionEvent($type$this->conditions[$type], $typeConfig);
  61.         $this->eventDispatcher->dispatch($eventTargetingEvents::BUILD_CONDITION);
  62.         if ($event->hasCondition()) {
  63.             return $event->getCondition();
  64.         }
  65.         return $this->buildInstance($type$typeConfig);
  66.     }
  67.     protected function buildInstance(string $type, array $config): ConditionInterface
  68.     {
  69.         $class $this->conditions[$type];
  70.         if (!class_exists($class)) {
  71.             throw new \RuntimeException(sprintf(
  72.                 'Configured condition class "%s" for type "%s" does not exist',
  73.                 $class,
  74.                 $type
  75.             ));
  76.         }
  77.         if (!is_subclass_of($classConditionInterface::class)) {
  78.             throw new \RuntimeException(sprintf(
  79.                 'Configured condition class "%s" for type "%s" has not the ConditionInterface',
  80.                 $class,
  81.                 $type
  82.             ));
  83.         }
  84.         return $class::fromConfig($config);
  85.     }
  86. }