<?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 25 wrz 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Arrangements\Subscriber;
use Crehler\Arrangements\Entity\Arrangement\Aggregate\ArrangementProduct\ArrangementProductDefinition;
use Crehler\Arrangements\Message\WriteArrangementProductMessage;
use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
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;
private CrehlerContextService $crehlerContext;
public function __construct(MessageBusInterface $messageBus, CrehlerContextService $crehlerContext)
{
$this->messageBus = $messageBus;
$this->crehlerContext = $crehlerContext;
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWrite',
'arrangement_product.written' => 'onProductWrite',
];
}
public function onProductWrite(EntityWrittenEvent $event): void
{
$context = $event->getContext();
if ($this->crehlerContext->get(CrehlerContextService::CREHLER_PRODUCT_IMPORT_COMMAND, false)
|| ($context->hasExtension('isFromMessage') && $context->getExtension('isFromMessage')->isFromMessage())) {
return;
}
$message = new WriteArrangementProductMessage();
if ($event->getEntityName() === ArrangementProductDefinition::ENTITY_NAME) {
$message->setIsArrangementProducts(true);
}
$message->withContext($context)
->setIds($event->getIds());
$this->messageBus->dispatch($message, [new TransportConfiguration(['topic' => 'crehlerarrangements'])]);
}
}