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

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