src/mywebsolutions/user-bundle/EventListener/FlashListener.php line 52

Open in your IDE?
  1. <?php
  2. /**
  3.  * Class FlashListener
  4.  * @package MWS\UserBundle\EventListener
  5.  * @author Martin Walther <martin@myweb.solutions>
  6.  *
  7.  * (c) MyWebSolutions
  8.  */
  9. namespace MWS\UserBundle\EventListener;
  10. use MWS\UserBundle\MWSUserEvents;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Contracts\EventDispatcher\Event;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class FlashListener implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var string[]
  20.      */
  21.     private static $successMessages = array(
  22.         MWSUserEvents::CHANGE_PASSWORD_COMPLETED => 'change_password.flash.success',
  23.         MWSUserEvents::GROUP_CREATE_COMPLETED => 'group.flash.created',
  24.         MWSUserEvents::GROUP_DELETE_COMPLETED => 'group.flash.deleted',
  25.         MWSUserEvents::GROUP_EDIT_COMPLETED => 'group.flash.updated',
  26.         MWSUserEvents::PROFILE_EDIT_COMPLETED => 'profile.flash.updated',
  27.         MWSUserEvents::REGISTRATION_COMPLETED => 'registration.flash.user_created',
  28.         MWSUserEvents::RESETTING_RESET_COMPLETED => 'resetting.flash.success',
  29.     );
  30.     /**
  31.      * @var Session
  32.      */
  33.     private $session;
  34.     /**
  35.      * @var TranslatorInterface
  36.      */
  37.     private $translator;
  38.     /**
  39.      * FlashListener constructor.
  40.      *
  41.      * @param RequestStack             $requestStack
  42.      * @param TranslatorInterface $translator
  43.      */
  44.     public function __construct(RequestStack $requestStackTranslatorInterface $translator)
  45.     {
  46.         $this->session $requestStack->getSession();
  47.         $this->translator $translator;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public static function getSubscribedEvents()
  53.     {
  54.         return array(
  55.             MWSUserEvents::CHANGE_PASSWORD_COMPLETED => 'addSuccessFlash',
  56.             MWSUserEvents::GROUP_CREATE_COMPLETED => 'addSuccessFlash',
  57.             MWSUserEvents::GROUP_DELETE_COMPLETED => 'addSuccessFlash',
  58.             MWSUserEvents::GROUP_EDIT_COMPLETED => 'addSuccessFlash',
  59.             MWSUserEvents::PROFILE_EDIT_COMPLETED => 'addSuccessFlash',
  60.             MWSUserEvents::REGISTRATION_COMPLETED => 'addSuccessFlash',
  61.             MWSUserEvents::RESETTING_RESET_COMPLETED => 'addSuccessFlash',
  62.         );
  63.     }
  64.     /**
  65.      * @param Event  $event
  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.      * @param array $params
  78.      *
  79.      * @return string
  80.      */
  81.     private function trans($message, array $params = array())
  82.     {
  83.         return $this->translator->trans($message$params'MWSUserBundle');
  84.     }
  85. }