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 PragmaRX\Health\Support\Traits\ToArray; |
||
8 | use Illuminate\Database\Eloquent\Collection; |
||
9 | use PragmaRX\Health\Support\Traits\ImportProperties; |
||
10 | |||
11 | class Target implements JsonSerializable |
||
12 | { |
||
13 | use ToArray, ImportProperties; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | public $id; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | public $name; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | public $display; |
||
29 | |||
30 | /** |
||
31 | * @var resource |
||
32 | */ |
||
33 | public $resource; |
||
34 | |||
35 | /** |
||
36 | * @var Result |
||
37 | */ |
||
38 | public $result; |
||
39 | |||
40 | /** |
||
41 | * @var Result |
||
42 | */ |
||
43 | public $value; |
||
44 | |||
45 | /** |
||
46 | * @var Collection |
||
47 | */ |
||
48 | public $checks; |
||
49 | |||
50 | /** |
||
51 | * @param \Exception|\Throwable |
||
52 | */ |
||
53 | protected function exceptionResult($exception) |
||
54 | { |
||
55 | $this->result([ |
||
56 | 'healthy' => false, |
||
57 | 'exception_message' => $exception->getMessage(), |
||
58 | ]); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Target factory. |
||
63 | * |
||
64 | * @param $resource |
||
65 | * @param $data |
||
66 | * @return Target |
||
67 | */ |
||
68 | public static function factory($resource, $data, $name = null) |
||
69 | { |
||
70 | $instance = new static(); |
||
71 | |||
72 | $instance->id = (string) Uuid::uuid4(); |
||
73 | |||
74 | $instance->name = self::makeName($data, $name); |
||
75 | |||
76 | $instance->display = $instance->name; |
||
77 | |||
78 | $instance->resource = $resource; |
||
79 | |||
80 | $instance->importProperties($data); |
||
81 | |||
82 | return $instance; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Check a resource target. |
||
87 | * |
||
88 | * @return Target |
||
89 | */ |
||
90 | public function check() |
||
91 | { |
||
92 | try { |
||
93 | try { |
||
94 | $this->result( |
||
95 | $this->resource->checker->setTarget($this)->checkTarget( |
||
96 | $this |
||
97 | ) |
||
98 | ); |
||
99 | } catch (\Exception $exception) { |
||
100 | report($exception); |
||
101 | |||
102 | $this->exceptionResult($exception); |
||
103 | } |
||
104 | } catch (\Throwable $error) { |
||
105 | report($error); |
||
0 ignored issues
–
show
|
|||
106 | |||
107 | $this->exceptionResult($error); |
||
108 | } |
||
109 | |||
110 | $this->moveChecksBackToTarget(); |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Display getter. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getDisplay() |
||
121 | { |
||
122 | return $this->display; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getName() |
||
129 | { |
||
130 | return $this->name; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Result getter. |
||
135 | * |
||
136 | * @return Result |
||
137 | */ |
||
138 | public function getResult() |
||
139 | { |
||
140 | return $this->result; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Make target name. |
||
145 | * |
||
146 | * @param $data |
||
147 | * @param $name |
||
148 | * @return string |
||
149 | */ |
||
150 | protected static function makeName($data, $name) |
||
151 | { |
||
152 | return ( |
||
153 | ( |
||
154 | $data['name'] ?? |
||
155 | ($name !== 'default' ? $name : ($data['hostname'] ?? null)) |
||
156 | ) ?? 'default' |
||
157 | ); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Move checks to target object. |
||
162 | */ |
||
163 | protected function moveChecksBackToTarget() |
||
164 | { |
||
165 | if (isset($this->result->checks)) { |
||
166 | $this->checks = $this->result->checks; |
||
0 ignored issues
–
show
The property
checks does not seem to exist in PragmaRX\Health\Support\Result .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
167 | |||
168 | unset($this->result->checks); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Make a result. |
||
174 | * |
||
175 | * @param $result |
||
176 | * @return Result |
||
177 | */ |
||
178 | protected function result($result) |
||
179 | { |
||
180 | $this->result = |
||
181 | $result instanceof Result |
||
182 | ? $result |
||
183 | : new Result($result['healthy'], $result['exception_message']); |
||
184 | |||
185 | return $this->result; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get result error message. |
||
190 | * |
||
191 | * @return mixed |
||
192 | */ |
||
193 | public function getMessage() |
||
194 | { |
||
195 | return $this->result->healthy |
||
196 | ? $this->getSuccessMessage() |
||
197 | : $this->getErrorMessage(); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get result error message. |
||
202 | * |
||
203 | * @return mixed |
||
204 | */ |
||
205 | public function getErrorMessage() |
||
206 | { |
||
207 | return $this->result->errorMessage ?? $this->resource->errorMessage; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get result error message. |
||
212 | * |
||
213 | * @return mixed |
||
214 | */ |
||
215 | public function getSuccessMessage() |
||
216 | { |
||
217 | return config('health.action.success.message'); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Display setter. |
||
222 | * |
||
223 | * @param string $display |
||
224 | * @return Target |
||
225 | */ |
||
226 | public function setDisplay(string $display): self |
||
227 | { |
||
228 | $this->display = $display; |
||
229 | |||
230 | return $this; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param string $name |
||
235 | */ |
||
236 | public function setName(string $name): void |
||
237 | { |
||
238 | $this->name = $name; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Result setter. |
||
243 | * |
||
244 | * @param Result $result |
||
245 | * @return Target |
||
246 | */ |
||
247 | public function setResult(Result $result) |
||
248 | { |
||
249 | $this->result = $result; |
||
250 | |||
251 | return $this; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Object to json. |
||
256 | * |
||
257 | * @return false|string |
||
258 | */ |
||
259 | public function __toString() |
||
260 | { |
||
261 | return json_encode($this->__toArray($this, 6)); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Prepare the resource for JSON serialization. |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function jsonSerialize() |
||
270 | { |
||
271 | return json_decode($this->__toString(), true); |
||
272 | } |
||
273 | } |
||
274 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.