custom/plugins/CrswCleverReachOfficial/src/Subscriber/Automation/SalesChannelSubscriber.php line 84

Open in your IDE?
  1. <?php
  2. namespace Crsw\CleverReachOfficial\Subscriber\Automation;
  3. use Crsw\CleverReachOfficial\Components\Utility\Bootstrap;
  4. use Crsw\CleverReachOfficial\Components\Utility\Initializer;
  5. use Crsw\CleverReachOfficial\Core\Infrastructure\Exceptions\BaseException;
  6. use Crsw\CleverReachOfficial\Core\Infrastructure\Logger\Logger;
  7. use Crsw\CleverReachOfficial\Entity\SalesChannel\Repositories\SalesChannelRepository;
  8. use Crsw\CleverReachOfficial\Service\BusinessLogic\Automation\AutomationService;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\System\SalesChannel\SalesChannelEvents;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14.  * Class SalesChannelSubscriber
  15.  *
  16.  * @package Crsw\CleverReachOfficial\Subscriber\Automation
  17.  */
  18. class SalesChannelSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var AutomationService
  22.      */
  23.     private $automationService;
  24.     /**
  25.      * @var SalesChannelRepository
  26.      */
  27.     private $salesChannelRepository;
  28.     /**
  29.      * SalesChannelSubscriber constructor.
  30.      *
  31.      * @param Initializer $initializer
  32.      * @param AutomationService $automationService
  33.      * @param SalesChannelRepository $salesChannelRepository
  34.      */
  35.     public function __construct(
  36.         Initializer $initializer,
  37.         AutomationService $automationService,
  38.         SalesChannelRepository $salesChannelRepository
  39.     ) {
  40.         Bootstrap::register();
  41.         $initializer->registerServices();
  42.         $this->automationService $automationService;
  43.         $this->salesChannelRepository $salesChannelRepository;
  44.     }
  45.     /**
  46.      * @inheritDoc
  47.      */
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             SalesChannelEvents::SALES_CHANNEL_DELETED => 'onSalesChannelDelete',
  52.             SalesChannelEvents::SALES_CHANNEL_WRITTEN => 'onSalesChannelSave'
  53.         ];
  54.     }
  55.     /**
  56.      * Removes cart automation when store has been deleted.
  57.      *
  58.      * @param EntityDeletedEvent $event
  59.      */
  60.     public function onSalesChannelDelete(EntityDeletedEvent $event): void
  61.     {
  62.         $ids $event->getIds();
  63.         foreach ($ids as $id) {
  64.             try {
  65.                 $this->automationService->delete($id);
  66.             } catch (BaseException $e) {
  67.                 Logger::logError('Failed to remove automation for store: ' $id'Integration');
  68.             }
  69.         }
  70.     }
  71.     /**
  72.      * Removes cart records when store has been deactivated.
  73.      *
  74.      * @param EntityWrittenEvent $event
  75.      */
  76.     public function onSalesChannelSave(EntityWrittenEvent $event): void
  77.     {
  78.         $ids $event->getIds();
  79.         foreach ($ids as $id) {
  80.             $salesChannel $this->salesChannelRepository->getSalesChannelById($id$event->getContext());
  81.             if (!$salesChannel) {
  82.                 return;
  83.             }
  84.             if (!$salesChannel->getActive()) {
  85.                 try {
  86.                     $this->automationService->deleteRecords($id);
  87.                 } catch (BaseException $e) {
  88.                     Logger::logError('Failed to remove automation records for store: ' $id'Integration');
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }