Total Complexity | 40 |
Total Lines | 485 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Call often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Call, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class Call implements JsonSerializable, Stringable |
||
34 | { |
||
35 | /** |
||
36 | * @var DialogManager |
||
37 | */ |
||
38 | protected $xDialogManager; |
||
39 | |||
40 | /** |
||
41 | * The name of the javascript function |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private $sFunction; |
||
46 | |||
47 | /** |
||
48 | * @var array<ParameterInterface> |
||
49 | */ |
||
50 | protected $aParameters = []; |
||
51 | |||
52 | /** |
||
53 | * Convert the parameter value to integer |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $bToInt = false; |
||
58 | |||
59 | /** |
||
60 | * The arguments of the else() calls |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $aMessage = []; |
||
65 | |||
66 | /** |
||
67 | * A condition to check before making the call |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $aCondition = []; |
||
72 | |||
73 | /** |
||
74 | * The arguments of the confirm() call |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $aConfirm = []; |
||
79 | |||
80 | /** |
||
81 | * The constructor. |
||
82 | * |
||
83 | * @param string $sFunction The javascript function |
||
84 | */ |
||
85 | public function __construct(string $sFunction) |
||
86 | { |
||
87 | $this->sFunction = $sFunction; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return Call |
||
92 | */ |
||
93 | public function toInt(): Call |
||
94 | { |
||
95 | $this->bToInt = true; |
||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Clear the parameter list associated with this request |
||
101 | * |
||
102 | * @return Call |
||
103 | */ |
||
104 | public function clearParameters(): Call |
||
105 | { |
||
106 | $this->aParameters = []; |
||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Set the value of the parameter at the given position |
||
112 | * |
||
113 | * @param ParameterInterface $xParameter The value to be used |
||
114 | * |
||
115 | * @return Call |
||
116 | */ |
||
117 | public function pushParameter(ParameterInterface $xParameter): Call |
||
118 | { |
||
119 | $this->aParameters[] = $xParameter; |
||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Add a parameter value to the parameter list for this request |
||
125 | * |
||
126 | * @param string $sType The type of the value to be used |
||
127 | * @param string $sValue The value to be used |
||
128 | * |
||
129 | * Types should be one of the following <Parameter::FORM_VALUES>, <Parameter::QUOTED_VALUE>, <Parameter::NUMERIC_VALUE>, |
||
130 | * <Parameter::JS_VALUE>, <Parameter::INPUT_VALUE>, <Parameter::CHECKED_VALUE>, <Parameter::PAGE_NUMBER>. |
||
131 | * The value should be as follows: |
||
132 | * - <Parameter::FORM_VALUES> - Use the ID of the form you want to process. |
||
133 | * - <Parameter::QUOTED_VALUE> - The string data to be passed. |
||
134 | * - <Parameter::JS_VALUE> - A string containing valid javascript |
||
135 | * (either a javascript variable name that will be in scope at the time of the call or |
||
136 | * a javascript function call whose return value will become the parameter). |
||
137 | * |
||
138 | * @return Call |
||
139 | */ |
||
140 | public function addParameter(string $sType, string $sValue): Call |
||
141 | { |
||
142 | $this->pushParameter(new Parameter($sType, $sValue)); |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Add a set of parameters to this request |
||
148 | * |
||
149 | * @param array $aParameters The parameters |
||
150 | * |
||
151 | * @return Call |
||
152 | */ |
||
153 | public function addParameters(array $aParameters): Call |
||
154 | { |
||
155 | foreach($aParameters as $xParameter) |
||
156 | { |
||
157 | $this->pushParameter(Parameter::make($xParameter)); |
||
158 | } |
||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param array $aArgs |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | private function getArgs(array $aArgs): array |
||
168 | { |
||
169 | array_shift($aArgs); |
||
170 | return $aArgs; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param DialogManager $xDialogManager |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | public function setDialogManager(DialogManager $xDialogManager) |
||
179 | { |
||
180 | $this->xDialogManager = $xDialogManager; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Show a message if the condition to the call is not met |
||
185 | * |
||
186 | * @param string $sMessage The message to show |
||
187 | * |
||
188 | * @return Call |
||
189 | */ |
||
190 | public function elseShow(string $sMessage): Call |
||
191 | { |
||
192 | $this->aMessage = $this->xDialogManager->warning($sMessage, $this->getArgs(func_get_args())); |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Show an information message if the condition to the call is not met |
||
198 | * |
||
199 | * @param string $sMessage The message to show |
||
200 | * |
||
201 | * @return Call |
||
202 | */ |
||
203 | public function elseInfo(string $sMessage): Call |
||
204 | { |
||
205 | $this->aMessage = $this->xDialogManager->info($sMessage, $this->getArgs(func_get_args())); |
||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Show a success message if the condition to the call is not met |
||
211 | * |
||
212 | * @param string $sMessage The message to show |
||
213 | * |
||
214 | * @return Call |
||
215 | */ |
||
216 | public function elseSuccess(string $sMessage): Call |
||
217 | { |
||
218 | $this->aMessage = $this->xDialogManager->success($sMessage, $this->getArgs(func_get_args())); |
||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Show a warning message if the condition to the call is not met |
||
224 | * |
||
225 | * @param string $sMessage The message to show |
||
226 | * |
||
227 | * @return Call |
||
228 | */ |
||
229 | public function elseWarning(string $sMessage): Call |
||
230 | { |
||
231 | $this->aMessage = $this->xDialogManager->warning($sMessage, $this->getArgs(func_get_args())); |
||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Show an error message if the condition to the call is not met |
||
237 | * |
||
238 | * @param string $sMessage The message to show |
||
239 | * |
||
240 | * @return Call |
||
241 | */ |
||
242 | public function elseError(string $sMessage): Call |
||
243 | { |
||
244 | $this->aMessage = $this->xDialogManager->error($sMessage, $this->getArgs(func_get_args())); |
||
245 | return $this; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Add a confirmation question to the request |
||
250 | * |
||
251 | * @param string $sQuestion The question to ask |
||
252 | * |
||
253 | * @return Call |
||
254 | */ |
||
255 | public function confirm(string $sQuestion): Call |
||
256 | { |
||
257 | $this->aConfirm = $this->xDialogManager->confirm($sQuestion, $this->getArgs(func_get_args())); |
||
258 | return $this; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Check if a value is equal to another before sending the request |
||
263 | * |
||
264 | * @param mixed $xValue1 The first value to compare |
||
265 | * @param mixed $xValue2 The second value to compare |
||
266 | * |
||
267 | * @return Call |
||
268 | */ |
||
269 | public function ifeq($xValue1, $xValue2): Call |
||
270 | { |
||
271 | $this->aCondition = ['eq', Parameter::make($xValue1), Parameter::make($xValue2)]; |
||
272 | return $this; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Check if a value is equal to another before sending the request |
||
277 | * |
||
278 | * @param mixed $xValue1 The first value to compare |
||
279 | * @param mixed $xValue2 The second value to compare |
||
280 | * |
||
281 | * @return Call |
||
282 | */ |
||
283 | public function ifteq($xValue1, $xValue2): Call |
||
284 | { |
||
285 | $this->aCondition = ['teq', Parameter::make($xValue1), Parameter::make($xValue2)]; |
||
286 | return $this; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Check if a value is not equal to another before sending the request |
||
291 | * |
||
292 | * @param mixed $xValue1 The first value to compare |
||
293 | * @param mixed $xValue2 The second value to compare |
||
294 | * |
||
295 | * @return Call |
||
296 | */ |
||
297 | public function ifne($xValue1, $xValue2): Call |
||
298 | { |
||
299 | $this->aCondition = ['ne', Parameter::make($xValue1), Parameter::make($xValue2)]; |
||
300 | return $this; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Check if a value is not equal to another before sending the request |
||
305 | * |
||
306 | * @param mixed $xValue1 The first value to compare |
||
307 | * @param mixed $xValue2 The second value to compare |
||
308 | * |
||
309 | * @return Call |
||
310 | */ |
||
311 | public function ifnte($xValue1, $xValue2): Call |
||
312 | { |
||
313 | $this->aCondition = ['nte', Parameter::make($xValue1), Parameter::make($xValue2)]; |
||
314 | return $this; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Check if a value is greater than another before sending the request |
||
319 | * |
||
320 | * @param mixed $xValue1 The first value to compare |
||
321 | * @param mixed $xValue2 The second value to compare |
||
322 | * |
||
323 | * @return Call |
||
324 | */ |
||
325 | public function ifgt($xValue1, $xValue2): Call |
||
326 | { |
||
327 | $this->aCondition = ['gt', Parameter::make($xValue1), Parameter::make($xValue2)]; |
||
328 | return $this; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Check if a value is greater or equal to another before sending the request |
||
333 | * |
||
334 | * @param mixed $xValue1 The first value to compare |
||
335 | * @param mixed $xValue2 The second value to compare |
||
336 | * |
||
337 | * @return Call |
||
338 | */ |
||
339 | public function ifge($xValue1, $xValue2): Call |
||
340 | { |
||
341 | $this->aCondition = ['ge', Parameter::make($xValue1), Parameter::make($xValue2)]; |
||
342 | return $this; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Check if a value is lower than another before sending the request |
||
347 | * |
||
348 | * @param mixed $xValue1 The first value to compare |
||
349 | * @param mixed $xValue2 The second value to compare |
||
350 | * |
||
351 | * @return Call |
||
352 | */ |
||
353 | public function iflt($xValue1, $xValue2): Call |
||
354 | { |
||
355 | $this->aCondition = ['lt', Parameter::make($xValue1), Parameter::make($xValue2)]; |
||
356 | return $this; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Check if a value is lower or equal to another before sending the request |
||
361 | * |
||
362 | * @param mixed $xValue1 The first value to compare |
||
363 | * @param mixed $xValue2 The second value to compare |
||
364 | * |
||
365 | * @return Call |
||
366 | */ |
||
367 | public function ifle($xValue1, $xValue2): Call |
||
368 | { |
||
369 | $this->aCondition = ['le', Parameter::make($xValue1), Parameter::make($xValue2)]; |
||
370 | return $this; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Add a condition to the request |
||
375 | * |
||
376 | * The request is sent only if the condition is true. |
||
377 | * |
||
378 | * @param mixed $xCondition The condition to check |
||
379 | * |
||
380 | * @return Call |
||
381 | */ |
||
382 | public function when($xCondition): Call |
||
383 | { |
||
384 | return $this->ifeq(true, $xCondition); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Add a condition to the request |
||
389 | * |
||
390 | * The request is sent only if the condition is false. |
||
391 | * |
||
392 | * @param mixed $xCondition The condition to check |
||
393 | * |
||
394 | * @return Call |
||
395 | */ |
||
396 | public function unless($xCondition): Call |
||
397 | { |
||
398 | return $this->ifeq(false, $xCondition); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Check if the request has a parameter of type Parameter::PAGE_NUMBER |
||
403 | * |
||
404 | * @return ParameterInterface|null |
||
405 | */ |
||
406 | private function findPageNumber(): ?ParameterInterface |
||
407 | { |
||
408 | foreach($this->aParameters as $xParameter) |
||
409 | { |
||
410 | if($xParameter->getType() === Parameter::PAGE_NUMBER) |
||
411 | { |
||
412 | return $xParameter; |
||
413 | } |
||
414 | } |
||
415 | return null; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Check if the request has a parameter of type Parameter::PAGE_NUMBER |
||
420 | * |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function hasPageNumber(): bool |
||
424 | { |
||
425 | return $this->findPageNumber() !== null; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Set a value to the Parameter::PAGE_NUMBER parameter |
||
430 | * |
||
431 | * @param integer $nPageNumber The current page number |
||
432 | * |
||
433 | * @return Call |
||
434 | */ |
||
435 | public function setPageNumber(int $nPageNumber): Call |
||
436 | { |
||
437 | /** @var Parameter */ |
||
438 | $xParameter = $this->findPageNumber(); |
||
439 | if($xParameter !== null) |
||
440 | { |
||
441 | $xParameter->setValue($nPageNumber); |
||
442 | } |
||
443 | return $this; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Convert this call to array |
||
448 | * |
||
449 | * @return array |
||
450 | */ |
||
451 | public function toArray(): array |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Convert this call to array, when converting the response into json. |
||
488 | * |
||
489 | * @return array |
||
490 | */ |
||
491 | public function jsonSerialize(): array |
||
492 | { |
||
493 | return $this->toArray(); |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Returns a call to jaxon as a string |
||
498 | * |
||
499 | * @return string |
||
500 | */ |
||
501 | public function __toString(): string |
||
502 | { |
||
503 | return 'jaxon.exec(' . json_encode($this->toArray()) . ')'; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Returns the js code of the call |
||
508 | * |
||
509 | * @return string |
||
510 | */ |
||
511 | public function raw(): string |
||
518 | } |
||
519 | } |
||
520 |