Passed
Push — master ( eddd1e...93f15f )
by Thierry
09:22
created

DialogPlugin::hasOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * DialogPlugin.php - ModalInterface, message and question dialogs for Jaxon.
5
 *
6
 * Show modal, message and question dialogs with various javascript libraries
7
 * based on user settings.
8
 *
9
 * @package jaxon-dialogs
10
 * @author Thierry Feuzeu <[email protected]>
11
 * @copyright 2016 Thierry Feuzeu <[email protected]>
12
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
13
 * @link https://github.com/jaxon-php/jaxon-dialogs
14
 */
15
16
namespace Jaxon\Dialogs;
17
18
use Jaxon\Config\ConfigManager;
19
use Jaxon\Di\Container;
0 ignored issues
show
Bug introduced by
The type Jaxon\Di\Container was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Jaxon\Plugin\ResponsePlugin;
21
use Jaxon\Ui\Dialogs\DialogFacade;
0 ignored issues
show
Bug introduced by
The type Jaxon\Ui\Dialogs\DialogFacade was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Jaxon\Ui\Dialogs\MessageInterface;
23
use Jaxon\Ui\Dialogs\QuestionInterface;
24
use Jaxon\Utils\Template\TemplateEngine;
0 ignored issues
show
Bug introduced by
The type Jaxon\Utils\Template\TemplateEngine was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
use Exception;
27
28
use function dirname;
29
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);
0 ignored issues
show
Bug introduced by
The method getOptionNames() does not exist on Jaxon\Config\ConfigManager. Did you maybe mean getOption()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
        return $this->xConfigManager->/** @scrutinizer ignore-call */ getOptionNames($sPrefix);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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));
0 ignored issues
show
Bug introduced by
It seems like $this->xConfigManager->getOption($sFullName) can also be of type null; however, parameter $sClass of Jaxon\Dialogs\DialogPlugin::registerLibrary() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

253
            $this->registerLibrary($sShortName, /** @scrutinizer ignore-type */ $this->xConfigManager->getOption($sFullName));
Loading history...
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)
356
    {
357
        $this->sQuestionLibrary = $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
368
    {
369
        // Get the current confirm library
370
        if(($this->sQuestionLibrary) &&
371
            ($library = $this->getLibrary($this->sQuestionLibrary)) &&
372
            ($library instanceof QuestionInterface))
373
        {
374
            return $library;
375
        }
376
        // Get the configured confirm library
377
        if(($sName = $this->xConfigManager->getOption('dialogs.default.question', '')) &&
378
            ($library = $this->getLibrary($sName)) && ($library instanceof QuestionInterface))
379
        {
380
            return $library;
381
        }
382
        // Get the default confirm library
383
        return ($bReturnDefault ? $this->xDialogFacade->getDefaultQuestion() : null);
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;
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Jaxon\Dialogs\ModalInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jaxon\Dialogs\ModalInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

403
                $libraries[$library->/** @scrutinizer ignore-call */ getName()] = $library;
Loading history...
Bug introduced by
The method getName() does not exist on Jaxon\Ui\Dialogs\QuestionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Jaxon\Ui\Dialogs\Question. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

403
                $libraries[$library->/** @scrutinizer ignore-call */ getName()] = $library;
Loading history...
Bug introduced by
The method getName() does not exist on Jaxon\Ui\Dialogs\MessageInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Jaxon\Ui\Dialogs\Message. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

403
                $libraries[$library->/** @scrutinizer ignore-call */ getName()] = $library;
Loading history...
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
554
    {
555
        return $this->getMessageLibrary(true)->getReturn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMessage...rary(true)->getReturn() returns the type void which is incompatible with the type-hinted return boolean.
Loading history...
Bug introduced by
Are you sure the usage of $this->getMessageLibrary(true)->getReturn() targeting Jaxon\Ui\Dialogs\MessageInterface::getReturn() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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
570
    {
571
        return $this->getMessageLibrary(true)->info($sMessage, $sTitle);
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
594
    {
595
        return $this->getQuestionLibrary(true)->confirm($sQuestion, $sYesScript, $sNoScript);
596
    }
597
}
598