<?php declare(strict_types=1);
namespace Nxs\NxsDeliveryPriceSorting\Communication\Subscriber;
use Nxs\NxsDeliveryPriceSorting\Business\BusinessFacadeInterface;
use Shopware\Core\Checkout\Cart\Exception\InvalidQuantityException;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class CartSubscriber implements EventSubscriberInterface
{
/**
* @var BusinessFacadeInterface
*/
private $businessFacade;
public function __construct(BusinessFacadeInterface $businessFacade)
{
$this->businessFacade = $businessFacade;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'onCartAccess',
CheckoutConfirmPageLoadedEvent::class => 'onCartAccess',
OffcanvasCartPageLoadedEvent::class => 'onCartAccess',
CheckoutRegisterPageLoadedEvent::class => 'onCartAccess',
CartConvertedEvent::class => 'onCartCreationInsideOrderProcess',
];
}
/**
* @param PageLoadedEvent $event
*/
public function onCartAccess(PageLoadedEvent $event): void
{
try {
$this->businessFacade->modifyDeliveryPrices($event);
} catch (InvalidQuantityException | NotFoundHttpException | InconsistentCriteriaIdsException $e) {
}
}
/**
* @param CartConvertedEvent $event
*/
public function onCartCreationInsideOrderProcess(CartConvertedEvent $event): void
{
try{
$this->businessFacade->modifyDeliveryPricesOrderProcess($event);
} catch (InvalidQuantityException | NotFoundHttpException | InconsistentCriteriaIdsException $e) {
}
}
}