vendor/pimcore/pimcore/lib/Targeting/ActionHandler/DelegatingActionHandler.php line 65

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\ActionHandler;
  16. use Pimcore\Model\Tool\Targeting\Rule;
  17. use Pimcore\Targeting\DataLoaderInterface;
  18. use Pimcore\Targeting\DataProviderDependentInterface;
  19. use Pimcore\Targeting\Model\VisitorInfo;
  20. use Psr\Container\ContainerInterface;
  21. class DelegatingActionHandler implements ActionHandlerInterface
  22. {
  23.     /**
  24.      * @var ContainerInterface
  25.      */
  26.     private $actionHandlers;
  27.     /**
  28.      * @var DataLoaderInterface
  29.      */
  30.     private $dataLoader;
  31.     public function __construct(
  32.         ContainerInterface $actionHandlers,
  33.         DataLoaderInterface $dataLoader
  34.     ) {
  35.         $this->actionHandlers $actionHandlers;
  36.         $this->dataLoader $dataLoader;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function apply(VisitorInfo $visitorInfo, array $actionRule $rule null)
  42.     {
  43.         /** @var string $type */
  44.         $type $action['type'] ?? null;
  45.         if (empty($type)) {
  46.             throw new \InvalidArgumentException('Invalid action: type is not set');
  47.         }
  48.         $actionHandler $this->getActionHandler($type);
  49.         // load data providers if necessary
  50.         if ($actionHandler instanceof DataProviderDependentInterface) {
  51.             $this->dataLoader->loadDataFromProviders($visitorInfo$actionHandler->getDataProviderKeys());
  52.         }
  53.         $actionHandler->apply($visitorInfo$action$rule);
  54.     }
  55.     public function hasActionHandler(string $type): bool
  56.     {
  57.         return $this->actionHandlers->has($type);
  58.     }
  59.     public function getActionHandler(string $type): ActionHandlerInterface
  60.     {
  61.         if (!$this->actionHandlers->has($type)) {
  62.             throw new \InvalidArgumentException(sprintf(
  63.                 'Invalid condition: there is no action handler registered for type "%s"',
  64.                 $type
  65.             ));
  66.         }
  67.         return $this->actionHandlers->get($type);
  68.     }
  69. }