<?php
declare(strict_types=1);
namespace Harmonizely\EventListener;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Harmonizely\Model\UserInterface;
use Harmonizely\Service\Panel\Unsubscribe\Contract\IUnsubscribeEmailService;
use Harmonizely\Types\WelcomeTourStepType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
final class SetDefaultUserIntroListener implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $translator;
/**
* @var IUnsubscribeEmailService
*/
private IUnsubscribeEmailService $unsubscribeEmailService;
/**
* @var SessionInterface
*/
private SessionInterface $session;
/**
* @param TranslatorInterface $translator
* @param IUnsubscribeEmailService $unsubscribeEmailService
* @param SessionInterface $session
*/
public function __construct(
TranslatorInterface $translator,
IUnsubscribeEmailService $unsubscribeEmailService,
SessionInterface $session
)
{
$this->translator = $translator;
$this->unsubscribeEmailService = $unsubscribeEmailService;
$this->session = $session;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_INITIALIZE => 'onInitialize',
FOSUserEvents::REGISTRATION_COMPLETED => 'onCompeted',
];
}
public function onInitialize(UserEvent $event)
{
$user = $event->getUser();
if ($user instanceof UserInterface) {
$user->setWelcomeTourStep(WelcomeTourStepType::TYPE_EVENT_TYPE);
$user->setReferral($event->getRequest()->cookies->get('ref'));
$user->setIntro($this->translator->trans('Welcome to my scheduling page. Please follow the instructions to add a meeting to my calendar.'));
}
}
public function onCompeted(FilterUserResponseEvent $event)
{
$user = $event->getUser();
$this->session->set(UserInterface::USER_CONFIRMATION_TOKEN_SESSION_KEY, $user->getConfirmationToken());
$registrationFormData = $event->getRequest()->get('fos_user_registration_form');
if ($registrationFormData && (!isset($registrationFormData['promoLetters']) || !$registrationFormData['promoLetters'])) {
$this->unsubscribeEmailService->perform($user);
}
}
}