<?php declare(strict_types=1);
namespace AgiqonOci\Subscriber;
use AgiqonOci\Models\CxmlSession;
use AgiqonOci\Models\OciSession;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Framework\Routing\Router;
use Shopware\Storefront\Page\Account\CustomerGroupRegistration\CustomerGroupRegistrationPageLoadedEvent;
use Shopware\Storefront\Page\Account\Document\DocumentPageLoadedEvent;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
use Shopware\Storefront\Page\Account\Order\AccountOrderDetailPageLoadedEvent;
use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class AccountSubscriber implements EventSubscriberInterface
{
private SystemConfigService $configService;
private string $redirectPath;
public function __construct(SystemConfigService $configService, RouterInterface $router)
{
$this->configService = $configService;
$this->redirectPath = $router->generate('frontend.checkout.cart.page');
}
public static function getSubscribedEvents(): array
{
return [
CustomerGroupRegistrationPageLoadedEvent::class => 'redirectToCart',
DocumentPageLoadedEvent::class => 'redirectToCart',
AccountLoginPageLoadedEvent::class => 'redirectToCart',
AccountEditOrderPageLoadedEvent::class => 'redirectToCart',
AccountOrderDetailPageLoadedEvent::class => 'redirectToCart',
AccountOrderPageLoadedEvent::class => 'redirectToCart',
AccountOverviewPageLoadedEvent::class => 'redirectToCart',
AccountPaymentMethodPageLoadedEvent::class => 'redirectToCart',
AccountProfilePageLoadedEvent::class => 'redirectToCart',
HeaderPageletLoadedEvent::class => 'onHeaderLoaded'
];
}
/**
* 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();
}
}
/**
* Gives information to the template, if OCI session exists, to remove the account icon.
*
* @param HeaderPageletLoadedEvent $event
*/
public function onHeaderLoaded(HeaderPageletLoadedEvent $event)
{
$ociSession = $event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME);
if ($ociSession) {
$event->getPagelet()->assign(['AgiqonOciSession' => true]);
$showLogoutButton = $this->configService->getBool('AgiqonOci.config.showLogoutButton', $event->getSalesChannelContext()->getSalesChannelId());
$event->getPagelet()->assign(['AgiqonOciShowLogoutButton' => $showLogoutButton]);
if($ociSession instanceof CxmlSession && $ociSession->getOperation() === CxmlSession::INSPECT) {
$event->getPagelet()->assign([
'AgiqonCxmlInspect' => true
]);
}
}
}
}