<?php
/**
* @copyright 2020 Crehler Sp. z o. o.
* @link https://crehler.com/
* @support support@crehler.com
*
* @author Mateusz FlasiĆski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Catalog\Subscriber;
use Crehler\Catalog\Service\OrderCustomFieldsService;
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderWrittenSubscriber implements EventSubscriberInterface
{
private array $handled = [];
private OrderCustomFieldsService $orderCustomFieldsService;
public function __construct(OrderCustomFieldsService $orderCustomFieldsService)
{
$this->orderCustomFieldsService = $orderCustomFieldsService;
}
public static function getSubscribedEvents()
{
return [OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten'];
}
public function onOrderWritten(EntityWrittenEvent $event)
{
foreach ($event->getWriteResults() as $entityWriteResult) {
if (in_array($entityWriteResult->getPrimaryKey(), $this->handled)) {
continue;
}
if ($entityWriteResult->getEntityName() !== OrderDefinition::ENTITY_NAME) {
continue;
}
$this->handled[] = $entityWriteResult->getPrimaryKey();
$this->orderCustomFieldsService->handle($entityWriteResult->getPrimaryKey(), $event->getContext());
}
}
}