vendor/pimcore/pimcore/lib/Targeting/EventListener/DocumentTargetGroupListener.php line 74

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\EventListener;
  16. use Pimcore\Event\Targeting\AssignDocumentTargetGroupEvent;
  17. use Pimcore\Event\Targeting\TargetingEvent;
  18. use Pimcore\Event\TargetingEvents;
  19. use Pimcore\Http\Request\Resolver\DocumentResolver;
  20. use Pimcore\Model\Document;
  21. use Pimcore\Model\Staticroute;
  22. use Pimcore\Targeting\ActionHandler\ActionHandlerInterface;
  23. use Pimcore\Targeting\ActionHandler\DelegatingActionHandler;
  24. use Pimcore\Targeting\Model\VisitorInfo;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  27. /**
  28.  * Handles target groups configured on the document settings panel. If a document
  29.  * has configured target groups, the assign_target_group will be manually called
  30.  * for that target group before starting to match other conditions.
  31.  */
  32. class DocumentTargetGroupListener implements EventSubscriberInterface
  33. {
  34.     /**
  35.      * @var DocumentResolver
  36.      */
  37.     private $documentResolver;
  38.     /**
  39.      * @var ActionHandlerInterface|DelegatingActionHandler
  40.      */
  41.     private $actionHandler;
  42.     /**
  43.      * @var EventDispatcherInterface
  44.      */
  45.     private $eventDispatcher;
  46.     public function __construct(
  47.         DocumentResolver $documentResolver,
  48.         ActionHandlerInterface $actionHandler,
  49.         EventDispatcherInterface $eventDispatcher
  50.     ) {
  51.         $this->documentResolver $documentResolver;
  52.         $this->actionHandler $actionHandler;
  53.         $this->eventDispatcher $eventDispatcher;
  54.     }
  55.     /**
  56.      * @return string[]
  57.      */
  58.     public static function getSubscribedEvents()// : array
  59.     {
  60.         return [
  61.             TargetingEvents::PRE_RESOLVE => 'onVisitorInfoResolve',
  62.         ];
  63.     }
  64.     public function onVisitorInfoResolve(TargetingEvent $event)
  65.     {
  66.         $request $event->getRequest();
  67.         $document $this->documentResolver->getDocument($request);
  68.         if ($document) {
  69.             $this->assignDocumentTargetGroups($document$event->getVisitorInfo());
  70.         }
  71.     }
  72.     private function assignDocumentTargetGroups(Document $documentVisitorInfo $visitorInfo)
  73.     {
  74.         if (!$document instanceof Document\Page || null !== Staticroute::getCurrentRoute()) {
  75.             return;
  76.         }
  77.         // get target groups from document
  78.         $targetGroups $document->getTargetGroups();
  79.         if (empty($targetGroups)) {
  80.             return;
  81.         }
  82.         foreach ($targetGroups as $targetGroup) {
  83.             $this->actionHandler->apply($visitorInfo, [
  84.                 'type' => 'assign_target_group',
  85.                 'targetGroup' => $targetGroup,
  86.             ]);
  87.             $this->eventDispatcher->dispatch(
  88.                 new AssignDocumentTargetGroupEvent($visitorInfo$document$targetGroup),
  89.                 TargetingEvents::ASSIGN_DOCUMENT_TARGET_GROUP
  90.             );
  91.         }
  92.     }
  93. }