custom/plugins/SwagSocialShopping/src/EventListener/DataFeedEventListener.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace SwagSocialShopping\EventListener;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  14. use Shopware\Core\System\SalesChannel\SalesChannelEvents;
  15. use SwagSocialShopping\Component\DataFeed\DataFeedHandler;
  16. use SwagSocialShopping\SwagSocialShopping;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class DataFeedEventListener implements EventSubscriberInterface
  19. {
  20.     private DataFeedHandler $dataFeedHandler;
  21.     private EntityRepositoryInterface $salesChannelRepository;
  22.     public function __construct(DataFeedHandler $dataFeedHandlerEntityRepositoryInterface $salesChannelRepository)
  23.     {
  24.         $this->dataFeedHandler $dataFeedHandler;
  25.         $this->salesChannelRepository $salesChannelRepository;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             SwagSocialShopping::SOCIAL_SHOPPING_SALES_CHANNEL_WRITTEN_EVENT => 'afterWrite',
  31.             SalesChannelEvents::SALES_CHANNEL_WRITTEN => 'afterWriteSalesChannel',
  32.         ];
  33.     }
  34.     public function afterWrite(EntityWrittenEvent $event): void
  35.     {
  36.         foreach ($event->getWriteResults() as $writeResult) {
  37.             if (!$this->needsDataFeed($writeResult)) {
  38.                 continue;
  39.             }
  40.             $this->dataFeedHandler->createDataFeedForWriteResult($writeResult$event);
  41.         }
  42.     }
  43.     public function afterWriteSalesChannel(EntityWrittenEvent $event): void
  44.     {
  45.         foreach ($event->getWriteResults() as $writeResult) {
  46.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_DELETE
  47.                 || $writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT) {
  48.                 continue;
  49.             }
  50.             $payload $writeResult->getPayload();
  51.             if (!empty($payload['typeId']) && $payload['typeId'] !== SwagSocialShopping::SALES_CHANNEL_TYPE_SOCIAL_SHOPPING) {
  52.                 continue;
  53.             }
  54.             if (empty($payload['id'])) {
  55.                 continue;
  56.             }
  57.             if (empty($payload['typeId'])) {
  58.                 /** @var SalesChannelEntity|null $salesChannel */
  59.                 $salesChannel $this->salesChannelRepository->search(new Criteria([$payload['id']]), $event->getContext())->first();
  60.                 if ($salesChannel === null || $salesChannel->getTypeId() !== SwagSocialShopping::SALES_CHANNEL_TYPE_SOCIAL_SHOPPING) {
  61.                     continue;
  62.                 }
  63.             }
  64.             $this->dataFeedHandler->updateActiveStatus($writeResult$event->getContext());
  65.         }
  66.     }
  67.     private function needsDataFeed(EntityWriteResult $writeResult): bool
  68.     {
  69.         if ($writeResult->getEntityName() !== 'swag_social_shopping_sales_channel') {
  70.             return false;
  71.         }
  72.         if (\in_array($writeResult->getOperation(), [
  73.             EntityWriteResult::OPERATION_DELETE,
  74.             EntityWriteResult::OPERATION_UPDATE,
  75.         ], true)) {
  76.             return false;
  77.         }
  78.         if (isset($writeResult->getPayload()['network'])
  79.             && \in_array($writeResult->getPayload()['network'], DataFeedHandler::RELEVANT_NETWORKStrue)) {
  80.             return true;
  81.         }
  82.         return !empty($writeResult->getPayload()['id']);
  83.     }
  84. }