1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace SM\AirbrakeBundle\Service; |
6
|
|
|
|
7
|
|
|
use Airbrake\ErrorHandler; |
8
|
|
|
use Airbrake\Instance; |
9
|
|
|
use Airbrake\Notifier; |
10
|
|
|
use SM\AirbrakeBundle\Builder\NotifierBuilder; |
11
|
|
|
use SM\AirbrakeBundle\Exception\AirbrakeConnectionException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Defines the Airbrake service wrapper. |
15
|
|
|
* |
16
|
|
|
* @package SM\AirbrakeBundle\Service |
17
|
|
|
* @author Petre Pătrașc <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class AirbrakeService |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Notifier that has been set up with configuration parameters |
23
|
|
|
* and filters. |
24
|
|
|
* |
25
|
|
|
* @var Notifier |
26
|
|
|
*/ |
27
|
|
|
protected $notifier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The global error and exception handler instance. |
31
|
|
|
* Defaults to null if the global_error_and_exception_handler |
32
|
|
|
* configuration is set to false. |
33
|
|
|
* |
34
|
|
|
* @var ErrorHandler |
35
|
|
|
*/ |
36
|
|
|
protected $errorHandler; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* AirbrakeService constructor. |
40
|
|
|
* |
41
|
|
|
* @param NotifierBuilder $notifierBuilder |
42
|
|
|
* @param string $projectId |
43
|
|
|
* @param string $projectKey |
44
|
|
|
* @param bool $globalExceptionInstance |
45
|
|
|
* @param bool $globalErrorAndExceptionHandler |
46
|
|
|
* @param string $host |
47
|
|
|
* @param string $httpClient |
48
|
|
|
* @param string $rootDirectory |
49
|
|
|
* @param array $ignoredExceptions |
50
|
|
|
* @param string $environment |
51
|
|
|
* @param string $appVersion |
52
|
|
|
*/ |
53
|
5 |
|
public function __construct( |
54
|
|
|
NotifierBuilder $notifierBuilder, |
55
|
|
|
string $projectId, |
56
|
|
|
string $projectKey, |
57
|
|
|
bool $globalExceptionInstance, |
58
|
|
|
bool $globalErrorAndExceptionHandler, |
59
|
|
|
string $host, |
60
|
|
|
string $httpClient, |
61
|
|
|
string $rootDirectory, |
62
|
|
|
array $ignoredExceptions, |
63
|
|
|
string $environment, |
64
|
|
|
string $appVersion |
65
|
|
|
) |
66
|
|
|
{ |
67
|
|
|
|
68
|
4 |
|
$this->notifier = $notifierBuilder |
69
|
5 |
|
->withProjectKey($projectKey) |
70
|
5 |
|
->withProjectId($projectId) |
71
|
4 |
|
->withHost($host) |
72
|
4 |
|
->withHttpClient($httpClient) |
73
|
4 |
|
->withIgnoredExceptions($ignoredExceptions) |
74
|
4 |
|
->withRootDirectory($rootDirectory) |
75
|
4 |
|
->withEnvironment($environment) |
76
|
4 |
|
->withAppVersion($appVersion) |
77
|
4 |
|
->build(); |
78
|
|
|
|
79
|
|
|
$this |
80
|
4 |
|
->withGlobalExceptionInstance($globalExceptionInstance) |
81
|
4 |
|
->withGlobalErrorAndExceptionHandler($globalErrorAndExceptionHandler); |
82
|
4 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Notify Airbrake of the exception. |
86
|
|
|
* |
87
|
|
|
* @param \Exception $exception |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
* @throws AirbrakeConnectionException |
91
|
|
|
*/ |
92
|
2 |
|
public function notify(\Exception $exception): bool |
93
|
|
|
{ |
94
|
2 |
|
$airbrakeResponse = $this->notifier->notify($exception); |
95
|
|
|
|
96
|
2 |
|
if (true === is_array($airbrakeResponse) && array_key_exists('id', $airbrakeResponse)) { |
97
|
1 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Notifier |
105
|
|
|
*/ |
106
|
1 |
|
public function getNotifier(): Notifier |
107
|
|
|
{ |
108
|
1 |
|
return $this->notifier; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return ErrorHandler|null |
113
|
|
|
*/ |
114
|
2 |
|
public function getErrorHandler() |
115
|
|
|
{ |
116
|
2 |
|
return $this->errorHandler; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Add a global exception instance if applicable. |
121
|
|
|
* |
122
|
|
|
* @param bool $globalExceptionInstance |
123
|
|
|
* |
124
|
|
|
* @return AirbrakeService |
125
|
|
|
*/ |
126
|
4 |
|
protected function withGlobalExceptionInstance(bool $globalExceptionInstance): AirbrakeService |
127
|
|
|
{ |
128
|
4 |
|
if (true === $globalExceptionInstance) { |
129
|
4 |
|
Instance::set($this->notifier); |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Add a global error and exception handler if applicable. |
137
|
|
|
* |
138
|
|
|
* @param bool $globalErrorAndExceptionHandler |
139
|
|
|
* |
140
|
|
|
* @return AirbrakeService |
141
|
|
|
*/ |
142
|
4 |
|
protected function withGlobalErrorAndExceptionHandler(bool $globalErrorAndExceptionHandler): AirbrakeService |
143
|
|
|
{ |
144
|
4 |
|
if (true === $globalErrorAndExceptionHandler) { |
145
|
1 |
|
$this->errorHandler = new ErrorHandler($this->notifier); |
146
|
1 |
|
$this->errorHandler->register(); |
147
|
|
|
} |
148
|
|
|
|
149
|
4 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|