vendor/pimcore/customer-management-framework-bundle/src/Event/TargetingEventListener.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace CustomerManagementFrameworkBundle\Event;
  15. use CustomerManagementFrameworkBundle\ActionTrigger\Event\TargetGroupAssigned;
  16. use CustomerManagementFrameworkBundle\Targeting\DataProvider\Customer;
  17. use Pimcore\Event\Targeting\AssignDocumentTargetGroupEvent;
  18. use Pimcore\Event\Targeting\TargetingRuleEvent;
  19. use Pimcore\Model\Tool\Targeting\TargetGroup;
  20. use Pimcore\Targeting\DataLoaderInterface;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. class TargetingEventListener
  24. {
  25.     /**
  26.      * @var EventDispatcherInterface
  27.      */
  28.     protected $eventDispatcher;
  29.     protected $dataLoader;
  30.     protected $securityToken;
  31.     public function __construct(EventDispatcherInterface $eventDispatcherTokenStorageInterface $tokenDataLoaderInterface $dataLoader)
  32.     {
  33.         $this->eventDispatcher $eventDispatcher;
  34.         $this->securityToken $token;
  35.         $this->dataLoader $dataLoader;
  36.     }
  37.     public function onAssignDocumentTargetGroup(AssignDocumentTargetGroupEvent $event) {
  38.         $visitorInfo $event->getVisitorInfo();
  39.         //get customer
  40.         $this->dataLoader->loadDataFromProviders($visitorInfo, [Customer::PROVIDER_KEY]);
  41.         $customer $visitorInfo->get(Customer::PROVIDER_KEY);
  42.         if(!$customer) {
  43.             return;
  44.         }
  45.         $event TargetGroupAssigned::create(
  46.             $customer,
  47.             TargetGroupAssigned::ASSIGNMENT_TYPE_DOCUMENT,
  48.             $event->getTargetGroup(),
  49.             $visitorInfo
  50.         );
  51.         $this->eventDispatcher->dispatch($event->getName(), $event);
  52.     }
  53.     public function onPostRuleActions(TargetingRuleEvent $event) {
  54.         $visitorInfo $event->getVisitorInfo();
  55.         //get customer
  56.         $this->dataLoader->loadDataFromProviders($visitorInfo, [Customer::PROVIDER_KEY]);
  57.         $customer $visitorInfo->get(Customer::PROVIDER_KEY);
  58.         if(!$customer) {
  59.             return;
  60.         }
  61.         $rule $event->getRule();
  62.         foreach($rule->getActions() as $action) {
  63.             if($action['type'] == "assign_target_group") {
  64.                 $targetGroup TargetGroup::getById($action['targetGroup']);
  65.                 if(null !== $targetGroup) {
  66.                     $event TargetGroupAssigned::create(
  67.                         $customer,
  68.                         TargetGroupAssigned::ASSIGNMENT_TYPE_TARGETING_RULE,
  69.                         $targetGroup,
  70.                         $visitorInfo
  71.                     );
  72.                     $this->eventDispatcher->dispatch($event->getName(), $event);
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }