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\Pagination\SlidingPagination;
  27. use Knp\Component\Pager\PaginatorInterface;
  28. class DefaultEventHandler implements EventHandlerInterface
  29. {
  30.     use LoggerAware;
  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.         // var_dump($this->getAppliedRules($event, false) );
  83.         foreach ($this->getAppliedRules($event$environmentfalse) as $rule) {
  84.             if ($conditions $rule->getCondition()) {
  85.                 $where Checker::getDbConditionForRule($rule);
  86.                 $listing \Pimcore::getContainer()->get('cmf.customer_provider')->getList();
  87.                 $listing->setCondition($where);
  88.                 $listing->setOrderKey('o_id');
  89.                 $listing->setOrder('asc');
  90.                 /**
  91.                  * @var $paginator SlidingPagination
  92.                  */
  93.                 $paginator $this->paginator->paginate($listing1100);
  94.                 $this->getLogger()->info(
  95.                     sprintf('handleCustomerListEvent: found %s matching customers'$paginator->getTotalItemCount())
  96.                 );
  97.                 $totalPages $paginator->getPaginationData()['totalCount'];
  98.                 for ($i 1$i <= $totalPages$i++) {
  99.                     $paginator $this->paginator->paginate($listing$i100);
  100.                     foreach ($paginator as $customer) {
  101.                         $this->handleActionsForCustomer($rule$customer$environment);
  102.                     }
  103.                     \Pimcore::collectGarbage();
  104.                 }
  105.             }
  106.         }
  107.     }
  108.     private function handleActionsForCustomer(Rule $ruleCustomerInterface $customerRuleEnvironmentInterface $environment)
  109.     {
  110.         if ($actions $rule->getAction()) {
  111.             foreach ($actions as $action) {
  112.                 if ($action->getActionDelay()) {
  113.                     $this->actionTriggerQueue->addToQueue(
  114.                         $action,
  115.                         $customer,
  116.                         $environment
  117.                     );
  118.                 } else {
  119.                     \Pimcore::getContainer()->get('cmf.action_trigger.action_manager')->processAction(
  120.                         $action,
  121.                         $customer,
  122.                         $environment
  123.                     );
  124.                 }
  125.             }
  126.         }
  127.     }
  128.     /**
  129.      * @param EventInterface $event
  130.      * @param bool $checkConditions
  131.      *
  132.      * @return Rule[]
  133.      */
  134.     private function getAppliedRules(EventInterface $eventRuleEnvironmentInterface $environment$checkConditions true)
  135.     {
  136.         $appliedRules = [];
  137.         if (isset($this->getRulesGroupedByEvents()[$event->getName()]) && sizeof(
  138.                 $this->getRulesGroupedByEvents()[$event->getName()]
  139.             )
  140.         ) {
  141.             $rules $this->rulesGroupedByEvents[$event->getName()];
  142.             foreach ($rules as $rule) {
  143.                 /**
  144.                  * @var Rule $rule ;
  145.                  */
  146.                 foreach ($rule->getTrigger() as $trigger) {
  147.                     if ($event->appliesToTrigger($trigger)) {
  148.                         if ($event instanceof RuleEnvironmentAwareEventInterface) {
  149.                             $event->updateEnvironment($trigger$environment);
  150.                         }
  151.                         if ($checkConditions) {
  152.                             if ($this->checkConditions($rule$event$environment)) {
  153.                                 $appliedRules[] = $rule;
  154.                             }
  155.                         } else {
  156.                             $appliedRules[] = $rule;
  157.                         }
  158.                         break;
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.         return $appliedRules;
  164.     }
  165.     protected function checkConditions(Rule $ruleSingleCustomerEventInterface $eventRuleEnvironmentInterface $environment)
  166.     {
  167.         return Checker::checkConditionsForRuleAndEvent($rule$event$environment);
  168.     }
  169. }