src/EventListener/SetDefaultUserIntroListener.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventListener;
  4. use FOS\UserBundle\Event\FilterUserResponseEvent;
  5. use FOS\UserBundle\Event\UserEvent;
  6. use FOS\UserBundle\FOSUserEvents;
  7. use Harmonizely\Model\UserInterface;
  8. use Harmonizely\Service\Panel\Unsubscribe\Contract\IUnsubscribeEmailService;
  9. use Harmonizely\Types\WelcomeTourStepType;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. final class SetDefaultUserIntroListener implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var TranslatorInterface
  17.      */
  18.     private TranslatorInterface $translator;
  19.     /**
  20.      * @var IUnsubscribeEmailService
  21.      */
  22.     private IUnsubscribeEmailService $unsubscribeEmailService;
  23.     /**
  24.      * @var SessionInterface
  25.      */
  26.     private SessionInterface $session;
  27.     /**
  28.      * @param TranslatorInterface $translator
  29.      * @param IUnsubscribeEmailService $unsubscribeEmailService
  30.      * @param SessionInterface $session
  31.      */
  32.     public function __construct(
  33.         TranslatorInterface $translator,
  34.         IUnsubscribeEmailService $unsubscribeEmailService,
  35.         SessionInterface $session
  36.     )
  37.     {
  38.         $this->translator $translator;
  39.         $this->unsubscribeEmailService $unsubscribeEmailService;
  40.         $this->session $session;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             FOSUserEvents::REGISTRATION_INITIALIZE => 'onInitialize',
  49.             FOSUserEvents::REGISTRATION_COMPLETED => 'onCompeted',
  50.         ];
  51.     }
  52.     public function onInitialize(UserEvent $event)
  53.     {
  54.         $user $event->getUser();
  55.         if ($user instanceof UserInterface) {
  56.             $user->setWelcomeTourStep(WelcomeTourStepType::TYPE_EVENT_TYPE);
  57.             $user->setReferral($event->getRequest()->cookies->get('ref'));
  58.             $user->setIntro($this->translator->trans('Welcome to my scheduling page. Please follow the instructions to add a meeting to my calendar.'));
  59.         }
  60.     }
  61.     public function onCompeted(FilterUserResponseEvent $event)
  62.     {
  63.         $user $event->getUser();
  64.         $this->session->set(UserInterface::USER_CONFIRMATION_TOKEN_SESSION_KEY$user->getConfirmationToken());
  65.         $registrationFormData $event->getRequest()->get('fos_user_registration_form');
  66.         if ($registrationFormData && (!isset($registrationFormData['promoLetters']) || !$registrationFormData['promoLetters'])) {
  67.             $this->unsubscribeEmailService->perform($user);
  68.         }
  69.     }
  70. }