vendor/store.shopware.com/swagb2bplatform/components/Order/BridgePlatform/OrderDeleteSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Order\BridgePlatform;
  3. use Shopware\B2B\Common\IdValue;
  4. use Shopware\B2B\Order\Framework\OrderContextService;
  5. use Shopware\Core\Checkout\Order\OrderDefinition;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use function array_walk;
  12. class OrderDeleteSubscriber implements EventSubscriberInterface
  13. {
  14.     protected OrderContextService $orderContextService;
  15.     public function __construct(OrderContextService $orderContextService)
  16.     {
  17.         $this->orderContextService $orderContextService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             EntityWrittenContainerEvent::class => 'onOrderWrittenEvent',
  23.         ];
  24.     }
  25.     public function onOrderWrittenEvent(EntityWrittenContainerEvent $eventContainer): void
  26.     {
  27.         $event $eventContainer->getEventByEntityName(OrderDefinition::ENTITY_NAME);
  28.         if ($event instanceof EntityDeletedEvent) {
  29.             $this->onOrderDelete($event);
  30.         }
  31.     }
  32.     /**
  33.      * @internal
  34.      */
  35.     protected function onOrderDelete(EntityDeletedEvent $deletedEvent): void
  36.     {
  37.         $list = [];
  38.         $writeResults $deletedEvent->getWriteResults();
  39.         array_walk($writeResults, function (EntityWriteResult $writeResult) use (&$list) {
  40.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_DELETE) {
  41.                 $payload $writeResult->getPayload();
  42.                 if (isset($payload['versionId']) && $payload['versionId'] === Defaults::LIVE_VERSION) {
  43.                     $list[] = IdValue::create($writeResult->getPrimaryKey());
  44.                 }
  45.             }
  46.         });
  47.         $this->orderContextService->deleteContextsByReferencedOrderIds($list);
  48.     }
  49. }