<?php declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 09 gru 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\DefaultProductData\Subscriber;
use Crehler\DefaultProductData\Message\WriteProductMessage;
use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class ProductWriteSubscriber implements EventSubscriberInterface
{
private MessageBusInterface $messageBus;
public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWrite'
];
}
public function onProductWrite(EntityWrittenEvent $event): void
{
$message = new WriteProductMessage();
$message->withContext($event->getContext())
->setIds($event->getIds());
$this->messageBus->dispatch(
$message,
[new TransportConfiguration(['topic' => 'crehlerdefaultproductdata'])]
);
}
}