vendor/friendsofsymfony/user-bundle/EventListener/FlashListener.php line 72

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\UserBundle\EventListener;
  11. use FOS\UserBundle\FOSUserEvents;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Contracts\EventDispatcher\Event;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. /**
  18.  * @internal
  19.  * @final
  20.  */
  21. class FlashListener implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var string[]
  25.      */
  26.     private static $successMessages = [
  27.         FOSUserEvents::CHANGE_PASSWORD_COMPLETED => 'change_password.flash.success',
  28.         FOSUserEvents::PROFILE_EDIT_COMPLETED => 'profile.flash.updated',
  29.         FOSUserEvents::REGISTRATION_COMPLETED => 'registration.flash.user_created',
  30.         FOSUserEvents::RESETTING_RESET_COMPLETED => 'resetting.flash.success',
  31.     ];
  32.     /**
  33.      * @var RequestStack
  34.      */
  35.     private $requestStack;
  36.     /**
  37.      * @var TranslatorInterface
  38.      */
  39.     private $translator;
  40.     /**
  41.      * FlashListener constructor.
  42.      */
  43.     public function __construct(RequestStack $requestStackTranslatorInterface $translator)
  44.     {
  45.         $this->translator $translator;
  46.         $this->requestStack $requestStack;
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public static function getSubscribedEvents(): array
  52.     {
  53.         return [
  54.             FOSUserEvents::CHANGE_PASSWORD_COMPLETED => 'addSuccessFlash',
  55.             FOSUserEvents::PROFILE_EDIT_COMPLETED => 'addSuccessFlash',
  56.             FOSUserEvents::REGISTRATION_COMPLETED => 'addSuccessFlash',
  57.             FOSUserEvents::RESETTING_RESET_COMPLETED => 'addSuccessFlash',
  58.         ];
  59.     }
  60.     /**
  61.      * @param string $eventName
  62.      */
  63.     public function addSuccessFlash(Event $event$eventName)
  64.     {
  65.         if (!isset(self::$successMessages[$eventName])) {
  66.             throw new \InvalidArgumentException('This event does not correspond to a known flash message');
  67.         }
  68.         $this->getSession()->getFlashBag()->add('success'$this->trans(self::$successMessages[$eventName]));
  69.     }
  70.     private function getSession(): Session
  71.     {
  72.         $request $this->requestStack->getCurrentRequest();
  73.         if (null === $request) {
  74.             throw new \LogicException('Cannot get the session without an active request.');
  75.         }
  76.         return $request->getSession();
  77.     }
  78.     /**
  79.      * @param string $message
  80.      */
  81.     private function trans($message, array $params = []): string
  82.     {
  83.         return $this->translator->trans($message$params'FOSUserBundle');
  84.     }
  85. }