<?php
namespace App\EventListener;
use App\Event\GetResponseVacancyEvent;
use App\Security\ApplicationVoter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ApplicationListener
{
protected ApplicationVoter $applicationVoter;
protected AuthorizationCheckerInterface $authorizationChecker;
protected RouterInterface $router;
protected ParameterBagInterface $parameterBag;
/**
* ApplicationListener constructor.
*/
public function __construct(
ApplicationVoter $applicationVoter,
AuthorizationCheckerInterface $authorizationChecker,
RouterInterface $router,
ParameterBagInterface $parameterBag
) {
$this->applicationVoter = $applicationVoter;
$this->authorizationChecker = $authorizationChecker;
$this->router = $router;
$this->parameterBag = $parameterBag;
}
public function onVacancyApplicationInitialize(GetResponseVacancyEvent $event)
{
$vacancy = $event->getVacancy();
if (!$this->authorizationChecker->isGranted(ApplicationVoter::AUTHORIZED_APPLY, $vacancy)) {
$event->setResponse(new RedirectResponse($this->router->generate('vacancies')));
}
}
public function onVacancyApplicationCompleted(GetResponseVacancyEvent $event)
{
$vacancy = $event->getVacancy();
$request = $event->getRequest();
$referer = $request->headers->get('referer');
$refererPathInfo = parse_url($referer, \PHP_URL_PATH);
$refererQuery = parse_url($referer, \PHP_URL_QUERY);
try {
$route = $this->router->match($refererPathInfo);
} catch (\Exception) {
// 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
return;
}
if ((empty($route['_route']) || 'mobility_vacancy_detail' !== $route['_route']) && !str_contains($refererQuery, 'mobility')) {
// 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
return;
}
// At this point it is certain the refferal route is mobility_vacancy_detail and therefore know we must redirect to the mobility thanks page
$event->setResponse(new RedirectResponse(
$this->router->generate('mobility_apply_success', ['id' => $vacancy->getId()])
));
}
}