diff --git a/packages/framework/src/Component/Error/LogoutExceptionSubscriber.php b/packages/framework/src/Component/Error/LogoutExceptionSubscriber.php index b35fa5321ac..87d59add81b 100644 --- a/packages/framework/src/Component/Error/LogoutExceptionSubscriber.php +++ b/packages/framework/src/Component/Error/LogoutExceptionSubscriber.php @@ -5,11 +5,11 @@ namespace Shopsys\FrameworkBundle\Component\Error; use Shopsys\FrameworkBundle\Component\Domain\Domain; +use Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider; use Shopsys\FrameworkBundle\Component\FlashMessage\FlashMessage; use Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\HttpKernel\Event\ExceptionEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Routing\RouterInterface; @@ -18,9 +18,9 @@ class LogoutExceptionSubscriber implements EventSubscriberInterface { /** - * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface + * @var \Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider */ - protected $flashBag; + protected FlashBagProvider $flashBagProvider; /** * @var \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser @@ -38,18 +38,18 @@ class LogoutExceptionSubscriber implements EventSubscriberInterface protected $domain; /** - * @param \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface $flashBag + * @param \Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider $flashBagProvider * @param \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser $currentCustomerUser * @param \Symfony\Component\Routing\RouterInterface $router * @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain */ public function __construct( - FlashBagInterface $flashBag, + FlashBagProvider $flashBagProvider, CurrentCustomerUser $currentCustomerUser, RouterInterface $router, Domain $domain ) { - $this->flashBag = $flashBag; + $this->flashBagProvider = $flashBagProvider; $this->currentCustomerUser = $currentCustomerUser; $this->router = $router; $this->domain = $domain; @@ -78,7 +78,7 @@ public function onKernelException(ExceptionEvent $event): void $domainId = $this->currentCustomerUser->findCurrentCustomerUser()->getDomainId(); $locale = $this->domain->getDomainConfigById($domainId)->getLocale(); - $this->flashBag->add( + $this->flashBagProvider->getFlashBag()?->add( FlashMessage::KEY_ERROR, t( 'There was an error during logout attempt. If you really want to sign out, please try it again.', diff --git a/packages/framework/src/Component/FlashMessage/FlashBagProvider.php b/packages/framework/src/Component/FlashMessage/FlashBagProvider.php new file mode 100644 index 00000000000..c19340c72fb --- /dev/null +++ b/packages/framework/src/Component/FlashMessage/FlashBagProvider.php @@ -0,0 +1,40 @@ +requestStack = $requestStack; + } + + /** + * @return \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface|null + */ + public function getFlashBag(): ?FlashBagInterface + { + $currentRequest = $this->requestStack->getCurrentRequest(); + if ($currentRequest === null) { + return null; + } + + /** @var \Symfony\Component\HttpFoundation\Session\Session $session */ + $session = $currentRequest->getSession(); + + return $session->getFlashBag(); + } +} diff --git a/packages/framework/src/Model/Cart/Watcher/CartWatcherFacade.php b/packages/framework/src/Model/Cart/Watcher/CartWatcherFacade.php index 1b397e1f641..38694a2ab69 100644 --- a/packages/framework/src/Model/Cart/Watcher/CartWatcherFacade.php +++ b/packages/framework/src/Model/Cart/Watcher/CartWatcherFacade.php @@ -3,11 +3,11 @@ namespace Shopsys\FrameworkBundle\Model\Cart\Watcher; use Doctrine\ORM\EntityManagerInterface; +use Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider; use Shopsys\FrameworkBundle\Component\FlashMessage\FlashMessage; use Shopsys\FrameworkBundle\Model\Cart\Cart; use Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser; use Shopsys\FrameworkBundle\Model\Product\Exception\ProductNotFoundException; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Twig\Environment; abstract class CartWatcherFacade @@ -28,9 +28,9 @@ abstract class CartWatcherFacade protected $currentCustomerUser; /** - * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface + * @var \Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider */ - protected $flashBag; + protected FlashBagProvider $flashBagProvider; /** * @var \Twig\Environment @@ -38,20 +38,20 @@ abstract class CartWatcherFacade protected $twigEnvironment; /** - * @param \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface $flashBag + * @param \Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider $flashBagProvider * @param \Doctrine\ORM\EntityManagerInterface $em * @param \Shopsys\FrameworkBundle\Model\Cart\Watcher\CartWatcher $cartWatcher * @param \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser $currentCustomerUser * @param \Twig\Environment $twigEnvironment */ public function __construct( - FlashBagInterface $flashBag, + FlashBagProvider $flashBagProvider, EntityManagerInterface $em, CartWatcher $cartWatcher, CurrentCustomerUser $currentCustomerUser, Environment $twigEnvironment ) { - $this->flashBag = $flashBag; + $this->flashBagProvider = $flashBagProvider; $this->em = $em; $this->cartWatcher = $cartWatcher; $this->currentCustomerUser = $currentCustomerUser; @@ -79,7 +79,7 @@ protected function checkModifiedPrices(Cart $cart) ); foreach ($modifiedItems as $cartItem) { - $this->flashBag->add(FlashMessage::KEY_INFO, $messageTemplate->render(['name' => $cartItem->getName()])); + $this->flashBagProvider->getFlashBag()?->add(FlashMessage::KEY_INFO, $messageTemplate->render(['name' => $cartItem->getName()])); } if (count($modifiedItems) > 0) { @@ -100,12 +100,13 @@ protected function checkNotListableItems(Cart $cart) $this->getMessageForNoLongerAvailableExistingProduct() ); + $flashBag = $this->flashBagProvider->getFlashBag(); foreach ($notVisibleItems as $cartItem) { try { $productName = $cartItem->getName(); - $this->flashBag->add(FlashMessage::KEY_ERROR, $messageTemplate->render(['name' => $productName])); + $flashBag?->add(FlashMessage::KEY_ERROR, $messageTemplate->render(['name' => $productName])); } catch (ProductNotFoundException $e) { - $this->flashBag->add( + $flashBag?->add( FlashMessage::KEY_ERROR, $this->getMessageForNoLongerAvailableProduct() );