1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace SM\AirbrakeBundle\Builder; |
6
|
|
|
|
7
|
|
|
use Airbrake\Notifier; |
8
|
|
|
use SM\AirbrakeBundle\Enum\AirbrakeDefaultEnum; |
9
|
|
|
use SM\AirbrakeBundle\Exception\AirbrakeConfigurationException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Handles the construction of Airbrake notifiers. |
13
|
|
|
* |
14
|
|
|
* @package SM\AirbrakeBundle\Builder |
15
|
|
|
* @author Petre Pătrașc <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class NotifierBuilder implements BuilderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $projectKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $projectId; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $host; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $httpClient; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $rootDirectory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $ignoredExceptions; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $environment; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $appVersion; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* NotifierBuilder constructor. |
61
|
|
|
*/ |
62
|
20 |
|
public function __construct() |
63
|
|
|
{ |
64
|
20 |
|
$this->clear(); |
65
|
20 |
|
} |
66
|
|
|
|
67
|
8 |
|
public function withProjectKey(string $projectKey): NotifierBuilder |
68
|
|
|
{ |
69
|
8 |
|
if (empty(trim($projectKey))) { |
70
|
2 |
|
throw new AirbrakeConfigurationException('Project API key cannot be empty'); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
$this->projectKey = $projectKey; |
74
|
|
|
|
75
|
6 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
public function withProjectId(string $projectId): NotifierBuilder |
79
|
|
|
{ |
80
|
8 |
|
if (empty(trim($projectId))) { |
81
|
3 |
|
throw new AirbrakeConfigurationException('Project ID cannot be empty'); |
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
$this->projectId = $projectId; |
85
|
|
|
|
86
|
5 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
View Code Duplication |
public function withHost(string $host): NotifierBuilder |
|
|
|
|
90
|
|
|
{ |
91
|
7 |
|
if (empty(trim($host))) { |
92
|
2 |
|
throw new AirbrakeConfigurationException('Host cannot be empty'); |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
$this->host = $host; |
96
|
|
|
|
97
|
5 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
6 |
|
public function withHttpClient(string $httpClient): NotifierBuilder |
101
|
|
|
{ |
102
|
6 |
|
if (empty(trim($httpClient))) { |
103
|
2 |
|
throw new AirbrakeConfigurationException('HTTP client cannot be empty'); |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
$this->httpClient = $httpClient; |
107
|
|
|
|
108
|
4 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
7 |
|
public function withRootDirectory(string $rootDirectory): NotifierBuilder |
112
|
|
|
{ |
113
|
7 |
|
if (empty(trim($rootDirectory))) { |
114
|
2 |
|
throw new AirbrakeConfigurationException('Root directory cannot be empty'); |
115
|
|
|
} |
116
|
|
|
|
117
|
5 |
|
$this->rootDirectory = $rootDirectory; |
118
|
|
|
|
119
|
5 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
5 |
|
public function withIgnoredExceptions(array $ignoredExceptions = []): NotifierBuilder |
123
|
|
|
{ |
124
|
5 |
|
$this->ignoredExceptions = $ignoredExceptions; |
125
|
|
|
|
126
|
5 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
7 |
View Code Duplication |
public function withEnvironment(string $environment): NotifierBuilder |
|
|
|
|
130
|
|
|
{ |
131
|
7 |
|
if (empty(trim($environment))) { |
132
|
2 |
|
throw new AirbrakeConfigurationException('Environment cannot be empty'); |
133
|
|
|
} |
134
|
|
|
|
135
|
5 |
|
$this->environment = $environment; |
136
|
|
|
|
137
|
5 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
7 |
|
public function withAppVersion(string $appVersion): NotifierBuilder |
141
|
|
|
{ |
142
|
7 |
|
if (empty(trim($appVersion))) { |
143
|
2 |
|
throw new AirbrakeConfigurationException('App version cannot be empty'); |
144
|
|
|
} |
145
|
|
|
|
146
|
5 |
|
$this->appVersion = $appVersion; |
147
|
|
|
|
148
|
5 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
5 |
|
public function build(): Notifier |
155
|
|
|
{ |
156
|
5 |
|
$notifierInstance = new Notifier([ |
157
|
5 |
|
'projectId' => $this->projectId, |
158
|
5 |
|
'projectKey' => $this->projectKey, |
159
|
5 |
|
'host' => $this->host, |
160
|
5 |
|
'rootDirectory' => $this->rootDirectory, |
161
|
5 |
|
'httpClient' => $this->httpClient, |
162
|
5 |
|
'environment' => $this->environment, |
163
|
5 |
|
'appVersion' => $this->appVersion, |
164
|
|
|
]); |
165
|
|
|
|
166
|
5 |
|
$this->addFilteredExceptions($notifierInstance); |
167
|
5 |
|
$this->clear(); |
168
|
|
|
|
169
|
5 |
|
return $notifierInstance; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritDoc |
174
|
|
|
*/ |
175
|
20 |
|
public function clear() |
176
|
|
|
{ |
177
|
20 |
|
$this->projectKey = AirbrakeDefaultEnum::PROJECT_KEY; |
178
|
20 |
|
$this->projectId = AirbrakeDefaultEnum::PROJECT_ID; |
179
|
20 |
|
$this->host = AirbrakeDefaultEnum::HOST; |
180
|
20 |
|
$this->httpClient = AirbrakeDefaultEnum::HTTP_CLIENT; |
181
|
20 |
|
$this->ignoredExceptions = AirbrakeDefaultEnum::IGNORED_EXCEPTIONS; |
182
|
20 |
|
$this->rootDirectory = AirbrakeDefaultEnum::ROOT_DIRECTORY; |
183
|
20 |
|
$this->environment = AirbrakeDefaultEnum::ENVIRONMENT; |
184
|
20 |
|
$this->appVersion = AirbrakeDefaultEnum::APP_VERSION; |
185
|
20 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Add filtered exceptions to the notifier. |
189
|
|
|
* |
190
|
|
|
* @param Notifier $notifierInstance |
191
|
|
|
*/ |
192
|
5 |
|
protected function addFilteredExceptions(Notifier $notifierInstance) |
193
|
|
|
{ |
194
|
5 |
|
if (false === empty($this->ignoredExceptions)) { |
195
|
5 |
|
$ignoredExceptions = $this->ignoredExceptions; |
196
|
5 |
|
$notifierInstance->addFilter(function ($notice) use ($ignoredExceptions) { |
197
|
|
|
foreach ($ignoredExceptions as $exceptionClass) { |
198
|
|
|
if ($notice['errors'][0]['type'] === $exceptionClass) { |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $notice; |
204
|
5 |
|
}); |
205
|
|
|
} |
206
|
5 |
|
} |
207
|
|
|
} |
208
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.