Passed
Push — master ( 22a324...09a587 )
by Thierry
06:55 queued 03:38
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
     * The QuestionInterface class name
47
     *
48
     * @var string
49
     */
50
    private $sQuestionLibrary = '';
51
52
    /**
53
     * The MessageInterface class name
54
     *
55
     * @var string
56
     */
57
    private $sMessageLibrary = '';
58
59
    /**
60
     * The ModalInterface class name
61
     *
62
     * @var string
63
     */
64
    private $sModalLibrary = '';
65
66
    /**
67
     * The name of the library to use for the next call.
68
     * This is used to override the default library.
69
     *
70
     * @var string
71
     */
72
    protected $sNextLibrary = '';
73
74
    /**
75
     * @var ConfigManager
76
     */
77
    protected $xConfigManager;
78
79
    /**
80
     * @var Translator
81
     */
82
    private $xTranslator;
83
84
    /**
85
     * The constructor
86
     *
87
     * @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...
88
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
89
     * @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...
90
     */
91
    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...
92
    {
93
        $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...
94
        $this->xConfigManager = $xConfigManager;
95
        $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...
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * Register a javascript dialog library adapter.
100
     *
101
     * @param string $sClassName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
102
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
103
     *
104
     * @return void
105
     * @throws SetupException
106
     */
107
    public function registerLibrary(string $sClassName, string $sLibraryName)
108
    {
109
        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...
110
        {
111
            // The class is invalid.
112
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
113
            throw new SetupException($sMessage);
114
        }
115
116
        $bIsQuestion = in_array(QuestionInterface::class, $aInterfaces);
117
        $bIsMessage = in_array(MessageInterface::class, $aInterfaces);
118
        $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...
119
        if(!$bIsQuestion && !$bIsMessage && !$bIsModal)
120
        {
121
            // The class is invalid.
122
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
123
            throw new SetupException($sMessage);
124
        }
125
126
        // Save the library
127
        $this->aLibraries[$sLibraryName] = [
128
            'question' => $bIsQuestion,
129
            'message' => $bIsMessage,
130
            'modal' => $bIsModal,
131
        ];
132
        // Register the library class in the container
133
        $this->di->registerDialogLibrary($sClassName, $sLibraryName);
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Get the library class instances
138
     *
139
     * @return LibraryInterface[]
140
     */
141
    public function getLibraries(): array
142
    {
143
        return array_map(function($sLibraryName) {
144
            return $this->di->getDialogLibrary($sLibraryName);
145
        }, array_keys($this->aLibraries));
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Set the QuestionInterface library
150
     *
151
     * @param string $sLibraryName The QuestionInterface library name
152
     *
153
     * @return void
154
     * @throws SetupException
155
     */
156
    public function setQuestionLibrary(string $sLibraryName)
157
    {
158
        if(!isset($this->aLibraries[$sLibraryName]) || !$this->aLibraries[$sLibraryName]['question'])
159
        {
160
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
161
                ['type' => 'question', 'name' => $sLibraryName]);
162
            throw new SetupException($sMessage);
163
        }
164
        $this->sQuestionLibrary = $sLibraryName;
165
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
166
167
    /**
168
     * Get the QuestionInterface library
169
     *
170
     * @return QuestionInterface
171
     */
172
    public function getQuestionLibrary(): QuestionInterface
173
    {
174
        return $this->di->getQuestionLibrary($this->sNextLibrary ?: $this->sQuestionLibrary);
175
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
176
177
    /**
178
     * Set MessageInterface library
179
     *
180
     * @param string $sLibraryName The MessageInterface library name
181
     *
182
     * @return void
183
     * @throws SetupException
184
     */
185
    public function setMessageLibrary(string $sLibraryName)
186
    {
187
        if(!isset($this->aLibraries[$sLibraryName]) || !$this->aLibraries[$sLibraryName]['message'])
188
        {
189
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
190
                ['type' => 'message', 'name' => $sLibraryName]);
191
            throw new SetupException($sMessage);
192
        }
193
        $this->sMessageLibrary = $sLibraryName;
194
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
195
196
    /**
197
     * Get the MessageInterface library
198
     *
199
     * @return MessageInterface
200
     */
201
    public function getMessageLibrary(): MessageInterface
202
    {
203
        return $this->di->getMessageLibrary($this->sNextLibrary ?: $this->sMessageLibrary);
204
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
205
206
    /**
207
     * Set the ModalInterface library
208
     *
209
     * @param string $sLibraryName The ModalInterface library name
210
     *
211
     * @return void
212
     * @throws SetupException
213
     */
214
    public function setModalLibrary(string $sLibraryName)
215
    {
216
        if(!isset($this->aLibraries[$sLibraryName]) || !$this->aLibraries[$sLibraryName]['modal'])
217
        {
218
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
219
                ['type' => 'modal', 'name' => $sLibraryName]);
220
            throw new SetupException($sMessage);
221
        }
222
        $this->sModalLibrary = $sLibraryName;
223
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
224
225
    /**
226
     * Get the ModalInterface library
227
     *
228
     * @return ModalInterface
229
     */
230
    public function getModalLibrary(): ?ModalInterface
231
    {
232
        return $this->di->getModalLibrary($this->sNextLibrary ?: $this->sModalLibrary);
233
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
234
235
    /**
236
     * Set the name of the library to use for the next call
237
     *
238
     * @param string $sNextLibrary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
239
     *
240
     * @return void
241
     */
242
    public function setNextLibrary(string $sNextLibrary): void
243
    {
244
        $this->sNextLibrary = $sNextLibrary;
245
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
246
247
    /**
248
     * Register the javascript dialog libraries from config options.
249
     *
250
     * @return void
251
     * @throws SetupException
252
     */
253
    protected function registerLibraries()
254
    {
255
        $aLibraries = $this->xConfigManager->getOption('dialogs.libraries', []);
256
        foreach($aLibraries as $sLibraryName => $sClassName)
257
        {
258
            $this->registerLibrary($sClassName, $sLibraryName);
259
        }
260
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
261
262
    /**
263
     * Set the default library for each dialog feature.
264
     *
265
     * @return void
266
     * @throws SetupException
267
     */
268
    protected function setDefaultLibraries()
269
    {
270
        // Set the default modal library
271
        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...
272
        {
273
            $this->setModalLibrary($sLibraryName);
274
        }
275
        // Set the default message library
276
        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...
277
        {
278
            $this->setMessageLibrary($sLibraryName);
279
        }
280
        // Set the default question library
281
        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...
282
        {
283
            $this->setQuestionLibrary($sLibraryName);
284
        }
285
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
286
287
    /**
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...
288
     * @inheritDoc
289
     * @throws SetupException
290
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
291
    public function onChange(Config $xConfig, string $sName)
292
    {
293
        if($sName === '')
294
        {
295
            // Reset the default libraries any time the config is changed.
296
            $this->registerLibraries();
297
            $this->setDefaultLibraries();
298
            return;
299
        }
300
        if(substr($sName, 0, 17) === 'dialogs.libraries')
301
        {
302
            $this->registerLibraries();
303
            return;
304
        }
305
        if(substr($sName, 0, 15) === 'dialogs.default')
306
        {
307
            $this->setDefaultLibraries();
308
        }
309
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
310
}
311