<?php
namespace App\EventListener;
use App\Service\SlackNotify;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ExceptionListener
{
private $notify;
/**
* ExceptionListener constructor.
*
* @param SlackNotify $notify
*/
public function __construct(SlackNotify $notify)
{
$this->notify = $notify;
}
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
$path = $event->getRequest()->getPathInfo();
// Nie wysyła notyfikacji, jeżeli błąd to not found i adres strony nie zawiera wyrazu "api"
if ( !($exception instanceof NotFoundHttpException && strpos($path, 'api') === false ) ) {
$this->notify->send($exception);
}
// if ( $exception instanceof \PDOException ) {
// $event->setResponse( new RedirectResponse('/admin/extra') );
// dump($event); die;
// }
}
}