Passed
Push — master ( 382ef3...ddad46 )
by Thierry
03:13 queued 01:02
created

DialogLibraryManager::updateLibraries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
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\LibraryInterface;
19
use Jaxon\App\Dialog\MessageInterface;
20
use Jaxon\App\Dialog\ModalInterface;
21
use Jaxon\App\Dialog\QuestionInterface;
22
use Jaxon\App\I18n\Translator;
23
use Jaxon\Di\Container;
24
use Jaxon\Exception\SetupException;
25
use Jaxon\Utils\Config\Config;
26
27
use function array_map;
28
use function array_keys;
29
use function class_implements;
30
use function in_array;
31
use function substr;
32
33
class DialogLibraryManager implements ConfigListenerInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DialogLibraryManager
Loading history...
34
{
35
    /**
36
     * @var Container
37
     */
38
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
39
40
    /**
41
     * @var array
42
     */
43
    protected $aLibraries = [];
44
45
    /**
46
     * @var array
47
     */
48
    protected $aQuestionLibraries = [];
49
50
    /**
51
     * @var array
52
     */
53
    protected $aMessageLibraries = [];
54
55
    /**
56
     * @var array
57
     */
58
    protected $aModalLibraries = [];
59
60
    /**
61
     * The QuestionInterface class name
62
     *
63
     * @var string
64
     */
65
    private $sQuestionLibrary = '';
66
67
    /**
68
     * The MessageInterface class name
69
     *
70
     * @var string
71
     */
72
    private $sMessageLibrary = '';
73
74
    /**
75
     * The ModalInterface class name
76
     *
77
     * @var string
78
     */
79
    private $sModalLibrary = '';
80
81
    /**
82
     * The name of the library to use for the next call.
83
     * This is used to override the default library.
84
     *
85
     * @var string
86
     */
87
    protected $sNextLibrary = '';
88
89
    /**
90
     * @var ConfigManager
91
     */
92
    protected $xConfigManager;
93
94
    /**
95
     * @var Translator
96
     */
97
    private $xTranslator;
98
99
    /**
100
     * The constructor
101
     *
102
     * @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...
103
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
104
     * @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...
105
     */
106
    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...
107
    {
108
        $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...
109
        $this->xConfigManager = $xConfigManager;
110
        $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...
111
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
112
113
    /**
114
     * Register a javascript dialog library adapter.
115
     *
116
     * @param string $sClassName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
117
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
118
     *
119
     * @return void
120
     * @throws SetupException
121
     */
122
    public function registerLibrary(string $sClassName, string $sLibraryName)
123
    {
124
        $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...
125
        $aInterfaces = class_implements($sClassName);
126
        if(in_array(QuestionInterface::class, $aInterfaces))
127
        {
128
            $this->aQuestionLibraries[$sLibraryName] = $bIsUsed = true;
129
        }
130
        if(in_array(MessageInterface::class, $aInterfaces))
131
        {
132
            $this->aMessageLibraries[$sLibraryName] = $bIsUsed = true;
133
        }
134
        if(in_array(ModalInterface::class, $aInterfaces))
135
        {
136
            $this->aModalLibraries[$sLibraryName] = $bIsUsed = true;
137
        }
138
        if(!$bIsUsed)
139
        {
140
            // The class is invalid.
141
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
142
            throw new SetupException($sMessage);
143
        }
144
        // Register the library in the container
145
        $this->di->registerDialogLibrary($sClassName, $sLibraryName);
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Get the library class instances
150
     *
151
     * @return LibraryInterface[]
152
     */
153
    public function getLibraries(): array
154
    {
155
        return array_map(function($sLibraryName) {
156
            return $this->di->getDialogLibrary($sLibraryName);
157
        }, array_keys($this->aLibraries));
158
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
159
160
    /**
161
     * Set the QuestionInterface library
162
     *
163
     * @param string $sLibraryName The QuestionInterface library name
164
     *
165
     * @return void
166
     * @throws SetupException
167
     */
168
    public function setQuestionLibrary(string $sLibraryName)
169
    {
170
        if(!isset($this->aQuestionLibraries[$sLibraryName]))
171
        {
172
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
173
                ['type' => 'question', 'name' => $sLibraryName]);
174
            throw new SetupException($sMessage);
175
        }
176
        $this->sQuestionLibrary = $sLibraryName;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
177
        $this->aLibraries[$sLibraryName] = true;
178
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
179
180
    /**
181
     * Get the QuestionInterface library
182
     *
183
     * @return QuestionInterface
184
     */
185
    public function getQuestionLibrary(): QuestionInterface
186
    {
187
        return $this->di->getQuestionLibrary($this->sNextLibrary ?: $this->sQuestionLibrary);
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
189
190
    /**
191
     * Set MessageInterface library
192
     *
193
     * @param string $sLibraryName The MessageInterface library name
194
     *
195
     * @return void
196
     * @throws SetupException
197
     */
198
    public function setMessageLibrary(string $sLibraryName)
199
    {
200
        if(!isset($this->aMessageLibraries[$sLibraryName]))
201
        {
202
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
203
                ['type' => 'message', 'name' => $sLibraryName]);
204
            throw new SetupException($sMessage);
205
        }
206
        $this->sMessageLibrary = $sLibraryName;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
207
        $this->aLibraries[$sLibraryName] = true;
208
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
209
210
    /**
211
     * Get the MessageInterface library
212
     *
213
     * @return MessageInterface
214
     */
215
    public function getMessageLibrary(): MessageInterface
216
    {
217
        return $this->di->getMessageLibrary($this->sNextLibrary ?: $this->sMessageLibrary);
218
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
219
220
    /**
221
     * Set the ModalInterface library
222
     *
223
     * @param string $sLibraryName The ModalInterface library name
224
     *
225
     * @return void
226
     * @throws SetupException
227
     */
228
    public function setModalLibrary(string $sLibraryName)
229
    {
230
        if(!isset($this->aModalLibraries[$sLibraryName]))
231
        {
232
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
233
                ['type' => 'modal', 'name' => $sLibraryName]);
234
            throw new SetupException($sMessage);
235
        }
236
        $this->sModalLibrary = $sLibraryName;
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...
237
        $this->aLibraries[$sLibraryName] = true;
238
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
239
240
    /**
241
     * Get the ModalInterface library
242
     *
243
     * @return ModalInterface
244
     */
245
    public function getModalLibrary(): ?ModalInterface
246
    {
247
        return $this->di->getModalLibrary($this->sNextLibrary ?: $this->sModalLibrary);
248
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
249
250
    /**
251
     * Set the name of the library to use for the next call
252
     *
253
     * @param string $sNextLibrary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
254
     *
255
     * @return void
256
     */
257
    public function setNextLibrary(string $sNextLibrary): void
258
    {
259
        $this->sNextLibrary = $sNextLibrary;
260
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
261
262
    /**
263
     * Register the javascript dialog libraries from config options.
264
     *
265
     * @return void
266
     * @throws SetupException
267
     */
268
    protected function registerLibraries()
269
    {
270
        $aLibraries = $this->xConfigManager->getOption('dialogs.classes', []);
271
        foreach($aLibraries as $sLibraryName => $sClassName)
272
        {
273
            $this->registerLibrary($sClassName, $sLibraryName);
274
        }
275
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
276
277
    /**
278
     * Update the javascript dialog libraries list.
279
     *
280
     * @return void
281
     */
282
    protected function updateLibraries()
283
    {
284
        $aLibraries = $this->xConfigManager->getOption('dialogs.libraries', []);
285
        foreach($aLibraries as $sLibraryName)
286
        {
287
            $this->aLibraries[$sLibraryName] = true;
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(($sLibraryName = $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($sLibraryName);
303
        }
304
        // Set the default message library
305
        if(($sLibraryName = $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($sLibraryName);
308
        }
309
        // Set the default question library
310
        if(($sLibraryName = $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($sLibraryName);
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->updateLibraries();
325
        $this->setDefaultLibraries();
326
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
327
328
    /**
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...
329
     * @inheritDoc
330
     * @throws SetupException
331
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
332
    public function onChange(Config $xConfig, string $sName)
333
    {
334
        if(substr($sName, 0, 15) === 'dialogs.classes')
335
        {
336
            $this->registerLibraries();
337
            return;
338
        }
339
        if($sName === 'dialogs.libraries')
340
        {
341
            $this->updateLibraries();
342
            return;
343
        }
344
        if(substr($sName, 0, 15) === 'dialogs.default')
345
        {
346
            $this->setDefaultLibraries();
347
        }
348
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
349
}
350