Passed
Push — master ( b86a9b...818b3e )
by Thierry
02:35
created

DialogLibraryManager   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 306
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 33
eloc 71
c 0
b 0
f 0
dl 0
loc 306
rs 9.76

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultLibraries() 0 16 4
A __construct() 0 7 1
A onChanges() 0 5 1
A getLibraries() 0 5 1
A onChange() 0 6 2
A getMessageLibrary() 0 4 3
A registerLibrary() 0 28 5
A setQuestionLibrary() 0 9 2
A setNextLibrary() 0 3 1
A setMessageLibrary() 0 9 2
A getQuestionLibrary() 0 4 3
A getModalLibrary() 0 4 3
A setModalLibrary() 0 9 2
A registerLibraries() 0 8 3
1
<?php
2
3
/**
4
 * DialogLibraryManager.php
5
 *
6
 * Manage dialog library list and defaults.
7
 *
8
 * @author Thierry Feuzeu <[email protected]>
9
 * @copyright 2019 Thierry Feuzeu <[email protected]>
10
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
11
 * @link https://github.com/jaxon-php/jaxon-core
12
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
13
14
namespace Jaxon\App\Dialog\Library;
15
16
use Jaxon\App\Config\ConfigListenerInterface;
17
use Jaxon\App\Config\ConfigManager;
18
use Jaxon\App\Dialog\MessageInterface;
19
use Jaxon\App\Dialog\ModalInterface;
20
use Jaxon\App\Dialog\QuestionInterface;
21
use Jaxon\App\I18n\Translator;
22
use Jaxon\Di\Container;
23
use Jaxon\Exception\SetupException;
24
use Jaxon\Utils\Config\Config;
25
26
use function array_map;
27
use function class_implements;
28
use function in_array;
29
use function substr;
30
31
class DialogLibraryManager implements ConfigListenerInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DialogLibraryManager
Loading history...
32
{
33
    /**
34
     * @var Container
35
     */
36
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
37
38
    /**
39
     * @var array
40
     */
41
    protected $aLibraries = [];
42
43
    /**
44
     * @var array
45
     */
46
    protected $aQuestionLibraries = [];
47
48
    /**
49
     * @var array
50
     */
51
    protected $aMessageLibraries = [];
52
53
    /**
54
     * @var array
55
     */
56
    protected $aModalLibraries = [];
57
58
    /**
59
     * The QuestionInterface class name
60
     *
61
     * @var string
62
     */
63
    private $sQuestionLibrary = '';
64
65
    /**
66
     * The MessageInterface class name
67
     *
68
     * @var string
69
     */
70
    private $sMessageLibrary = '';
71
72
    /**
73
     * The ModalInterface class name
74
     *
75
     * @var string
76
     */
77
    private $sModalLibrary = '';
78
79
    /**
80
     * The name of the library to use for the next call.
81
     * This is used to override the default library.
82
     *
83
     * @var string
84
     */
85
    protected $sNextLibrary = '';
86
87
    /**
88
     * Default javascript alert library
89
     *
90
     * @var AlertLibrary
91
     */
92
    private $xAlertLibrary;
93
94
    /**
95
     * @var ConfigManager
96
     */
97
    protected $xConfigManager;
98
99
    /**
100
     * @var Translator
101
     */
102
    private $xTranslator;
103
104
    /**
105
     * The constructor
106
     *
107
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
108
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
109
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
110
     */
111
    public function __construct(Container $di, ConfigManager $xConfigManager, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
112
    {
113
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
114
        $this->xConfigManager = $xConfigManager;
115
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
116
        // Library for javascript confirm and alert functions.
117
        $this->xAlertLibrary = new AlertLibrary();
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Register a javascript dialog library adapter.
122
     *
123
     * @param string $sClassName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
124
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
125
     *
126
     * @return void
127
     * @throws SetupException
128
     */
129
    public function registerLibrary(string $sClassName, string $sName)
130
    {
131
        $bIsUsed = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
132
        $aInterfaces = class_implements($sClassName);
133
        if(in_array(QuestionInterface::class, $aInterfaces))
134
        {
135
            $this->aQuestionLibraries[$sName] = $sClassName;
136
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 26 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
137
        }
138
        if(in_array(MessageInterface::class, $aInterfaces))
139
        {
140
            $this->aMessageLibraries[$sName] = $sClassName;
141
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 25 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
142
        }
143
        if(in_array(ModalInterface::class, $aInterfaces))
144
        {
145
            $this->aModalLibraries[$sName] = $sClassName;
146
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 23 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
147
        }
148
149
        if(!$bIsUsed)
150
        {
151
            // The class is invalid.
152
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
153
            throw new SetupException($sMessage);
154
        }
155
        $this->aLibraries[] = $sClassName;
156
        $this->di->registerDialogLibrary($sClassName);
157
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
158
159
    /**
160
     * Get the library class instances
161
     *
162
     * @return array
163
     */
164
    public function getLibraries(): array
165
    {
166
        return array_map(function($sClassName) {
167
            return $this->di->g($sClassName);
168
        }, $this->aLibraries);
169
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
170
171
    /**
172
     * Set the QuestionInterface library
173
     *
174
     * @param string $sLibraryName The QuestionInterface library name
175
     *
176
     * @return void
177
     * @throws SetupException
178
     */
179
    public function setQuestionLibrary(string $sLibraryName)
180
    {
181
        if(!isset($this->aQuestionLibraries[$sLibraryName]))
182
        {
183
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
184
                ['type' => 'question', 'name' => $sLibraryName]);
185
            throw new SetupException($sMessage);
186
        }
187
        $this->sQuestionLibrary = $sLibraryName;
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
189
190
    /**
191
     * Get the QuestionInterface library
192
     *
193
     * @return QuestionInterface
194
     */
195
    public function getQuestionLibrary(): QuestionInterface
196
    {
197
        $sLibraryName = $this->sNextLibrary ?: $this->sQuestionLibrary;
198
        return ($sLibraryName) ? $this->di->g($this->aQuestionLibraries[$sLibraryName]) : $this->xAlertLibrary;
199
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
200
201
    /**
202
     * Set MessageInterface library
203
     *
204
     * @param string $sLibraryName The MessageInterface library name
205
     *
206
     * @return void
207
     * @throws SetupException
208
     */
209
    public function setMessageLibrary(string $sLibraryName)
210
    {
211
        if(!isset($this->aMessageLibraries[$sLibraryName]))
212
        {
213
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
214
                ['type' => 'message', 'name' => $sLibraryName]);
215
            throw new SetupException($sMessage);
216
        }
217
        $this->sMessageLibrary = $sLibraryName;
218
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
219
220
    /**
221
     * Get the MessageInterface library
222
     *
223
     * @return MessageInterface
224
     */
225
    public function getMessageLibrary(): MessageInterface
226
    {
227
        $sLibraryName = $this->sNextLibrary ?: $this->sMessageLibrary;
228
        return ($sLibraryName) ? $this->di->g($this->aMessageLibraries[$sLibraryName]) : $this->xAlertLibrary;
229
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
230
231
    /**
232
     * Set the ModalInterface library
233
     *
234
     * @param string $sLibraryName The ModalInterface library name
235
     *
236
     * @return void
237
     * @throws SetupException
238
     */
239
    public function setModalLibrary(string $sLibraryName)
240
    {
241
        if(!isset($this->aModalLibraries[$sLibraryName]))
242
        {
243
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
244
                ['type' => 'modal', 'name' => $sLibraryName]);
245
            throw new SetupException($sMessage);
246
        }
247
        $this->sModalLibrary = $sLibraryName;
248
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
249
250
    /**
251
     * Get the ModalInterface library
252
     *
253
     * @return ModalInterface
254
     */
255
    public function getModalLibrary(): ?ModalInterface
256
    {
257
        $sLibraryName = $this->sNextLibrary ?: $this->sModalLibrary;
258
        return ($sLibraryName) ? $this->di->g($this->aModalLibraries[$sLibraryName]) : null;
259
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
260
261
    /**
262
     * Set the name of the library to use for the next call
263
     *
264
     * @param string $sNextLibrary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
265
     *
266
     * @return void
267
     */
268
    public function setNextLibrary(string $sNextLibrary): void
269
    {
270
        $this->sNextLibrary = $sNextLibrary;
271
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
272
273
    /**
274
     * Register the javascript dialog libraries from config options.
275
     *
276
     * @return void
277
     * @throws SetupException
278
     */
279
    protected function registerLibraries()
280
    {
281
        $aLibraries = $this->xConfigManager->getOption('dialogs.libraries', []);
282
        foreach($aLibraries as $sClassName => $sName)
283
        {
284
            if(!$this->di->h($sClassName))
285
            {
286
                $this->registerLibrary($sClassName, $sName);
287
            }
288
        }
289
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
290
291
    /**
292
     * Set the default library for each dialog feature.
293
     *
294
     * @return void
295
     * @throws SetupException
296
     */
297
    protected function setDefaultLibraries()
298
    {
299
        // Set the default modal library
300
        if(($sName = $this->xConfigManager->getOption('dialogs.default.modal', '')))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
301
        {
302
            $this->setModalLibrary($sName);
303
        }
304
        // Set the default message library
305
        if(($sName = $this->xConfigManager->getOption('dialogs.default.message', '')))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
306
        {
307
            $this->setMessageLibrary($sName);
308
        }
309
        // Set the default question library
310
        if(($sName = $this->xConfigManager->getOption('dialogs.default.question', '')))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
311
        {
312
            $this->setQuestionLibrary($sName);
313
        }
314
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
315
316
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xConfig should have a doc-comment as per coding-style.
Loading history...
317
     * @inheritDoc
318
     * @throws SetupException
319
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
320
    public function onChanges(Config $xConfig)
321
    {
322
        // Reset the default libraries any time the config is changed.
323
        $this->registerLibraries();
324
        $this->setDefaultLibraries();
325
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
326
327
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xConfig should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sName should have a doc-comment as per coding-style.
Loading history...
328
     * @inheritDoc
329
     * @throws SetupException
330
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
331
    public function onChange(Config $xConfig, string $sName)
332
    {
333
        // Reset the default libraries any time the config is changed.
334
        if(substr($sName, 0, 15) === 'dialogs.default')
335
        {
336
            $this->setDefaultLibraries();
337
        }
338
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
339
}
340