vendor/store.shopware.com/agiqonoci/src/Subscriber/CheckoutSubscriber.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace AgiqonOci\Subscriber;
  3. use AgiqonOci\Services\CxmlResponseGenerator;
  4. use AgiqonOci\Models\OciSession;
  5. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  6. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  7. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  8. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  9. use Shopware\Storefront\Page\PageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use AgiqonOci\Services\DataCollectorService;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\Routing\RouterInterface;
  14. class CheckoutSubscriber implements EventSubscriberInterface
  15. {
  16.     private DataCollectorService $dataCollectorService;
  17.     private CxmlResponseGenerator $cxmlResponseGenerator;
  18.     private string $redirectPath;
  19.     public function __construct(DataCollectorService  $dataCollectorService,
  20.                                 CxmlResponseGenerator $cxmlResponseGenerator,
  21.                                 RouterInterface       $router)
  22.     {
  23.         $this->dataCollectorService $dataCollectorService;
  24.         $this->cxmlResponseGenerator $cxmlResponseGenerator;
  25.         $this->redirectPath $router->generate('frontend.checkout.cart.page');
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCart',
  31.             CheckoutConfirmPageLoadedEvent::class => 'redirectToCart',
  32.             CheckoutFinishPageLoadedEvent::class => 'redirectToCart',
  33.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCart'
  34.         ];
  35.     }
  36.     /**
  37.      * Gets Oci data if the basket is not empty and Oci Session has been found
  38.      *
  39.      * @param CheckoutCartPageLoadedEvent $event
  40.      */
  41.     public function onCheckoutCart(CheckoutCartPageLoadedEvent $event)
  42.     {
  43.         $isBasketEmpty count($event->getPage()->getCart()->getLineItems()->getElements()) === 0;
  44.         /** @var OciSession $ociSession */
  45.         if (($ociSession $event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME)) && !$isBasketEmpty) {
  46.             $cart $event->getPage()->getCart();
  47.             $isCxml $ociSession->getSystem() === OciSession::TYPE_CXML;
  48.             if(!$isCxml) {
  49.                 $collectedData $this->dataCollectorService->collectOciData($cart$ociSession->getOciSystem(), $event->getSalesChannelContext());
  50.             } else {
  51.                 $collectedData true;
  52.             }
  53.             $event->getPage()->assign([
  54.                 'AgiqonOci' => $collectedData,
  55.                 'AgiqonOciLink' => $ociSession->getHookUrl(),
  56.                 'AgiqonOciValidUrl' => $this->isUrlValid($ociSession->getHookUrl()),
  57.                 'AgiqonOciStartIndex' => $isCxml $ociSession->getOciSystem()->getIndexStart(),
  58.                 'AgiqonOciCxmlResponse' => $isCxml $this->cxmlResponseGenerator->generateCxmlPunchOutOrderMessage($ociSession$cart$event->getSalesChannelContext()) : null,
  59.                 'AgiqonOciSystem' => $event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME)->getSystem()
  60.             ]);
  61.         }
  62.     }
  63.     /**
  64.      * Redirects to the checkout/cart from checkout/confirm and checkout/finish
  65.      *
  66.      * @param PageLoadedEvent $event
  67.      */
  68.     public function redirectToCart(PageLoadedEvent $event)
  69.     {
  70.         if ($event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME)) {
  71.             $response = new RedirectResponse($this->redirectPath);
  72.             $response->send();
  73.         }
  74.     }
  75.     /**
  76.      * Adds variable to the off-canvas page if Oci Session has been found
  77.      *
  78.      * @param OffcanvasCartPageLoadedEvent $event
  79.      */
  80.     public function onOffcanvasCart(OffcanvasCartPageLoadedEvent $event)
  81.     {
  82.         if ($event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME)) {
  83.             $event->getPage()->assign(['AgiqonOci' => true]);
  84.         }
  85.     }
  86.     private function isUrlValid(?string $url): bool
  87.     {
  88.         if(!$url) {
  89.             return false;
  90.         }
  91.         return filter_var($urlFILTER_VALIDATE_URL) !== false;
  92.     }
  93. }