1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace SM\AirbrakeBundle\Listener; |
6
|
|
|
|
7
|
|
|
use SM\AirbrakeBundle\Service\AirbrakeService; |
8
|
|
|
use Symfony\Component\Console\Event\ConsoleExceptionEvent; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Listens for exceptions and acts on them if the |
13
|
|
|
* Airbrake parameters have been provided. |
14
|
|
|
* |
15
|
|
|
* @package SM\AirbrakeBundle\Listener |
16
|
|
|
* @author Petre Pătrașc <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class AirbrakeExceptionListener |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Whether or not the listener is enabled. |
22
|
|
|
* |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $listenerEnabled; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var AirbrakeService |
29
|
|
|
*/ |
30
|
|
|
protected $airbrakeService; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* AirbrakeExceptionListener constructor. |
34
|
|
|
* |
35
|
|
|
* @param bool $listenerEnabled |
36
|
|
|
* @param AirbrakeService $airbrakeService |
37
|
|
|
*/ |
38
|
4 |
|
public function __construct($listenerEnabled, AirbrakeService $airbrakeService) |
39
|
|
|
{ |
40
|
4 |
|
$this->listenerEnabled = $listenerEnabled; |
41
|
4 |
|
$this->airbrakeService = $airbrakeService; |
42
|
4 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Handled on a kernel exceptions. |
46
|
|
|
* |
47
|
|
|
* @param GetResponseForExceptionEvent $exceptionEvent |
48
|
|
|
*/ |
49
|
2 |
|
public function onKernelException(GetResponseForExceptionEvent $exceptionEvent) |
50
|
|
|
{ |
51
|
2 |
|
$this->handleNotificationToAirbreakIfApplicable($exceptionEvent->getException()); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Handled on console exceptions. |
56
|
|
|
* |
57
|
|
|
* @param ConsoleExceptionEvent $exceptionEvent |
58
|
|
|
*/ |
59
|
2 |
|
public function onConsoleException(ConsoleExceptionEvent $exceptionEvent) |
60
|
|
|
{ |
61
|
2 |
|
$this->handleNotificationToAirbreakIfApplicable($exceptionEvent->getException()); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Unify the workflows for kernel and console exceptions. |
66
|
|
|
* |
67
|
|
|
* @param \Exception $exception |
68
|
|
|
*/ |
69
|
4 |
|
protected function handleNotificationToAirbreakIfApplicable($exception) |
70
|
|
|
{ |
71
|
4 |
|
if (true === $this->listenerEnabled) { |
72
|
2 |
|
$this->airbrakeService->notify($exception); |
73
|
|
|
} |
74
|
4 |
|
} |
75
|
|
|
} |
76
|
|
|
|