vendor/crehler/inspirations/src/Subscriber/ProductWriteSubscriber.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   25 wrz 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Inspirations\Subscriber;
  13. use Crehler\Inspirations\Entity\Inspiration\Aggregate\InspirationProduct\InspirationProductDefinition;
  14. use Crehler\Inspirations\Message\WriteInspirationProductMessage;
  15. use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
  16. use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
  17. use Shopware\Core\Content\Product\ProductEvents;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\Messenger\MessageBusInterface;
  21. class ProductWriteSubscriber implements EventSubscriberInterface
  22. {
  23.     private MessageBusInterface $messageBus;
  24.     private CrehlerContextService $crehlerContext;
  25.     public function __construct(MessageBusInterface $messageBusCrehlerContextService $crehlerContext)
  26.     {
  27.         $this->messageBus $messageBus;
  28.         $this->crehlerContext $crehlerContext;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWrite',
  34.             'inspiration_product.written' => 'onProductWrite'
  35.         ];
  36.     }
  37.     public function onProductWrite(EntityWrittenEvent $event): void
  38.     {
  39.         $context $event->getContext();
  40.         if ($this->crehlerContext->get(CrehlerContextService::CREHLER_PRODUCT_IMPORT_COMMANDfalse)
  41.             || ($context->hasExtension('isFromMessage') && $context->getExtension('isFromMessage')->isFromMessage())) {
  42.             return;
  43.         }
  44.         $message = new WriteInspirationProductMessage();
  45.         if ($event->getEntityName() === InspirationProductDefinition::ENTITY_NAME) {
  46.             $message->setIsInspirationProducts(true);
  47.         }
  48.         $message->withContext($context)
  49.             ->setIds($event->getIds());
  50.         $this->messageBus->dispatch($message, [new TransportConfiguration(['topic' => 'crehlerinspirations'])]);
  51.     }
  52. }