These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PragmaRX\Health\Support; |
||
4 | |||
5 | use JsonSerializable; |
||
6 | use Ramsey\Uuid\Uuid; |
||
7 | use Illuminate\Support\Collection; |
||
8 | use PragmaRX\Health\Support\Traits\ToArray; |
||
9 | use PragmaRX\Health\Events\RaiseHealthIssue; |
||
10 | use PragmaRX\Health\Support\Traits\ImportProperties; |
||
11 | |||
12 | class Resource implements JsonSerializable |
||
13 | { |
||
14 | use ToArray, ImportProperties; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | public $id; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | public $name; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | public $slug; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | public $abbreviation; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | public $isGlobal; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | public $errorMessage; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | public $style; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | public $notify; |
||
55 | |||
56 | /** |
||
57 | * @var Collection |
||
58 | */ |
||
59 | public $targets; |
||
60 | |||
61 | /** |
||
62 | * @var ResourceChecker |
||
63 | */ |
||
64 | public $checker; |
||
65 | |||
66 | /** |
||
67 | * @var Collection |
||
68 | */ |
||
69 | public $resources; |
||
70 | |||
71 | /** |
||
72 | * @var bool |
||
73 | */ |
||
74 | protected $notified; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $currentAction; |
||
80 | |||
81 | /** |
||
82 | * @var bool|null |
||
83 | */ |
||
84 | protected $graphEnabled = null; |
||
85 | |||
86 | /** |
||
87 | * Resource factory. |
||
88 | * |
||
89 | * @param Collection $data |
||
90 | * @return resource |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | 3 | public static function factory(Collection $data) |
|
94 | { |
||
95 | 3 | $instance = new static(); |
|
96 | |||
97 | 3 | $instance->id = (string) Uuid::uuid4(); |
|
98 | |||
99 | 3 | $instance->name = $data['name']; |
|
100 | |||
101 | 3 | $instance->slug = str_slug($data['name']); |
|
102 | |||
103 | 3 | $instance->graphEnabled = isset($data['graph_enabled']) |
|
104 | 3 | ? $data['graph_enabled'] |
|
105 | 3 | : null; |
|
106 | |||
107 | 3 | $instance->abbreviation = $data['abbreviation']; |
|
108 | |||
109 | 3 | $instance->targets = $instance->instantiateTargets( |
|
110 | 3 | $data['targets'] ?? collect() |
|
111 | ); |
||
112 | |||
113 | 3 | $instance->notify = |
|
114 | 3 | $data['notify'] ?? config('health.notifications.enabled'); |
|
115 | |||
116 | 3 | $instance->style = $instance->keysToCamel(config('health.style')); |
|
0 ignored issues
–
show
|
|||
117 | |||
118 | 3 | $instance->style['columnSize'] = |
|
119 | 3 | $data['column_size'] ?? $instance->style['columnSize']; |
|
120 | |||
121 | 3 | $instance->errorMessage = |
|
122 | 3 | $data['error_message'] ?? config('health.errors.message'); |
|
123 | |||
124 | 3 | $instance->isGlobal = $data['is_global'] ?? false; |
|
125 | |||
126 | 3 | $instance->checker = $instance->instantiateChecker($data['checker']); |
|
127 | |||
128 | 3 | $instance->importProperties($data); |
|
129 | |||
130 | 3 | return $instance; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Instantiate all checkers for a resource. |
||
135 | * |
||
136 | * @param Collection $targets |
||
137 | * @return Collection|\IlluminateAgnostic\Arr\Support\Collection|\IlluminateAgnostic\Collection\Support\Collection|\IlluminateAgnostic\Str\Support\Collection|mixed|\Tightenco\Collect\Support\Collection|\Vanilla\Support\Collection |
||
138 | */ |
||
139 | 3 | public function instantiateTargets(Collection $targets) |
|
140 | { |
||
141 | 3 | if ($targets->isEmpty()) { |
|
142 | 3 | return collect([Target::factory($this, $targets)]); |
|
143 | } |
||
144 | |||
145 | 3 | $current = collect(); |
|
146 | |||
147 | $targets = $targets |
||
148 | ->map(function (Collection $targetList) { |
||
149 | return $targetList->map(function ($target, $name) { |
||
150 | 3 | return Target::factory($this, $target, $name); |
|
151 | 3 | }); |
|
152 | 3 | }) |
|
153 | ->reduce(function ($current, $targetList) { |
||
154 | 3 | foreach ($targetList as $target) { |
|
155 | 3 | $current[] = $target; |
|
156 | } |
||
157 | |||
158 | 3 | return $current; |
|
159 | 3 | }, $current); |
|
160 | |||
161 | 3 | return $targets; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Instantiate one checker. |
||
166 | * |
||
167 | * @param string $checker |
||
168 | * @return object |
||
169 | */ |
||
170 | 3 | public function instantiateChecker(string $checker) |
|
171 | { |
||
172 | 3 | return instantiate($checker); |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Check all targets for a resource. |
||
177 | * |
||
178 | * @param string $action |
||
179 | * @return resource |
||
180 | */ |
||
181 | 1 | public function check($action = 'resource') |
|
182 | { |
||
183 | $this->setCurrentAction($action)->targets->each(function ( |
||
184 | Target $target |
||
185 | ) { |
||
186 | 1 | $target->check($target); |
|
187 | 1 | }); |
|
188 | |||
189 | 1 | $this->notify(); |
|
190 | |||
191 | 1 | return $this; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Check global resources. |
||
196 | * |
||
197 | * @param $resources |
||
198 | * @return resource |
||
199 | */ |
||
200 | public function checkGlobal($resources) |
||
201 | { |
||
202 | return $this->setResources($resources)->check(); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Check if is healthy. |
||
207 | * |
||
208 | * @return mixed |
||
209 | */ |
||
210 | 3 | public function isHealthy() |
|
211 | { |
||
212 | return $this->targets->reduce(function ($carry, $target) { |
||
213 | 3 | return $carry && $target->result->healthy; |
|
214 | 3 | }, true); |
|
215 | } |
||
216 | |||
217 | 3 | protected function keysToCamel($array) |
|
218 | { |
||
219 | return collect($array)->mapWithKeys(function ($item, $key) { |
||
220 | 3 | return [camel_case($key) => $item]; |
|
221 | 3 | }); |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * Notify about health problems. |
||
226 | */ |
||
227 | 1 | protected function notify() |
|
228 | { |
||
229 | 1 | if ($this->canNotify()) { |
|
230 | $this->sendNotifications(); |
||
231 | } |
||
232 | 1 | } |
|
233 | |||
234 | /** |
||
235 | * Send notifications. |
||
236 | * |
||
237 | * @return static |
||
238 | */ |
||
239 | protected function sendNotifications() |
||
240 | { |
||
241 | return collect(config('health.notifications.channels'))->each(function ( |
||
242 | $channel |
||
243 | ) { |
||
244 | try { |
||
245 | event(new RaiseHealthIssue($this, $channel)); |
||
246 | } catch (\Exception $exception) { |
||
247 | report($exception); |
||
248 | } |
||
249 | }); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Can we notify about errors on this resource? |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | 1 | protected function canNotify() |
|
258 | { |
||
259 | return |
||
260 | 1 | ! $this->notified && |
|
261 | 1 | $this->notificationsAreEnabled() && |
|
262 | 1 | ! $this->isHealthy(); |
|
263 | } |
||
264 | |||
265 | /** |
||
266 | * Is notification enabled for this resource? |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | 1 | protected function notificationsAreEnabled() |
|
271 | { |
||
272 | return |
||
273 | 1 | $this->notify && |
|
274 | 1 | config('health.notifications.enabled') && |
|
275 | 1 | config('health.notifications.notify_on.'.$this->currentAction); |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * Set current action. |
||
280 | * |
||
281 | * @param string $currentAction |
||
282 | * @return resource |
||
283 | */ |
||
284 | 1 | public function setCurrentAction(string $currentAction) |
|
285 | { |
||
286 | 1 | $this->currentAction = $currentAction; |
|
287 | |||
288 | 1 | return $this; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * Resources setter. |
||
293 | * |
||
294 | * @param $resources |
||
295 | * @return resource |
||
296 | */ |
||
297 | protected function setResources($resources) |
||
298 | { |
||
299 | $this->resources = $resources; |
||
300 | |||
301 | return $this; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Object to json. |
||
306 | * |
||
307 | * @return false|string |
||
308 | */ |
||
309 | 1 | public function __toString() |
|
310 | { |
||
311 | 1 | return json_encode($this->__toArray($this, 6)); |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Prepare the resource for JSON serialization. |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | 1 | public function jsonSerialize() |
|
320 | { |
||
321 | 1 | return json_decode($this->__toString(), true); |
|
322 | } |
||
323 | } |
||
324 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..