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

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