custom/plugins/CrswCleverReachOfficial/src/Subscriber/SalesChannels/SalesChannelSubscriber.php line 78

Open in your IDE?
  1. <?php
  2. namespace Crsw\CleverReachOfficial\Subscriber\SalesChannels;
  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\SalesChannel\Repositories\SalesChannelRepository;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\PlatformRequest;
  13. use Shopware\Core\System\SalesChannel\SalesChannelEvents;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. /**
  20.  * Class SalesChannelSubscriber
  21.  *
  22.  * @package Crsw\CleverReachOfficial\Subscriber\SalesChannels
  23.  */
  24. class SalesChannelSubscriber implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var array
  28.      */
  29.     private static $salesChannelForDelete = [];
  30.     /**
  31.      * @var TagHandler
  32.      */
  33.     private $tagHandler;
  34.     /**
  35.      * @var SalesChannelRepository
  36.      */
  37.     private $salesChannelRepository;
  38.     /**
  39.      * SalesChannelSubscriber constructor.
  40.      *
  41.      * @param TagHandler $tagHandler
  42.      * @param SalesChannelRepository $salesChannelRepository
  43.      * @param Initializer $initializer
  44.      */
  45.     public function __construct(
  46.         TagHandler $tagHandler,
  47.         SalesChannelRepository $salesChannelRepository,
  48.         Initializer $initializer
  49.     ) {
  50.         Bootstrap::register();
  51.         $initializer->registerServices();
  52.         $this->tagHandler $tagHandler;
  53.         $this->salesChannelRepository $salesChannelRepository;
  54.     }
  55.     /**
  56.      * @inheritDoc
  57.      */
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             SalesChannelEvents::SALES_CHANNEL_DELETED => 'onSalesChannelDelete',
  62.             SalesChannelEvents::SALES_CHANNEL_WRITTEN => 'onSalesChannelSave',
  63.             KernelEvents::CONTROLLER => 'saveDataForDelete',
  64.             KernelEvents::RESPONSE => ['onIframeResponse', -256],
  65.         ];
  66.     }
  67.     /**
  68.      * @param ResponseEvent $event
  69.      */
  70.     public function onIframeResponse(ResponseEvent $event): void
  71.     {
  72.         if (str_contains($event->getRequest()->getPathInfo(), '/cleverreach/auth')) {
  73.             $event->getResponse()->headers->remove('Content-Security-Policy');
  74.         }
  75.         $event->getResponse()->headers->set(PlatformRequest::HEADER_FRAME_OPTIONS'allowall');
  76.     }
  77.     /**
  78.      * Sales channel deleted.
  79.      *
  80.      * @param EntityDeletedEvent $event
  81.      */
  82.     public function onSalesChannelDelete(EntityDeletedEvent $event): void
  83.     {
  84.         if (!$this->tagHandler->canHandle()) {
  85.             return;
  86.         }
  87.         $ids $event->getIds();
  88.         foreach ($ids as $id) {
  89.             $salesChannel $this->salesChannelRepository->getSalesChannelById($id$event->getContext());
  90.             if (!$salesChannel) {
  91.                 continue;
  92.             }
  93.             $this->tagHandler->salesChannelDeleted($salesChannel->getName());
  94.         }
  95.     }
  96.     /**
  97.      * Sales channel created or modified.
  98.      *
  99.      * @param EntityWrittenEvent $event
  100.      */
  101.     public function onSalesChannelSave(EntityWrittenEvent $event): void
  102.     {
  103.         if (!$this->tagHandler->canHandle()) {
  104.             return;
  105.         }
  106.         $this->tagHandler->tagCreated();
  107.         foreach ($event->getIds() as $id) {
  108.             if (!empty(static::$salesChannelForDelete[$id])) {
  109.                 $this->tagHandler->salesChannelDeleted(static::$salesChannelForDelete[$id]);
  110.                 unset(static::$salesChannelForDelete[$id]);
  111.             }
  112.         }
  113.     }
  114.     /**
  115.      * @param ControllerEvent $controllerEvent
  116.      */
  117.     public function saveDataForDelete(ControllerEvent $controllerEvent): void
  118.     {
  119.         $request $controllerEvent->getRequest();
  120.         $routeName $request->get('_route');
  121.         if (in_array($routeName, ['api.sales_channel.update''api.sales_channel.delete'], true)) {
  122.             $salesChannelId $request->get('path');
  123.             // check if route contains subpaths
  124.             if (!strpos($salesChannelId'/')) {
  125.                 $this->saveSalesChannelName($request);
  126.             }
  127.         }
  128.     }
  129.     private function saveSalesChannelName(Request $request): void
  130.     {
  131.         $salesChannel $this->salesChannelRepository->getSalesChannelById(
  132.             $request->get('path'),
  133.             $request->get('sw-context') ?: Context::createDefaultContext()
  134.         );
  135.         if ($salesChannel) {
  136.             static::$salesChannelForDelete[$salesChannel->getId()] = $salesChannel->getName();
  137.         }
  138.     }
  139. }