src/Form/EventListener/AppExceptionListener.php line 21

Open in your IDE?
  1. <?php 
  2. // src/Form/EventListener/AppExceptionListener.php
  3. namespace App\Form\EventListener;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  6. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\Intl\Timezones;
  9. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  10. use App\Service\ConfigDataService;
  11. class AppExceptionListener
  12. {
  13.     private $configDataService;
  14.     public function __construct(ConfigDataService $configDataService) {
  15.         $this->configDataService $configDataService;
  16.     }
  17.     public function onKernelException(ExceptionEvent $event)
  18.     {
  19.         $exception $event->getThrowable();
  20.        // dd($exception);
  21.         if ($exception instanceof ForeignKeyConstraintViolationException) {
  22.             $message sprintf(
  23.                 'Cannot delete this record, has relation with another record. The message says: %s with code: %s',
  24.                 $exception->getMessage(),
  25.                 $exception->getCode()
  26.             );
  27.              // Customize your response object to display the exception details
  28.             $response = new Response();
  29.             $response->setContent($message);
  30.             $event->setResponse($response);
  31.         }
  32.     }
  33.     public function onKernelRequest(RequestEvent $event) {
  34.         date_default_timezone_set($this->configDataService->get('timezone'));
  35.         $request $event->getRequest();
  36.         //$countrycode = Timezones::getCountryCode($this->configDataService->get('timezone'));
  37.         $countrycode $this->configDataService->get('country');
  38.        //dd($countrycode);
  39.         $request->setLocale($countrycode);
  40.     }
  41. }