Skip to content

Commit

Permalink
created new FlashBagProvider for accessing flash message bag
Browse files Browse the repository at this point in the history
- FlashBagProvider is not wired in contructor anymore because it had side effects (session was started when the container was built)
	- see symfony/symfony#36063
  • Loading branch information
vitek-rostislav committed Jul 26, 2022
1 parent a13d385 commit 27b198d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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.',
Expand Down
33 changes: 33 additions & 0 deletions packages/framework/src/Component/FlashMessage/FlashBagProvider.php
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Component\FlashMessage;

use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class FlashBagProvider
{
/**
* @var \Symfony\Component\HttpFoundation\Session\Session
*/
protected Session $session;

/**
* @param \Symfony\Component\HttpFoundation\Session\Session $session
*/
public function __construct(SessionInterface $session)
{
$this->session = $session;
}

/**
* @return \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
*/
public function getFlashBag(): FlashBagInterface
{
return $this->session->getFlashBag();
}
}
19 changes: 10 additions & 9 deletions packages/framework/src/Model/Cart/Watcher/CartWatcherFacade.php
Expand Up @@ -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
Expand All @@ -28,30 +28,30 @@ 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
*/
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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
);
Expand Down

0 comments on commit 27b198d

Please sign in to comment.