Passed
Push — v5.x ( 59ec3d...dec8cc )
by Thierry
02:06
created

DialogLibraryManager::getMessageLibrary()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 1
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\Request\Call\Parameter;
26
use Jaxon\Utils\Config\Config;
27
28
use function array_map;
29
use function array_keys;
30
use function class_implements;
31
use function in_array;
32
use function substr;
33
34
class DialogLibraryManager implements ConfigListenerInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DialogLibraryManager
Loading history...
35
{
36
    use QuestionTrait;
37
    use MessageTrait;
38
    use ModalTrait;
39
40
    /**
41
     * @var Container
42
     */
43
    private $di;
44
45
    /**
46
     * @var array
47
     */
48
    protected $aLibraries = [];
49
50
    /**
51
     * The QuestionInterface class name
52
     *
53
     * @var string
54
     */
55
    private $sQuestionLibrary = '';
56
57
    /**
58
     * The MessageInterface class name
59
     *
60
     * @var string
61
     */
62
    private $sMessageLibrary = '';
63
64
    /**
65
     * The ModalInterface class name
66
     *
67
     * @var string
68
     */
69
    private $sModalLibrary = '';
70
71
    /**
72
     * The name of the library to use for the next call.
73
     * This is used to override the default library.
74
     *
75
     * @var string
76
     */
77
    protected $sNextLibrary = '';
78
79
    /**
80
     * @var ConfigManager
81
     */
82
    protected $xConfigManager;
83
84
    /**
85
     * @var Translator
86
     */
87
    private $xTranslator;
88
89
    /**
90
     * The constructor
91
     *
92
     * @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...
93
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
94
     * @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...
95
     */
96
    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...
97
    {
98
        $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...
99
        $this->xConfigManager = $xConfigManager;
100
        $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...
101
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
102
103
    /**
104
     * @param string $sStr
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
105
     * @param array $aArgs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
106
     *
107
     * @return array
108
     */
109
    private function phrase(string $sStr, array $aArgs = []): array
0 ignored issues
show
Unused Code introduced by
The method phrase() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
110
    {
111
        return [
112
            'str' => $sStr,
113
            'args' => array_map(fn($xArg) => Parameter::make($xArg), $aArgs),
114
        ];
115
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
116
117
    /**
118
     * Register a javascript dialog library adapter.
119
     *
120
     * @param string $sClassName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
121
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
122
     *
123
     * @return void
124
     * @throws SetupException
125
     */
126
    public function registerLibrary(string $sClassName, string $sLibraryName)
127
    {
128
        if(isset($this->aLibraries[$sLibraryName]))
129
        {
130
            return;
131
        }
132
        if(!($aInterfaces = class_implements($sClassName)))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
133
        {
134
            // The class is invalid.
135
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
136
            throw new SetupException($sMessage);
137
        }
138
139
        $bIsQuestion = in_array(QuestionInterface::class, $aInterfaces);
140
        $bIsMessage = in_array(MessageInterface::class, $aInterfaces);
141
        $bIsModal = in_array(ModalInterface::class, $aInterfaces);
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...
142
        if(!$bIsQuestion && !$bIsMessage && !$bIsModal)
143
        {
144
            // The class is invalid.
145
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
146
            throw new SetupException($sMessage);
147
        }
148
149
        // Save the library
150
        $this->aLibraries[$sLibraryName] = [
151
            'question' => $bIsQuestion,
152
            'message' => $bIsMessage,
153
            'modal' => $bIsModal,
154
            'used' => false,
155
        ];
156
        // Register the library class in the container
157
        $this->di->registerDialogLibrary($sClassName, $sLibraryName);
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->aLibraries[$sLibraryName]) || !$this->aLibraries[$sLibraryName]['question'])
171
        {
172
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
173
                ['type' => 'question', 'name' => $sLibraryName]);
174
            throw new SetupException($sMessage);
175
        }
176
        $this->sQuestionLibrary = $sLibraryName;
177
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
178
179
    /**
180
     * Get the QuestionInterface library
181
     *
182
     * @return QuestionInterface
183
     */
184
    public function getQuestionLibrary(): QuestionInterface
185
    {
186
        return $this->di->getQuestionLibrary($this->sNextLibrary ?: $this->sQuestionLibrary);
187
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
188
189
    /**
190
     * Set MessageInterface library
191
     *
192
     * @param string $sLibraryName The MessageInterface library name
193
     *
194
     * @return void
195
     * @throws SetupException
196
     */
197
    public function setMessageLibrary(string $sLibraryName)
198
    {
199
        if(!isset($this->aLibraries[$sLibraryName]) || !$this->aLibraries[$sLibraryName]['message'])
200
        {
201
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
202
                ['type' => 'message', 'name' => $sLibraryName]);
203
            throw new SetupException($sMessage);
204
        }
205
        $this->sMessageLibrary = $sLibraryName;
206
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
207
208
    /**
209
     * Get the MessageInterface library
210
     *
211
     * @return MessageInterface
212
     */
213
    public function getMessageLibrary(): MessageInterface
214
    {
215
        return $this->di->getMessageLibrary($this->sNextLibrary ?: $this->sMessageLibrary);
216
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
217
218
    /**
219
     * Set the ModalInterface library
220
     *
221
     * @param string $sLibraryName The ModalInterface library name
222
     *
223
     * @return void
224
     * @throws SetupException
225
     */
226
    public function setModalLibrary(string $sLibraryName)
227
    {
228
        if(!isset($this->aLibraries[$sLibraryName]) || !$this->aLibraries[$sLibraryName]['modal'])
229
        {
230
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
231
                ['type' => 'modal', 'name' => $sLibraryName]);
232
            throw new SetupException($sMessage);
233
        }
234
        $this->sModalLibrary = $sLibraryName;
235
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
236
237
    /**
238
     * Get the ModalInterface library
239
     *
240
     * @return ModalInterface|null
241
     */
242
    public function getModalLibrary(): ?ModalInterface
243
    {
244
        return $this->di->getModalLibrary($this->sNextLibrary ?: $this->sModalLibrary);
245
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
246
247
    /**
248
     * Set the name of the library to use for the next call
249
     *
250
     * @param string $sNextLibrary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
251
     *
252
     * @return void
253
     */
254
    public function setNextLibrary(string $sNextLibrary): void
255
    {
256
        $this->sNextLibrary = $sNextLibrary;
257
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
258
259
    /**
260
     * Register the javascript dialog libraries from config options.
261
     *
262
     * @return void
263
     * @throws SetupException
264
     */
265
    protected function registerLibraries()
266
    {
267
        $aLibraries = $this->xConfigManager->getOption('dialogs.lib.ext', []);
268
        foreach($aLibraries as $sLibraryName => $sClassName)
269
        {
270
            $this->registerLibrary($sClassName, $sLibraryName);
271
        }
272
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
273
274
    /**
275
     * Set the default library for each dialog feature.
276
     *
277
     * @return void
278
     * @throws SetupException
279
     */
280
    protected function setDefaultLibraries()
281
    {
282
        // Set the default modal library
283
        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...
284
        {
285
            $this->setModalLibrary($sLibraryName);
286
            $this->aLibraries[$sLibraryName]['used'] = true;
287
        }
288
        // Set the default message library
289
        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...
290
        {
291
            $this->setMessageLibrary($sLibraryName);
292
            $this->aLibraries[$sLibraryName]['used'] = true;
293
        }
294
        // Set the default question library
295
        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...
296
        {
297
            $this->setQuestionLibrary($sLibraryName);
298
            $this->aLibraries[$sLibraryName]['used'] = true;
299
        }
300
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
301
302
    /**
303
     * Set the libraries in use.
304
     *
305
     * @return void
306
     */
307
    protected function setUsedLibraries()
308
    {
309
        // Set the other libraries in use
310
        $aLibraries = $this->xConfigManager->getOption('dialogs.lib.use', []);
311
        foreach($aLibraries as $sLibraryName)
312
        {
313
            if(isset($this->aLibraries[$sLibraryName])) // Make sure the library exists
314
            {
315
                $this->aLibraries[$sLibraryName]['used'] = true;
316
            }
317
        }
318
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
319
320
    /**
321
     * Get the dialog libraries class instances
322
     *
323
     * @return LibraryInterface[]
324
     */
325
    public function getLibraries(): array
326
    {
327
        // Only return the libraries that are used.
328
        $aLibraries = array_filter($this->aLibraries, function($aLibrary) {
329
            return $aLibrary['used'];
330
        });
331
        return array_map(function($sLibraryName) {
332
            return $this->di->getDialogLibrary($sLibraryName);
333
        }, array_keys($aLibraries));
334
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
335
336
    /**
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...
337
     * @inheritDoc
338
     * @throws SetupException
339
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
340
    public function onChange(Config $xConfig, string $sName)
341
    {
342
        if($sName === '')
343
        {
344
            // Reset the default libraries any time the config is changed.
345
            $this->registerLibraries();
346
            $this->setDefaultLibraries();
347
            $this->setUsedLibraries();
348
            return;
349
        }
350
        $sPrefix = substr($sName, 0, 15);
351
        switch($sPrefix)
352
        {
353
        case 'dialogs.default':
354
            $this->setDefaultLibraries();
355
            return;
356
        case 'dialogs.lib.ext':
357
            $this->registerLibraries();
358
            return;
359
        case 'dialogs.lib.use':
360
            $this->setUsedLibraries();
361
            return;
362
        default:
363
        }
364
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
365
}
366