Total Complexity | 65 |
Total Lines | 566 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like DialogPlugin 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 DialogPlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class DialogPlugin extends ResponsePlugin implements ModalInterface, MessageInterface, QuestionInterface |
||
31 | { |
||
32 | /** |
||
33 | * @const The plugin name |
||
34 | */ |
||
35 | const NAME = 'dialog'; |
||
36 | |||
37 | /** |
||
38 | * Dependency Injection manager |
||
39 | * |
||
40 | * @var Container |
||
41 | */ |
||
42 | protected $di; |
||
43 | |||
44 | /** |
||
45 | * @var DialogFacade |
||
46 | */ |
||
47 | protected $xDialogFacade; |
||
48 | |||
49 | /** |
||
50 | * @var ConfigManager |
||
51 | */ |
||
52 | protected $xConfigManager; |
||
53 | |||
54 | /** |
||
55 | * The Jaxon template engine |
||
56 | * |
||
57 | * @var TemplateEngine |
||
58 | */ |
||
59 | protected $xTemplateEngine; |
||
60 | |||
61 | /** |
||
62 | * Javascript dialog library adapters |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $aLibraries = array( |
||
67 | // Bootbox |
||
68 | 'bootbox' => Libraries\Bootbox\Plugin::class, |
||
69 | // Bootstrap |
||
70 | 'bootstrap' => Libraries\Bootstrap\Plugin::class, |
||
71 | // PgwJS |
||
72 | 'pgwjs' => Libraries\PgwJS\Plugin::class, |
||
73 | // Toastr |
||
74 | 'toastr' => Libraries\Toastr\Plugin::class, |
||
75 | // JAlert |
||
76 | 'jalert' => Libraries\JAlert\Plugin::class, |
||
77 | // Tingle |
||
78 | 'tingle' => Libraries\Tingle\Plugin::class, |
||
79 | // SimplyToast |
||
80 | 'simply' => Libraries\SimplyToast\Plugin::class, |
||
81 | // Noty |
||
82 | 'noty' => Libraries\Noty\Plugin::class, |
||
83 | // Notify |
||
84 | 'notify' => Libraries\Notify\Plugin::class, |
||
85 | // Lobibox |
||
86 | 'lobibox' => Libraries\Lobibox\Plugin::class, |
||
87 | // Overhang |
||
88 | 'overhang' => Libraries\Overhang\Plugin::class, |
||
89 | // PNotify |
||
90 | 'pnotify' => Libraries\PNotify\Plugin::class, |
||
91 | // SweetAlert |
||
92 | 'sweetalert' => Libraries\SweetAlert\Plugin::class, |
||
93 | // JQuery Confirm |
||
94 | 'jconfirm' => Libraries\JQueryConfirm\Plugin::class, |
||
95 | // YmzBox |
||
96 | 'ymzbox' => Libraries\YmzBox\Plugin::class, |
||
97 | ); |
||
98 | |||
99 | /** |
||
100 | * The name of the library to use for modals |
||
101 | * |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $sModalLibrary = null; |
||
105 | |||
106 | /** |
||
107 | * The name of the library to use for messages |
||
108 | * |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $sMessageLibrary = null; |
||
112 | |||
113 | /** |
||
114 | * The name of the library to use for question |
||
115 | * |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $sQuestionLibrary = null; |
||
119 | |||
120 | /** |
||
121 | * The constructor |
||
122 | * |
||
123 | * @param Container $di |
||
124 | * @param ConfigManager $xConfigManager |
||
125 | * @param TemplateEngine $xTemplateEngine The template engine |
||
126 | * @param DialogFacade $xDialogFacade |
||
127 | */ |
||
128 | public function __construct(Container $di, ConfigManager $xConfigManager, |
||
129 | TemplateEngine $xTemplateEngine, DialogFacade $xDialogFacade) |
||
130 | { |
||
131 | $this->xDialogFacade = $xDialogFacade; |
||
132 | $this->di = $di; |
||
133 | $this->xConfigManager = $xConfigManager; |
||
134 | $this->xTemplateEngine = $xTemplateEngine; |
||
135 | |||
136 | // Register the template dir into the template renderer |
||
137 | $xTemplateEngine->addNamespace('jaxon::dialogs', dirname(__DIR__) . '/templates'); |
||
138 | |||
139 | $this->registerLibraries(); |
||
140 | $this->registerClasses(); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @inheritDoc |
||
145 | */ |
||
146 | public function getName(): string |
||
147 | { |
||
148 | return self::NAME; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Get the value of a config option |
||
153 | * |
||
154 | * @param string $sName The option name |
||
155 | * @param mixed $xDefault The default value, to be returned if the option is not defined |
||
156 | * |
||
157 | * @return mixed |
||
158 | */ |
||
159 | public function getOption(string $sName, $xDefault = null) |
||
160 | { |
||
161 | return $this->xConfigManager->getOption($sName, $xDefault); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Check the presence of a config option |
||
166 | * |
||
167 | * @param string $sName The option name |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function hasOption(string $sName): bool |
||
172 | { |
||
173 | return $this->xConfigManager->hasOption($sName); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get the names of the options matching a given prefix |
||
178 | * |
||
179 | * @param string $sPrefix The prefix to match |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | public function getOptionNames(string $sPrefix): array |
||
184 | { |
||
185 | return $this->xConfigManager->getOptionNames($sPrefix); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Render a template |
||
190 | * |
||
191 | * @param string $sTemplate The name of template to be rendered |
||
192 | * @param array $aVars The template vars |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function render(string $sTemplate, array $aVars = []): string |
||
197 | { |
||
198 | return $this->xTemplateEngine->render($sTemplate, $aVars); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @inheritDoc |
||
203 | */ |
||
204 | public function getHash(): string |
||
205 | { |
||
206 | // The version number is used as hash |
||
207 | return '3.1.0'; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Register the javascript libraries adapters in the DI container. |
||
212 | * |
||
213 | * @param string $sName |
||
214 | * @param string $sClass |
||
215 | * |
||
216 | * @return void |
||
217 | */ |
||
218 | protected function registerLibrary(string $sName, string $sClass) |
||
219 | { |
||
220 | // Register the library in the DI container |
||
221 | $this->di->set($sName, function() use($sName, $sClass) { |
||
222 | $xLibrary = new $sClass; |
||
223 | $xLibrary->init($sName, $this); |
||
224 | return $xLibrary; |
||
225 | }); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Register the javascript libraries adapters in the DI container. |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | protected function registerLibraries() |
||
234 | { |
||
235 | // Register supported libraries in the DI container |
||
236 | foreach($this->aLibraries as $sName => $sClass) |
||
237 | { |
||
238 | $this->registerLibrary($sName, $sClass); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Register the javascript libraries adapters in the DI container. |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | protected function registerClasses() |
||
248 | { |
||
249 | // Register user defined libraries in the DI container |
||
250 | $aLibraries = $this->xConfigManager->getOptionNames('dialogs.classes'); |
||
251 | foreach($aLibraries as $sShortName => $sFullName) |
||
252 | { |
||
253 | $this->registerLibrary($sShortName, $this->xConfigManager->getOption($sFullName)); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Get a library adapter by its name. |
||
259 | * |
||
260 | * @param string $sName The name of the library adapter |
||
261 | * |
||
262 | * @return ModalInterface|MessageInterface|QuestionInterface |
||
263 | */ |
||
264 | public function getLibrary(string $sName) |
||
265 | { |
||
266 | try |
||
267 | { |
||
268 | return $this->di->g($sName); |
||
269 | } |
||
270 | catch(Exception $e) |
||
271 | { |
||
272 | return null; |
||
273 | } |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Set the library adapter to use for modals. |
||
278 | * |
||
279 | * @param string $sLibrary The name of the library adapter |
||
280 | * |
||
281 | * @return void |
||
282 | */ |
||
283 | public function setModalLibrary(string $sLibrary) |
||
284 | { |
||
285 | $this->sModalLibrary = $sLibrary; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Get the library adapter to use for modals. |
||
290 | * |
||
291 | * @return ModalInterface|null |
||
292 | */ |
||
293 | protected function getModalLibrary(): ?ModalInterface |
||
294 | { |
||
295 | // Get the current modal library |
||
296 | if(($this->sModalLibrary) && |
||
297 | ($library = $this->getLibrary($this->sModalLibrary)) && ($library instanceof ModalInterface)) |
||
298 | { |
||
299 | return $library; |
||
300 | } |
||
301 | // Get the default modal library |
||
302 | if(($sName = $this->xConfigManager->getOption('dialogs.default.modal', '')) && |
||
303 | ($library = $this->getLibrary($sName)) && ($library instanceof ModalInterface)) |
||
304 | { |
||
305 | return $library; |
||
306 | } |
||
307 | return null; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Set the library adapter to use for messages. |
||
312 | * |
||
313 | * @param string $sLibrary The name of the library adapter |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | public function setMessageLibrary(string $sLibrary) |
||
318 | { |
||
319 | $this->sMessageLibrary = $sLibrary; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Get the library adapter to use for messages. |
||
324 | * |
||
325 | * @param bool $bReturnDefault |
||
326 | * |
||
327 | * @return MessageInterface|null |
||
328 | */ |
||
329 | protected function getMessageLibrary(bool $bReturnDefault = false): ?MessageInterface |
||
330 | { |
||
331 | // Get the current message library |
||
332 | if(($this->sMessageLibrary) && |
||
333 | ($library = $this->getLibrary($this->sMessageLibrary)) && |
||
334 | ($library instanceof MessageInterface)) |
||
335 | { |
||
336 | return $library; |
||
337 | } |
||
338 | // Get the configured message library |
||
339 | if(($sName = $this->xConfigManager->getOption('dialogs.default.message', '')) && |
||
340 | ($library = $this->getLibrary($sName)) && ($library instanceof MessageInterface)) |
||
341 | { |
||
342 | return $library; |
||
343 | } |
||
344 | // Get the default message library |
||
345 | return ($bReturnDefault ? $this->xDialogFacade->getDefaultMessage() : null); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Set the library adapter to use for question. |
||
350 | * |
||
351 | * @param string $sLibrary The name of the library adapter |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | public function setQuestionLibrary(string $sLibrary) |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Get the library adapter to use for question. |
||
362 | * |
||
363 | * @param bool $bReturnDefault Return the default confirm if none is configured |
||
364 | * |
||
365 | * @return QuestionInterface|null |
||
366 | */ |
||
367 | protected function getQuestionLibrary(bool $bReturnDefault = false): ?QuestionInterface |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Get the list of library adapters that are present in the configuration. |
||
388 | * |
||
389 | * @return array |
||
390 | */ |
||
391 | protected function getLibrariesInUse(): array |
||
392 | { |
||
393 | $aNames = $this->xConfigManager->getOption('dialogs.libraries', []); |
||
394 | if(!is_array($aNames)) |
||
395 | { |
||
396 | $aNames = []; |
||
397 | } |
||
398 | $libraries = []; |
||
399 | foreach($aNames as $sName) |
||
400 | { |
||
401 | if(($library = $this->getLibrary($sName))) |
||
402 | { |
||
403 | $libraries[$library->getName()] = $library; |
||
404 | } |
||
405 | } |
||
406 | if(($library = $this->getModalLibrary())) |
||
407 | { |
||
408 | $libraries[$library->getName()] = $library; |
||
409 | } |
||
410 | if(($library = $this->getMessageLibrary())) |
||
411 | { |
||
412 | $libraries[$library->getName()] = $library; |
||
413 | } |
||
414 | if(($library = $this->getQuestionLibrary())) |
||
415 | { |
||
416 | $libraries[$library->getName()] = $library; |
||
417 | } |
||
418 | return $libraries; |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @inheritDoc |
||
423 | */ |
||
424 | public function getJs(): string |
||
425 | { |
||
426 | $libraries = $this->getLibrariesInUse(); |
||
427 | $code = ''; |
||
428 | foreach($libraries as $library) |
||
429 | { |
||
430 | $code .= "\n" . $library->getJs() . "\n"; |
||
431 | } |
||
432 | return $code; |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @inheritDoc |
||
437 | */ |
||
438 | public function getCss(): string |
||
439 | { |
||
440 | $libraries = $this->getLibrariesInUse(); |
||
441 | $code = ''; |
||
442 | foreach($libraries as $library) |
||
443 | { |
||
444 | $code .= $library->getCss() . "\n"; |
||
445 | } |
||
446 | return $code; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @inheritDoc |
||
451 | */ |
||
452 | public function getScript(): string |
||
453 | { |
||
454 | $libraries = $this->getLibrariesInUse(); |
||
455 | $code = "jaxon.dialogs = {};\n"; |
||
456 | foreach($libraries as $library) |
||
457 | { |
||
458 | $code .= $library->getScript() . "\n"; |
||
459 | } |
||
460 | return $code; |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @inheritDoc |
||
465 | */ |
||
466 | public function getReadyScript(): string |
||
467 | { |
||
468 | $libraries = $this->getLibrariesInUse(); |
||
469 | $code = ""; |
||
470 | foreach($libraries as $library) |
||
471 | { |
||
472 | $code .= $library->getReadyScript() . "\n"; |
||
473 | } |
||
474 | return $code; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Show a modal dialog. |
||
479 | * |
||
480 | * It is a function of the Jaxon\Dialogs\Contracts\ModalInterface interface. |
||
481 | * |
||
482 | * @param string $sTitle The title of the dialog |
||
483 | * @param string $sContent The content of the dialog |
||
484 | * @param array $aButtons The buttons of the dialog |
||
485 | * @param array $aOptions The options of the dialog |
||
486 | * |
||
487 | * Each button is an array containin the following entries: |
||
488 | * - title: the text to be printed in the button |
||
489 | * - class: the CSS class of the button |
||
490 | * - click: the javascript function to be called when the button is clicked |
||
491 | * If the click value is set to "close", then the buttons closes the dialog. |
||
492 | * |
||
493 | * The content of the $aOptions depends on the javascript library in use. |
||
494 | * Check their specific documentation for more information. |
||
495 | * |
||
496 | * @return void |
||
497 | */ |
||
498 | public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = []) |
||
499 | { |
||
500 | $this->getModalLibrary()->show($sTitle, $sContent, $aButtons, $aOptions); |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Show a modal dialog. |
||
505 | * |
||
506 | * It is another name for the show() function. |
||
507 | * |
||
508 | * @param string $sTitle The title of the dialog |
||
509 | * @param string $sContent The content of the dialog |
||
510 | * @param array $aButtons The buttons of the dialog |
||
511 | * @param array $aOptions The options of the dialog |
||
512 | * |
||
513 | * @return void |
||
514 | */ |
||
515 | public function modal(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = []) |
||
516 | { |
||
517 | $this->show($sTitle, $sContent, $aButtons, $aOptions); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Hide the modal dialog. |
||
522 | * |
||
523 | * It is a function of the Jaxon\Dialogs\Contracts\ModalInterface interface. |
||
524 | * |
||
525 | * @return void |
||
526 | */ |
||
527 | public function hide() |
||
528 | { |
||
529 | $this->getModalLibrary()->hide(); |
||
530 | } |
||
531 | |||
532 | /** |
||
533 | * Set the library to return the javascript code or run it in the browser. |
||
534 | * |
||
535 | * It is a function of the Jaxon\Contracts\Dialogs\Message interface. |
||
536 | * |
||
537 | * @param boolean $bReturn Whether to return the code |
||
538 | * |
||
539 | * @return void |
||
540 | */ |
||
541 | public function setReturn(bool $bReturn) |
||
542 | { |
||
543 | $this->getMessageLibrary(true)->setReturn($bReturn); |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * Check if the library should return the js code or run it in the browser. |
||
548 | * |
||
549 | * It is a function of the Jaxon\Contracts\Dialogs\Message interface. |
||
550 | * |
||
551 | * @return bool |
||
552 | */ |
||
553 | public function getReturn(): bool |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * @inheritDoc |
||
560 | */ |
||
561 | public function success(string $sMessage, string $sTitle = ''): string |
||
562 | { |
||
563 | return $this->getMessageLibrary(true)->success($sMessage, $sTitle); |
||
564 | } |
||
565 | |||
566 | /** |
||
567 | * @inheritDoc |
||
568 | */ |
||
569 | public function info(string $sMessage, string $sTitle = ''): string |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * @inheritDoc |
||
576 | */ |
||
577 | public function warning(string $sMessage, string $sTitle = ''): string |
||
578 | { |
||
579 | return $this->getMessageLibrary(true)->warning($sMessage, $sTitle); |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * @inheritDoc |
||
584 | */ |
||
585 | public function error(string $sMessage, string $sTitle = ''): string |
||
586 | { |
||
587 | return $this->getMessageLibrary(true)->error($sMessage, $sTitle); |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * @inheritDoc |
||
592 | */ |
||
593 | public function confirm(string $sQuestion, string $sYesScript, string $sNoScript): string |
||
596 | } |
||
597 | } |
||
598 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths