<?php
namespace App\Service\Airbrake;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class EventSubscriber implements EventSubscriberInterface
{
/**
* @var Notifier
*/
protected $notifier;
/**
* @var array
*/
protected $ignoredExceptions;
/**
* @param Notifier $notifier
* @param array $ignoredExceptions
*/
public function __construct(Notifier $notifier, array $ignoredExceptions = [])
{
$this->notifier = $notifier;
$this->ignoredExceptions = $ignoredExceptions;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => ['onKernelException', -128],
ConsoleEvents::ERROR => ['onConsoleError', -128],
//ConsoleEvents::TERMINATE => ['onConsoleTerminate', -128],
];
}
/**
* @param ExceptionEvent $event
*/
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
foreach ($this->ignoredExceptions as $ignoredException) {
if ($exception instanceof $ignoredException) {
return;
}
}
// SPECIAL CASE : IGNORE missing files IN STAGING only
$message = $exception->getMessage();
if ((false !== stripos($message, 'hosts/staging')) && (false !== stripos($message, '/ed/')) && (false !== stripos($message, 'does not'))) {
return;
}
$this->notifier->notify($exception);
}
/**
* @param ConsoleErrorEvent $event
*/
public function onConsoleError(ConsoleErrorEvent $event)
{
$error = $event->getError();
$this->notifier->notify($error);
}
}