1 | <?php |
||
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')); |
|
|
|||
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 | * @return resource |
||
179 | */ |
||
180 | 1 | public function check() |
|
181 | { |
||
182 | $this->targets->each(function (Target $target) { |
||
183 | 1 | $target->check($target); |
|
184 | 1 | }); |
|
185 | |||
186 | 1 | $this->notify(); |
|
187 | |||
188 | 1 | return $this; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * Check global resources. |
||
193 | * |
||
194 | * @param $resources |
||
195 | * @return resource |
||
196 | */ |
||
197 | public function checkGlobal($resources) |
||
201 | |||
202 | /** |
||
203 | * Check if is healthy. |
||
204 | * |
||
205 | * @return mixed |
||
206 | */ |
||
207 | 4 | public function isHealthy() |
|
208 | { |
||
209 | return $this->targets->reduce(function ($carry, $target) { |
||
210 | 4 | return $carry && $target->result->healthy; |
|
211 | 4 | }, true); |
|
212 | } |
||
213 | |||
214 | 3 | protected function keysToCamel($array) |
|
215 | { |
||
216 | return collect($array)->mapWithKeys(function ($item, $key) { |
||
217 | 3 | return [camel_case($key) => $item]; |
|
218 | 3 | }); |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * Notify about health problems. |
||
223 | */ |
||
224 | 1 | protected function notify() |
|
225 | { |
||
226 | 1 | if ($this->canNotify()) { |
|
227 | $this->sendNotifications(); |
||
228 | } |
||
229 | 1 | } |
|
230 | |||
231 | /** |
||
232 | * Send notifications. |
||
233 | * |
||
234 | * @return static |
||
235 | */ |
||
236 | protected function sendNotifications() |
||
249 | |||
250 | /** |
||
251 | * Can we notify about errors on this resource? |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 1 | protected function canNotify() |
|
256 | { |
||
257 | return ( |
||
258 | 1 | !$this->notified && |
|
259 | 1 | $this->notificationsAreEnabled() && |
|
260 | 1 | !$this->isHealthy() |
|
261 | ); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Is notification enabled for this resource? |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | 1 | protected function notificationsAreEnabled() |
|
270 | { |
||
271 | return ( |
||
272 | 1 | $this->notify && |
|
273 | 1 | config('health.notifications.enabled') && |
|
274 | 1 | config('health.notifications.notify_on.' . $this->currentAction) |
|
275 | ); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Resources setter. |
||
280 | * |
||
281 | * @param $resources |
||
282 | * @return resource |
||
283 | */ |
||
284 | protected function setResources($resources) |
||
290 | |||
291 | /** |
||
292 | * Object to json. |
||
293 | * |
||
294 | * @return false|string |
||
295 | */ |
||
296 | 1 | public function __toString() |
|
297 | { |
||
298 | 1 | return json_encode($this->__toArray($this, 6)); |
|
299 | } |
||
300 | |||
301 | /** |
||
302 | * Prepare the resource for JSON serialization. |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | 1 | public function jsonSerialize() |
|
310 | } |
||
311 |
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..