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

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.     public static function getSubscribedEvents()
  56.     {
  57.         return [
  58.             TargetingEvents::PRE_RESOLVE => 'onVisitorInfoResolve',
  59.         ];
  60.     }
  61.     public function onVisitorInfoResolve(TargetingEvent $event)
  62.     {
  63.         $request $event->getRequest();
  64.         $document $this->documentResolver->getDocument($request);
  65.         if ($document) {
  66.             $this->assignDocumentTargetGroups($document$event->getVisitorInfo());
  67.         }
  68.     }
  69.     private function assignDocumentTargetGroups(Document $documentVisitorInfo $visitorInfo)
  70.     {
  71.         if (!$document instanceof Document\Page || null !== Staticroute::getCurrentRoute()) {
  72.             return;
  73.         }
  74.         // get target groups from document
  75.         $targetGroups $document->getTargetGroups();
  76.         if (empty($targetGroups)) {
  77.             return;
  78.         }
  79.         foreach ($targetGroups as $targetGroup) {
  80.             $this->actionHandler->apply($visitorInfo, [
  81.                 'type' => 'assign_target_group',
  82.                 'targetGroup' => $targetGroup,
  83.             ]);
  84.             $this->eventDispatcher->dispatch(
  85.                 new AssignDocumentTargetGroupEvent($visitorInfo$document$targetGroup),
  86.                 TargetingEvents::ASSIGN_DOCUMENT_TARGET_GROUP
  87.             );
  88.         }
  89.     }
  90. }