<?php
namespace App\Salesforce\EventListener;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
/**
* SalesForce Event listener for the kernel exception event
* This listener is here to avoid seeing an error 500 whenever SalesForce services are unavailable.
*
* @todo As of right now, this prevents an error 500, but is not making a natural transition
* Maybe needs some error handlers or something, but it has to be reworked
*
* @author Paul Kervella <pkervella@vpauto.fr>
*/
class SalesForceExceptionListener implements EventSubscriberInterface
{
/**
* Initialize values.
*/
public function __construct(private readonly RouterInterface $router)
{
}
public static function getSubscribedEvents()
{
// The default Symfony ExceptionListener has a priority order of -128, so -127 is enough to act before it
return [
KernelEvents::EXCEPTION => [['onSoapFault', -127]],
];
}
/**
* Exception handler for SoapFault
* This is the one thrown when an issue with SalesForce has occurred (unavailability, wrong login...).
*
* @return RedirectResponse
*/
public function onSoapFault(ExceptionEvent $evt): void
{
if ('SoapFault' === $evt->getThrowable()::class) {
$rq = $evt->getRequest();
if (stripos($rq->getRequestUri(), 'admin')) {
$rq->getSession()->getFlashBag()->add('error', 'Une erreur s\'est produite lors de la communication avec SalesForce. Merci de contacter l\'administrateur.');
$response = new RedirectResponse($this->router->generate('homepage'));
} elseif (stripos($rq->getRequestUri(), 'terminal')) {
if (!empty($rq->headers->get('referer'))) {
$response = new RedirectResponse($rq->headers->get('referer'));
} else {
$response = new RedirectResponse($rq->headers->get('vpauto_terminal_registration_homepage'));
}
} else {
$response = new RedirectResponse($this->router->generate('homepage'));
}
$evt->setResponse($response);
}
}
}