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

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