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

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\Event;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Translation\TranslatorInterface;
  16. /**
  17.  * @internal
  18.  * @final
  19.  */
  20. class FlashListener implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var string[]
  24.      */
  25.     private static $successMessages = [
  26.         FOSUserEvents::CHANGE_PASSWORD_COMPLETED => 'change_password.flash.success',
  27.         FOSUserEvents::GROUP_CREATE_COMPLETED => 'group.flash.created',
  28.         FOSUserEvents::GROUP_DELETE_COMPLETED => 'group.flash.deleted',
  29.         FOSUserEvents::GROUP_EDIT_COMPLETED => 'group.flash.updated',
  30.         FOSUserEvents::PROFILE_EDIT_COMPLETED => 'profile.flash.updated',
  31.         FOSUserEvents::REGISTRATION_COMPLETED => 'registration.flash.user_created',
  32.         FOSUserEvents::RESETTING_RESET_COMPLETED => 'resetting.flash.success',
  33.     ];
  34.     /**
  35.      * @var SessionInterface
  36.      */
  37.     private $session;
  38.     /**
  39.      * @var TranslatorInterface
  40.      */
  41.     private $translator;
  42.     /**
  43.      * FlashListener constructor.
  44.      */
  45.     public function __construct(SessionInterface $sessionTranslatorInterface $translator)
  46.     {
  47.         $this->session $session;
  48.         $this->translator $translator;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public static function getSubscribedEvents()
  54.     {
  55.         return [
  56.             FOSUserEvents::CHANGE_PASSWORD_COMPLETED => 'addSuccessFlash',
  57.             FOSUserEvents::GROUP_CREATE_COMPLETED => 'addSuccessFlash',
  58.             FOSUserEvents::GROUP_DELETE_COMPLETED => 'addSuccessFlash',
  59.             FOSUserEvents::GROUP_EDIT_COMPLETED => 'addSuccessFlash',
  60.             FOSUserEvents::PROFILE_EDIT_COMPLETED => 'addSuccessFlash',
  61.             FOSUserEvents::REGISTRATION_COMPLETED => 'addSuccessFlash',
  62.             FOSUserEvents::RESETTING_RESET_COMPLETED => 'addSuccessFlash',
  63.         ];
  64.     }
  65.     /**
  66.      * @param string $eventName
  67.      */
  68.     public function addSuccessFlash(Event $event$eventName)
  69.     {
  70.         if (!isset(self::$successMessages[$eventName])) {
  71.             throw new \InvalidArgumentException('This event does not correspond to a known flash message');
  72.         }
  73.         $this->session->getFlashBag()->add('success'$this->trans(self::$successMessages[$eventName]));
  74.     }
  75.     /**
  76.      * @param string$message
  77.      *
  78.      * @return string
  79.      */
  80.     private function trans($message, array $params = [])
  81.     {
  82.         return $this->translator->trans($message$params'FOSUserBundle');
  83.     }
  84. }