custom/plugins/CrswCleverReachOfficial/src/Subscriber/CustomerGroups/CustomerGroupSubscriber.php line 93

Open in your IDE?
  1. <?php
  2. namespace Crsw\CleverReachOfficial\Subscriber\CustomerGroups;
  3. use Crsw\CleverReachOfficial\Components\EventHandlers\TagHandler;
  4. use Crsw\CleverReachOfficial\Components\Utility\Bootstrap;
  5. use Crsw\CleverReachOfficial\Components\Utility\Initializer;
  6. use Crsw\CleverReachOfficial\Entity\CustomerGroup\Repositories\CustomerGroupRepository;
  7. use Shopware\Core\Checkout\Customer\CustomerEvents;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. /**
  15.  * Class CustomerGroupSubscriber
  16.  *
  17.  * @package Crsw\CleverReachOfficial\Subscriber\CustomerGroups
  18.  */
  19. class CustomerGroupSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var array
  23.      */
  24.     private static $groupsForDelete = [];
  25.     /**
  26.      * @var TagHandler
  27.      */
  28.     private $tagHandler;
  29.     /**
  30.      * @var CustomerGroupRepository
  31.      */
  32.     private $customerGroupRepository;
  33.     /**
  34.      * CustomerGroupSubscriber constructor.
  35.      *
  36.      * @param TagHandler $tagHandler
  37.      * @param CustomerGroupRepository $customerGroupRepository
  38.      * @param Initializer $initializer
  39.      */
  40.     public function __construct(
  41.         TagHandler $tagHandler,
  42.         CustomerGroupRepository $customerGroupRepository,
  43.         Initializer $initializer
  44.     ) {
  45.         Bootstrap::register();
  46.         $initializer->registerServices();
  47.         $this->tagHandler $tagHandler;
  48.         $this->customerGroupRepository $customerGroupRepository;
  49.     }
  50.     /**
  51.      * @inheritDoc
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             CustomerEvents::CUSTOMER_GROUP_WRITTEN_EVENT => 'onCustomerGroupChange',
  57.             KernelEvents::CONTROLLER => 'saveDataForDelete',
  58.         ];
  59.     }
  60.     /**
  61.      * Customer group created or modified.
  62.      *
  63.      * @param EntityWrittenEvent $event
  64.      */
  65.     public function onCustomerGroupChange(EntityWrittenEvent $event): void
  66.     {
  67.         if (!$this->tagHandler->canHandle()) {
  68.             return;
  69.         }
  70.         $this->tagHandler->tagCreated();
  71.         foreach ($event->getIds() as $id) {
  72.             if (!empty(static::$groupsForDelete[$id])) {
  73.                 $this->tagHandler->enqueueReceiverSyncTask(static::$groupsForDelete[$id]);
  74.                 unset(static::$groupsForDelete[$id]);
  75.             }
  76.         }
  77.     }
  78.     /**
  79.      * @param ControllerEvent $event
  80.      */
  81.     public function saveDataForDelete(ControllerEvent $event): void
  82.     {
  83.         $request $event->getRequest();
  84.         $context $request->get('sw-context');
  85.         if ($request->get('_route') === 'api.customer_group.update') {
  86.             $groupId $request->get('path');
  87.             // check if route contains subpaths
  88.             if (!strpos($groupId'/')) {
  89.                 $this->saveOldGroupName($groupId$context ?:
  90.                     Context::createDefaultContext());
  91.             }
  92.         }
  93.     }
  94.     private function saveOldGroupName(string $groupIdContext $context): void
  95.     {
  96.         $customerGroup $this->customerGroupRepository->getCustomerGroupById($groupId$context);
  97.         if ($customerGroup) {
  98.             static::$groupsForDelete[$customerGroup->getId()] = $customerGroup->getName();
  99.         }
  100.     }
  101. }