<?php
declare(strict_types=1);
namespace App\Module\Checkout\Subscriber;
use App\Module\ShortSimpleCheckout\Event\View\BaseViewEvent;
use App\Module\ShortSimpleCheckout\Events;
use App\Module\ShortSimpleCheckout\Provider\Customer\CustomerProviderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @package App\Module\Checkout\Subscriber
*/
class ShippingPaymentSubscriber implements EventSubscriberInterface
{
private CustomerProviderInterface $customerProvider;
private RequestStack $requestStack;
private SessionInterface $session;
private TranslatorInterface $translator;
/**
* ShippingPaymentSubscriber constructor.
* @param CustomerProviderInterface $customerProvider
* @param RequestStack $requestStack
* @param SessionInterface $session
* @param TranslatorInterface $translator
*/
public function __construct(
CustomerProviderInterface $customerProvider,
RequestStack $requestStack,
SessionInterface $session,
TranslatorInterface $translator
) {
$this->customerProvider = $customerProvider;
$this->requestStack = $requestStack;
$this->session = $session;
$this->translator = $translator;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
Events::SHIPPING_PAYMENT_INDEX_DISPATCH => 'addCustomerToView'
];
}
/**
* @param BaseViewEvent $event
* @return void
*/
public function addCustomerToView(BaseViewEvent $event): void
{
$event->replaceVariable('customer', $this->customerProvider->getCustomer());
$view = $event->getView();
//prevent double flash
if (!isset($view['showAlreadyLoggedInHint'])) {
$alreadyLoggedIn = (bool) $this->requestStack->getCurrentRequest()->get('alreadyLoggedIn', false);
$event->addVariable('showAlreadyLoggedInHint', $alreadyLoggedIn);
if ($alreadyLoggedIn) {
$this->session->getFlashBag()->add('error', $this->translator->trans('checkout_already_logged_in_hint'));
}
}
}
}