src/Module/Checkout/Subscriber/ShippingPaymentSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Module\Checkout\Subscriber;
  4. use App\Module\ShortSimpleCheckout\Event\View\BaseViewEvent;
  5. use App\Module\ShortSimpleCheckout\Events;
  6. use App\Module\ShortSimpleCheckout\Provider\Customer\CustomerProviderInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. /**
  12. * @package App\Module\Checkout\Subscriber
  13. */
  14. class ShippingPaymentSubscriber implements EventSubscriberInterface
  15. {
  16. private CustomerProviderInterface $customerProvider;
  17. private RequestStack $requestStack;
  18. private SessionInterface $session;
  19. private TranslatorInterface $translator;
  20. /**
  21. * ShippingPaymentSubscriber constructor.
  22. * @param CustomerProviderInterface $customerProvider
  23. * @param RequestStack $requestStack
  24. * @param SessionInterface $session
  25. * @param TranslatorInterface $translator
  26. */
  27. public function __construct(
  28. CustomerProviderInterface $customerProvider,
  29. RequestStack $requestStack,
  30. SessionInterface $session,
  31. TranslatorInterface $translator
  32. ) {
  33. $this->customerProvider = $customerProvider;
  34. $this->requestStack = $requestStack;
  35. $this->session = $session;
  36. $this->translator = $translator;
  37. }
  38. /**
  39. * @return array
  40. */
  41. public static function getSubscribedEvents(): array
  42. {
  43. return [
  44. Events::SHIPPING_PAYMENT_INDEX_DISPATCH => 'addCustomerToView'
  45. ];
  46. }
  47. /**
  48. * @param BaseViewEvent $event
  49. * @return void
  50. */
  51. public function addCustomerToView(BaseViewEvent $event): void
  52. {
  53. $event->replaceVariable('customer', $this->customerProvider->getCustomer());
  54. $view = $event->getView();
  55. //prevent double flash
  56. if (!isset($view['showAlreadyLoggedInHint'])) {
  57. $alreadyLoggedIn = (bool) $this->requestStack->getCurrentRequest()->get('alreadyLoggedIn', false);
  58. $event->addVariable('showAlreadyLoggedInHint', $alreadyLoggedIn);
  59. if ($alreadyLoggedIn) {
  60. $this->session->getFlashBag()->add('error', $this->translator->trans('checkout_already_logged_in_hint'));
  61. }
  62. }
  63. }
  64. }