src/EventSubscriber/RequireProfileSettingsSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use Harmonizely\Model\UserInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Harmonizely\Core\Exception\AccountDisabledException;
  12. final class RequireProfileSettingsSubscriber implements EventSubscriberInterface
  13. {
  14.     private $tokenStorage;
  15.     private $router;
  16.     public function __construct(RouterInterface $routerTokenStorageInterface $tokenStorage)
  17.     {
  18.         $this->router $router;
  19.         $this->tokenStorage $tokenStorage;
  20.     }
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             KernelEvents::REQUEST => ['redirect'],
  28.         ];
  29.     }
  30.     public function redirect(GetResponseEvent $event)
  31.     {
  32.         if (null !== ($token $this->tokenStorage->getToken()) && ($user $token->getUser()) instanceof UserInterface) {
  33.             $request $event->getRequest();
  34.             $routeName $request->get('_route');
  35.             if (null === $routeName) {
  36.                 return;
  37.             }
  38.             if ('hrm_welcome_index' === $routeName || === strpos($routeName'consent_') || === strpos($routeName'api_')) {
  39.                 return;
  40.             }
  41.             if ($user->isRequireConfirmEmail()) {
  42.                 $confirmEmailRoutes = [
  43.                     'resend_user_verification_code',
  44.                     'fos_user_registration_check_email',
  45.                     'validate_user_verification_code',
  46.                 ];
  47.                 if (!in_array($routeName$confirmEmailRoutes)) {
  48.                     if ($request->headers->has('X-AUTH-TOKEN')) {
  49.                         throw new AccountDisabledException('User email is not confirmed', [], 406);
  50.                     } else {
  51.                         $event->setResponse(new RedirectResponse($this->router->generate('fos_user_registration_check_email')));
  52.                     }
  53.                 }
  54.             }
  55.             if (null === $user->getConsent()) {
  56.                 $event->setResponse(new RedirectResponse($this->router->generate('consent_index')));
  57.             }
  58.             if (null === $user->getTimezone() || null === $user->getLocale() || null === $user->getTimeFormat()) {
  59.                 $event->setResponse(new RedirectResponse($this->router->generate('hrm_welcome_index'), 302));
  60.             }
  61.         }
  62.     }
  63. }