src/EventListener/ApplicationListener.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Event\GetResponseVacancyEvent;
  4. use App\Security\ApplicationVoter;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. class ApplicationListener
  10. {
  11.     protected ApplicationVoter $applicationVoter;
  12.     protected AuthorizationCheckerInterface $authorizationChecker;
  13.     protected RouterInterface $router;
  14.     protected ParameterBagInterface $parameterBag;
  15.     /**
  16.      * ApplicationListener constructor.
  17.      */
  18.     public function __construct(
  19.         ApplicationVoter $applicationVoter,
  20.         AuthorizationCheckerInterface $authorizationChecker,
  21.         RouterInterface $router,
  22.         ParameterBagInterface $parameterBag
  23.     ) {
  24.         $this->applicationVoter $applicationVoter;
  25.         $this->authorizationChecker $authorizationChecker;
  26.         $this->router $router;
  27.         $this->parameterBag $parameterBag;
  28.     }
  29.     public function onVacancyApplicationInitialize(GetResponseVacancyEvent $event)
  30.     {
  31.         $vacancy $event->getVacancy();
  32.         if (!$this->authorizationChecker->isGranted(ApplicationVoter::AUTHORIZED_APPLY$vacancy)) {
  33.             $event->setResponse(new RedirectResponse($this->router->generate('vacancies')));
  34.         }
  35.     }
  36.     public function onVacancyApplicationCompleted(GetResponseVacancyEvent $event)
  37.     {
  38.         $vacancy $event->getVacancy();
  39.         $request $event->getRequest();
  40.         $referer $request->headers->get('referer');
  41.         $refererPathInfo parse_url($referer\PHP_URL_PATH);
  42.         $refererQuery parse_url($referer\PHP_URL_QUERY);
  43.         try {
  44.             $route $this->router->match($refererPathInfo);
  45.         } catch (\Exception) {
  46.             // Exception is thrown when route cannot be matched, in this case, default redirect response is sufficient, use return statement as the default action should be handled in the controller
  47.             return;
  48.         }
  49.         if ((empty($route['_route']) || 'mobility_vacancy_detail' !== $route['_route']) && !str_contains($refererQuery'mobility')) {
  50.             // When route is not named mobility_vacancy_detail we know for sure the application was done in a public environment and should as of this moment use the default response
  51.             return;
  52.         }
  53.         // At this point it is certain the refferal route is mobility_vacancy_detail and therefore know we must redirect to the mobility thanks page
  54.         $event->setResponse(new RedirectResponse(
  55.             $this->router->generate('mobility_apply_success', ['id' => $vacancy->getId()])
  56.         ));
  57.     }
  58. }