vendor/store.shopware.com/swagb2bplatform/components/Cart/BridgePlatform/CartFinishSubscriber.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Cart\BridgePlatform;
  3. use Shopware\B2B\Order\BridgePlatform\OrderContextCreatedEvent;
  4. use Shopware\B2B\Order\Framework\OrderContext;
  5. use Shopware\Core\Checkout\Cart\Cart;
  6. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  7. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  8. use Shopware\Core\Checkout\Order\OrderEntity;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. abstract class CartFinishSubscriber implements EventSubscriberInterface
  13. {
  14.     private const PRIORITY_EARLY 10;
  15.     /**
  16.      * @var CartConvertedEvent
  17.      */
  18.     protected $cartConvertedEvent;
  19.     /**
  20.      * @var EventDispatcherInterface
  21.      */
  22.     protected $eventDispatcher;
  23.     public function __construct(EventDispatcherInterface $eventDispatcher)
  24.     {
  25.         $this->eventDispatcher $eventDispatcher;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CartConvertedEvent::class => ['initializeState'self::PRIORITY_EARLY],
  31.             CheckoutOrderPlacedEvent::class => ['handleCreatedOrder'self::PRIORITY_EARLY],
  32.         ];
  33.     }
  34.     public function initializeState(CartConvertedEvent $event): void
  35.     {
  36.         $this->cartConvertedEvent $event;
  37.     }
  38.     public function handleCreatedOrder(CheckoutOrderPlacedEvent $event): void
  39.     {
  40.         $this->testStateValid($event);
  41.         $orderContext $this->onCartFinish(
  42.             $this->cartConvertedEvent->getCart(),
  43.             $event->getOrder(),
  44.             $this->cartConvertedEvent->getSalesChannelContext()
  45.         );
  46.         if ($orderContext) {
  47.             $this->eventDispatcher->dispatch(new OrderContextCreatedEvent($orderContext));
  48.         }
  49.         $this->reset();
  50.     }
  51.     abstract protected function onCartFinish(Cart $cartOrderEntity $orderEntitySalesChannelContext $salesChannelContext): ?OrderContext;
  52.     protected function getCartState(): CartState
  53.     {
  54.         return $this
  55.             ->cartConvertedEvent
  56.             ->getCart()
  57.             ->getExtension(CartState::NAME);
  58.     }
  59.     /**
  60.      * @internal
  61.      */
  62.     protected function testStateValid(CheckoutOrderPlacedEvent $orderPlacedEvent): void
  63.     {
  64.         if (!$this->cartConvertedEvent) {
  65.             throw new CartFinishException('Placing order without dispatching cart converted');
  66.         }
  67.         $orderNumber $this->cartConvertedEvent->getConvertedCart()['orderNumber'];
  68.         if ($orderNumber !== $orderPlacedEvent->getOrder()->getOrderNumber()) {
  69.             throw new CartFinishException('CheckoutOrderPlacedEvent contains different order number than CartConvertedEvent');
  70.         }
  71.         $salesChannelContext $this->cartConvertedEvent->getSalesChannelContext();
  72.         if ($salesChannelContext->getSalesChannel()->getId() !== $orderPlacedEvent->getSalesChannelId()) {
  73.             throw new CartFinishException('SalesChannel id differs between events');
  74.         }
  75.     }
  76.     private function reset(): void
  77.     {
  78.         $this->cartConvertedEvent null;
  79.     }
  80. }