Passed
Push — master ( 6f7a23...2cf817 )
by Thierry
02:29
created

DialogLibraryManager::getLibraries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * DialogLibraryManager.php - Shows alert and confirm dialogs
5
 *
6
 * @author Thierry Feuzeu <[email protected]>
7
 * @copyright 2019 Thierry Feuzeu <[email protected]>
8
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
9
 * @link https://github.com/jaxon-php/jaxon-core
10
 */
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...
11
12
namespace Jaxon\Ui\Dialog\Library;
13
14
use Jaxon\Di\Container;
15
use Jaxon\Exception\SetupException;
16
use Jaxon\Ui\Dialog\MessageInterface;
17
use Jaxon\Ui\Dialog\ModalInterface;
18
use Jaxon\Ui\Dialog\QuestionInterface;
19
use Jaxon\Utils\Translation\Translator;
20
21
use function array_map;
22
use function class_implements;
23
use function in_array;
24
25
class DialogLibraryManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DialogLibraryManager
Loading history...
26
{
27
    /**
28
     * @var Container
29
     */
30
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
31
32
    /**
33
     * @var array
34
     */
35
    protected $aLibraries = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $aQuestionLibraries = [];
41
42
    /**
43
     * @var array
44
     */
45
    protected $aMessageLibraries = [];
46
47
    /**
48
     * @var array
49
     */
50
    protected $aModalLibraries = [];
51
52
    /**
53
     * The QuestionInterface class name
54
     *
55
     * @var string
56
     */
57
    private $sQuestionLibrary = '';
58
59
    /**
60
     * The MessageInterface class name
61
     *
62
     * @var string
63
     */
64
    private $sMessageLibrary = '';
65
66
    /**
67
     * The ModalInterface class name
68
     *
69
     * @var string
70
     */
71
    private $sModalLibrary = '';
72
73
    /**
74
     * The name of the library to use for the next call.
75
     * This is used to override the default library.
76
     *
77
     * @var string
78
     */
79
    protected $sNextLibrary = '';
80
81
    /**
82
     * Default javascript alert library
83
     *
84
     * @var AlertLibrary
85
     */
86
    private $xAlertLibrary;
87
88
    /**
89
     * @var Translator
90
     */
91
    private $xTranslator;
92
93
    /**
94
     * The constructor
95
     *
96
     * @param Container $di
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...
97
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
98
     */
99
    public function __construct(Container $di, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
100
    {
101
        $this->di = $di;
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...
102
        $this->xTranslator = $xTranslator;
103
        // Library for javascript confirm and alert functions.
104
        $this->xAlertLibrary = new AlertLibrary();
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     * Register a javascript dialog library adapter.
109
     *
110
     * @param string $sClassName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
111
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
112
     *
113
     * @return void
114
     * @throws SetupException
115
     */
116
    public function registerLibrary(string $sClassName, string $sName)
117
    {
118
        $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...
119
        $aInterfaces = class_implements($sClassName);
120
        if(in_array(QuestionInterface::class, $aInterfaces))
121
        {
122
            $this->aQuestionLibraries[$sName] = $sClassName;
123
            $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...
124
        }
125
        if(in_array(MessageInterface::class, $aInterfaces))
126
        {
127
            $this->aMessageLibraries[$sName] = $sClassName;
128
            $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...
129
        }
130
        if(in_array(ModalInterface::class, $aInterfaces))
131
        {
132
            $this->aModalLibraries[$sName] = $sClassName;
133
            $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...
134
        }
135
136
        if(!$bIsUsed)
137
        {
138
            // The class is invalid.
139
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
140
            throw new SetupException($sMessage);
141
        }
142
        $this->aLibraries[] = $sClassName;
143
        $this->di->registerDialogLibrary($sClassName);
144
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
145
146
    /**
147
     * Get the library class instances
148
     *
149
     * @return array
150
     */
151
    public function getLibraries(): array
152
    {
153
        return array_map(function($sClassName) {
154
            return $this->di->g($sClassName);
155
        }, $this->aLibraries);
156
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
157
158
    /**
159
     * Set the QuestionInterface library
160
     *
161
     * @param string $sLibraryName The QuestionInterface library name
162
     *
163
     * @return void
164
     * @throws SetupException
165
     */
166
    public function setQuestionLibrary(string $sLibraryName)
167
    {
168
        if(!isset($this->aQuestionLibraries[$sLibraryName]))
169
        {
170
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
171
                ['type' => 'question', 'name' => $sLibraryName]);
172
            throw new SetupException($sMessage);
173
        }
174
        $this->sQuestionLibrary = $sLibraryName;
175
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
176
177
    /**
178
     * Get the QuestionInterface library
179
     *
180
     * @return QuestionInterface
181
     */
182
    public function getQuestionLibrary(): QuestionInterface
183
    {
184
        $sLibraryName = $this->sNextLibrary ?: $this->sQuestionLibrary;
185
        return ($sLibraryName) ? $this->di->g($this->aQuestionLibraries[$sLibraryName]) : $this->xAlertLibrary;
186
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
187
188
    /**
189
     * Set MessageInterface library
190
     *
191
     * @param string $sLibraryName The MessageInterface library name
192
     *
193
     * @return void
194
     * @throws SetupException
195
     */
196
    public function setMessageLibrary(string $sLibraryName)
197
    {
198
        if(!isset($this->aMessageLibraries[$sLibraryName]))
199
        {
200
            $sMessage = $this->xTranslator->trans('errors.dialog.library',
201
                ['type' => 'message', 'name' => $sLibraryName]);
202
            throw new SetupException($sMessage);
203
        }
204
        $this->sMessageLibrary = $sLibraryName;
205
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
206
207
    /**
208
     * Get the MessageInterface library
209
     *
210
     * @return MessageInterface
211
     */
212
    public function getMessageLibrary(): MessageInterface
213
    {
214
        $sLibraryName = $this->sNextLibrary ?: $this->sMessageLibrary;
215
        return ($sLibraryName) ? $this->di->g($this->aMessageLibraries[$sLibraryName]) : $this->xAlertLibrary;
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->aModalLibraries[$sLibraryName]))
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
241
     */
242
    public function getModalLibrary(): ?ModalInterface
243
    {
244
        $sLibraryName = $this->sNextLibrary ?: $this->sModalLibrary;
245
        return ($sLibraryName) ? $this->di->g($this->aModalLibraries[$sLibraryName]) : null;
246
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
247
248
    /**
249
     * Set the name of the library to use for the next call
250
     *
251
     * @param string $sNextLibrary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
252
     *
253
     * @return void
254
     */
255
    public function setNextLibrary(string $sNextLibrary): void
256
    {
257
        $this->sNextLibrary = $sNextLibrary;
258
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
259
}
260