vendor/crehler/mojebambino-catalog/src/Subscriber/OrderWrittenSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o.
  4.  * @link https://crehler.com/
  5.  * @support support@crehler.com
  6.  *
  7.  * @author Mateusz FlasiƄski
  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\Catalog\Subscriber;
  13. use Crehler\Catalog\Service\OrderCustomFieldsService;
  14. use Shopware\Core\Checkout\Order\OrderDefinition;
  15. use Shopware\Core\Checkout\Order\OrderEvents;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class OrderWrittenSubscriber implements EventSubscriberInterface
  19. {
  20.     private array $handled = [];
  21.     private OrderCustomFieldsService $orderCustomFieldsService;
  22.     public function __construct(OrderCustomFieldsService $orderCustomFieldsService)
  23.     {
  24.         $this->orderCustomFieldsService $orderCustomFieldsService;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten'];
  29.     }
  30.     public function onOrderWritten(EntityWrittenEvent $event)
  31.     {
  32.         foreach ($event->getWriteResults() as $entityWriteResult) {
  33.             if (in_array($entityWriteResult->getPrimaryKey(), $this->handled)) {
  34.                 continue;
  35.             }
  36.             if ($entityWriteResult->getEntityName() !== OrderDefinition::ENTITY_NAME) {
  37.                 continue;
  38.             }
  39.             $this->handled[] = $entityWriteResult->getPrimaryKey();
  40.             $this->orderCustomFieldsService->handle($entityWriteResult->getPrimaryKey(), $event->getContext());
  41.         }
  42.     }
  43. }