vendor/pimcore/customer-management-framework-bundle/src/ActionTrigger/EventHandler/DefaultEventHandler.php line 76

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 Commercial License (PCL)
  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 PCL
  13.  */
  14. namespace CustomerManagementFrameworkBundle\ActionTrigger\EventHandler;
  15. use CustomerManagementFrameworkBundle\ActionTrigger\Condition\Checker;
  16. use CustomerManagementFrameworkBundle\ActionTrigger\Event\CustomerListEventInterface;
  17. use CustomerManagementFrameworkBundle\ActionTrigger\Event\EventInterface;
  18. use CustomerManagementFrameworkBundle\ActionTrigger\Event\RuleEnvironmentAwareEventInterface;
  19. use CustomerManagementFrameworkBundle\ActionTrigger\Event\SingleCustomerEventInterface;
  20. use CustomerManagementFrameworkBundle\ActionTrigger\Queue\QueueInterface;
  21. use CustomerManagementFrameworkBundle\ActionTrigger\RuleEnvironment;
  22. use CustomerManagementFrameworkBundle\ActionTrigger\RuleEnvironmentInterface;
  23. use CustomerManagementFrameworkBundle\Model\ActionTrigger\Rule;
  24. use CustomerManagementFrameworkBundle\Model\CustomerInterface;
  25. use CustomerManagementFrameworkBundle\Traits\LoggerAware;
  26. use Knp\Component\Pager\PaginatorInterface;
  27. class DefaultEventHandler implements EventHandlerInterface
  28. {
  29.     use LoggerAware;
  30.     /** @var Rule[][]|null */
  31.     private $rulesGroupedByEvents null;
  32.     /**
  33.      * @var QueueInterface
  34.      */
  35.     protected $actionTriggerQueue;
  36.     /**
  37.      * @var PaginatorInterface
  38.      */
  39.     protected $paginator;
  40.     public function __construct(QueueInterface $actionTriggerQueuePaginatorInterface $paginator)
  41.     {
  42.         $this->actionTriggerQueue $actionTriggerQueue;
  43.         $this->paginator $paginator;
  44.     }
  45.     protected function getRulesGroupedByEvents()
  46.     {
  47.         if ($this->rulesGroupedByEvents === null) {
  48.             $rules = new Rule\Listing();
  49.             $rules->setCondition('active = 1');
  50.             $rules $rules->load();
  51.             $rulesGroupedByEvents = [];
  52.             foreach ($rules as $rule) {
  53.                 if ($triggers $rule->getTrigger()) {
  54.                     foreach ($triggers as $trigger) {
  55.                         $rulesGroupedByEvents[$trigger->getEventName()][] = $rule;
  56.                     }
  57.                 }
  58.             }
  59.             $this->rulesGroupedByEvents $rulesGroupedByEvents;
  60.         }
  61.         return $this->rulesGroupedByEvents;
  62.     }
  63.     public function handleEvent($event)
  64.     {
  65.         $environment = new RuleEnvironment();
  66.         if ($event instanceof SingleCustomerEventInterface) {
  67.             $this->handleSingleCustomerEvent($event$environment);
  68.         } elseif ($event instanceof CustomerListEventInterface) {
  69.             $this->handleCustomerListEvent($event$environment);
  70.         }
  71.     }
  72.     public function handleSingleCustomerEvent(SingleCustomerEventInterface $eventRuleEnvironmentInterface $environment)
  73.     {
  74.         $this->getLogger()->debug(sprintf('handle single customer event: %s'$event->getName()));
  75.         $appliedRules $this->getAppliedRules($event$environmenttrue);
  76.         foreach ($appliedRules as $rule) {
  77.             $this->handleActionsForCustomer($rule$event->getCustomer(), $environment);
  78.         }
  79.     }
  80.     public function handleCustomerListEvent(CustomerListEventInterface $eventRuleEnvironmentInterface $environment)
  81.     {
  82.         foreach ($this->getAppliedRules($event$environmentfalse) as $rule) {
  83.             if ($conditions $rule->getCondition()) {
  84.                 $where Checker::getDbConditionForRule($rule);
  85.                 $listing \Pimcore::getContainer()->get('cmf.customer_provider')->getList();
  86.                 $listing->setCondition($where);
  87.                 $listing->setOrderKey('o_id');
  88.                 $listing->setOrder('asc');
  89.                 $paginator $this->paginator->paginate($listing1100);
  90.                 $this->getLogger()->info(
  91.                     sprintf('handleCustomerListEvent: found %s matching customers'$paginator->getTotalItemCount())
  92.                 );
  93.                 $totalPages $paginator->getPaginationData()['totalCount'];
  94.                 for ($i 1$i <= $totalPages$i++) {
  95.                     $paginator $this->paginator->paginate($listing$i100);
  96.                     foreach ($paginator as $customer) {
  97.                         $this->handleActionsForCustomer($rule$customer$environment);
  98.                     }
  99.                     \Pimcore::collectGarbage();
  100.                 }
  101.             }
  102.         }
  103.     }
  104.     private function handleActionsForCustomer(Rule $ruleCustomerInterface $customerRuleEnvironmentInterface $environment)
  105.     {
  106.         if ($actions $rule->getAction()) {
  107.             foreach ($actions as $action) {
  108.                 if ($action->getActionDelay()) {
  109.                     $this->actionTriggerQueue->addToQueue(
  110.                         $action,
  111.                         $customer,
  112.                         $environment
  113.                     );
  114.                 } else {
  115.                     \Pimcore::getContainer()->get('cmf.action_trigger.action_manager')->processAction(
  116.                         $action,
  117.                         $customer,
  118.                         $environment
  119.                     );
  120.                 }
  121.             }
  122.         }
  123.     }
  124.     /**
  125.      * @param EventInterface $event
  126.      * @param bool $checkConditions
  127.      *
  128.      * @return Rule[]
  129.      */
  130.     private function getAppliedRules(EventInterface $eventRuleEnvironmentInterface $environment$checkConditions true)
  131.     {
  132.         $appliedRules = [];
  133.         if (isset($this->getRulesGroupedByEvents()[$event->getName()]) && sizeof(
  134.                 $this->getRulesGroupedByEvents()[$event->getName()]
  135.             )
  136.         ) {
  137.             $rules $this->rulesGroupedByEvents[$event->getName()];
  138.             foreach ($rules as $rule) {
  139.                 foreach ($rule->getTrigger() as $trigger) {
  140.                     if ($event->appliesToTrigger($trigger)) {
  141.                         if ($event instanceof RuleEnvironmentAwareEventInterface) {
  142.                             $event->updateEnvironment($trigger$environment);
  143.                         }
  144.                         if ($checkConditions) {
  145.                             if ($this->checkConditions($rule$event$environment)) {
  146.                                 $appliedRules[] = $rule;
  147.                             }
  148.                         } else {
  149.                             $appliedRules[] = $rule;
  150.                         }
  151.                         break;
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.         return $appliedRules;
  157.     }
  158.     protected function checkConditions(Rule $ruleSingleCustomerEventInterface $eventRuleEnvironmentInterface $environment)
  159.     {
  160.         return Checker::checkConditionsForRuleAndEvent($rule$event$environment);
  161.     }
  162. }