<?php declare(strict_types=1);
namespace AgiqonOci\Subscriber;
use AgiqonOci\Services\CxmlResponseGenerator;
use AgiqonOci\Models\OciSession;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use AgiqonOci\Services\DataCollectorService;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class CheckoutSubscriber implements EventSubscriberInterface
{
private DataCollectorService $dataCollectorService;
private CxmlResponseGenerator $cxmlResponseGenerator;
private string $redirectPath;
public function __construct(DataCollectorService $dataCollectorService,
CxmlResponseGenerator $cxmlResponseGenerator,
RouterInterface $router)
{
$this->dataCollectorService = $dataCollectorService;
$this->cxmlResponseGenerator = $cxmlResponseGenerator;
$this->redirectPath = $router->generate('frontend.checkout.cart.page');
}
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'onCheckoutCart',
CheckoutConfirmPageLoadedEvent::class => 'redirectToCart',
CheckoutFinishPageLoadedEvent::class => 'redirectToCart',
OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCart'
];
}
/**
* Gets Oci data if the basket is not empty and Oci Session has been found
*
* @param CheckoutCartPageLoadedEvent $event
*/
public function onCheckoutCart(CheckoutCartPageLoadedEvent $event)
{
$isBasketEmpty = count($event->getPage()->getCart()->getLineItems()->getElements()) === 0;
/** @var OciSession $ociSession */
if (($ociSession = $event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME)) && !$isBasketEmpty) {
$cart = $event->getPage()->getCart();
$isCxml = $ociSession->getSystem() === OciSession::TYPE_CXML;
if(!$isCxml) {
$collectedData = $this->dataCollectorService->collectOciData($cart, $ociSession->getOciSystem(), $event->getSalesChannelContext());
} else {
$collectedData = true;
}
$event->getPage()->assign([
'AgiqonOci' => $collectedData,
'AgiqonOciLink' => $ociSession->getHookUrl(),
'AgiqonOciValidUrl' => $this->isUrlValid($ociSession->getHookUrl()),
'AgiqonOciStartIndex' => $isCxml ? 0 : $ociSession->getOciSystem()->getIndexStart(),
'AgiqonOciCxmlResponse' => $isCxml ? $this->cxmlResponseGenerator->generateCxmlPunchOutOrderMessage($ociSession, $cart, $event->getSalesChannelContext()) : null,
'AgiqonOciSystem' => $event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME)->getSystem()
]);
}
}
/**
* Redirects to the checkout/cart from checkout/confirm and checkout/finish
*
* @param PageLoadedEvent $event
*/
public function redirectToCart(PageLoadedEvent $event)
{
if ($event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME)) {
$response = new RedirectResponse($this->redirectPath);
$response->send();
}
}
/**
* Adds variable to the off-canvas page if Oci Session has been found
*
* @param OffcanvasCartPageLoadedEvent $event
*/
public function onOffcanvasCart(OffcanvasCartPageLoadedEvent $event)
{
if ($event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME)) {
$event->getPage()->assign(['AgiqonOci' => true]);
}
}
private function isUrlValid(?string $url): bool
{
if(!$url) {
return false;
}
return filter_var($url, FILTER_VALIDATE_URL) !== false;
}
}