vendor/dachcom-digital/members/src/MembersBundle/EventListener/FlashListener.php line 43

Open in your IDE?
  1. <?php
  2. namespace MembersBundle\EventListener;
  3. use MembersBundle\MembersEvents;
  4. use Symfony\Contracts\EventDispatcher\Event;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Pimcore\Translation\Translator;
  9. class FlashListener implements EventSubscriberInterface
  10. {
  11.     private static array $successMessages = [
  12.         MembersEvents::CHANGE_PASSWORD_COMPLETED        => 'members.change_password.flash.success',
  13.         MembersEvents::PROFILE_EDIT_COMPLETED           => 'members.profile.flash.updated',
  14.         MembersEvents::REGISTRATION_COMPLETED           => 'members.registration.flash.user_created',
  15.         MembersEvents::RESETTING_RESET_COMPLETED        => 'members.resetting.flash.success',
  16.         MembersEvents::OAUTH_PROFILE_CONNECTION_SUCCESS => 'members.oauth.connection.success',
  17.     ];
  18.     protected SessionInterface $session;
  19.     protected Translator $translator;
  20.     public function __construct(SessionInterface $sessionTranslator $translator)
  21.     {
  22.         $this->session $session;
  23.         $this->translator $translator;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             MembersEvents::CHANGE_PASSWORD_COMPLETED        => 'addSuccessFlash',
  29.             MembersEvents::PROFILE_EDIT_COMPLETED           => 'addSuccessFlash',
  30.             MembersEvents::REGISTRATION_COMPLETED           => 'addSuccessFlash',
  31.             MembersEvents::RESETTING_RESET_COMPLETED        => 'addSuccessFlash',
  32.             // oauth events
  33.             MembersEvents::OAUTH_PROFILE_CONNECTION_SUCCESS => 'addSuccessFlash',
  34.         ];
  35.     }
  36.     public function addSuccessFlash(Event $eventstring $eventName): void
  37.     {
  38.         if (!isset(self::$successMessages[$eventName])) {
  39.             throw new \InvalidArgumentException('This event does not correspond to a known flash message.');
  40.         }
  41.         if (!$this->session instanceof Session) {
  42.             throw new \InvalidArgumentException('"%s" needs to be an instance of "%s".'get_class($this->session), Session::class);
  43.         }
  44.         $this->session->getFlashBag()->add('success'$this->trans(self::$successMessages[$eventName]));
  45.     }
  46.     private function trans(string $message, array $params = []): string
  47.     {
  48.         return $this->translator->trans($message$params);
  49.     }
  50. }