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

DialogPlugin::getSubdir()   A

Complexity

Conditions 1
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 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * DialogPlugin.php - ModalInterface, message and question dialogs for Jaxon.
5
 *
6
 * Show modal, message and question dialogs with various javascript libraries
7
 * based on user settings.
8
 *
9
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
10
 * @author Thierry Feuzeu <[email protected]>
11
 * @copyright 2016 Thierry Feuzeu <[email protected]>
12
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
13
 * @link https://github.com/jaxon-php/jaxon-dialogs
14
 */
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...
15
16
namespace Jaxon\Plugin\Response\Dialog;
17
18
use Jaxon\App\Dialog\Library\DialogLibraryManager;
19
use Jaxon\Exception\SetupException;
20
use Jaxon\Plugin\ResponsePlugin;
21
use Jaxon\Response\ResponseInterface;
22
23
use function array_reduce;
24
use function trim;
25
26
class DialogPlugin extends ResponsePlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DialogPlugin
Loading history...
27
{
28
    /**
29
     * @const The plugin name
30
     */
31
    const NAME = 'dialog';
32
33
    /**
34
     * @var DialogLibraryManager
35
     */
36
    protected $xLibraryManager;
37
38
    /**
39
     * @var array
40
     */
41
    protected $aLibraries = null;
42
43
    /**
44
     * The constructor
45
     *
46
     * @param DialogLibraryManager $xLibraryManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
47
     */
48
    public function __construct(DialogLibraryManager $xLibraryManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
49
    {
50
        $this->xLibraryManager = $xLibraryManager;
51
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
52
53
    /**
54
     * @inheritDoc
55
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
56
    public function getName(): string
57
    {
58
        return self::NAME;
59
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
60
61
    /**
62
     * @inheritDoc
63
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
64
    public function getHash(): string
65
    {
66
        // The version number is used as hash
67
        return '4.0.0';
68
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
69
70
    public function getUri(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getUri()
Loading history...
71
    {
72
        return '';
73
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
74
75
    public function getSubdir(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getSubdir()
Loading history...
76
    {
77
        return '';
78
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
79
80
    public function getVersion(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getVersion()
Loading history...
81
    {
82
        return '';
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xResponse should have a doc-comment as per coding-style.
Loading history...
86
     * @inheritDoc
87
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
88
    public function setResponse(ResponseInterface $xResponse)
89
    {
90
        parent::setResponse($xResponse);
91
92
        // Hack the setResponse() method, to set the default libraries on each access to this plugin.
93
        $this->xLibraryManager->setNextLibrary('');
94
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
95
96
    /**
97
     * Set the library to use for the next call.
98
     *
99
     * @param string $sLibrary The name of the library
100
     *
101
     * @return DialogPlugin
102
     */
103
    public function with(string $sLibrary): DialogPlugin
104
    {
105
        $this->xLibraryManager->setNextLibrary($sLibrary);
106
        return $this;
107
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
108
109
    /**
110
     * @return array
111
     */
112
    private function getLibraries(): array
113
    {
114
        if($this->aLibraries === null)
115
        {
116
            $this->aLibraries = $this->xLibraryManager->getLibraries();
117
        }
118
        return $this->aLibraries;
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * @inheritDoc
123
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
124
    public function getJs(): string
125
    {
126
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
127
            return $sCode . $xLibrary->getJs() . "\n\n";
128
        }, '');
129
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
130
131
    /**
132
     * @inheritDoc
133
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
134
    public function getCss(): string
135
    {
136
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
137
            return $sCode . trim($xLibrary->getCss()) . "\n\n";
138
        }, '');
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
    /**
142
     * @inheritDoc
143
     * @throws SetupException
144
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
145
    public function getScript(): string
146
    {
147
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
148
            return $sCode . trim($xLibrary->getScript()) . "\n\n";
149
        }, "jaxon.dialogs = {};\n");
150
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
151
152
    /**
153
     * @inheritDoc
154
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
155
    public function getReadyScript(): string
156
    {
157
        return array_reduce($this->getLibraries(), function($sCode, $xLibrary) {
158
            return $sCode . trim($xLibrary->getReadyScript()) . "\n\n";
159
        }, '');
160
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
161
162
    /**
163
     * Show a modal dialog.
164
     *
165
     * @param string $sTitle The title of the dialog
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
166
     * @param string $sContent The content of the dialog
167
     * @param array $aButtons The buttons of the dialog
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
168
     * @param array $aOptions The options of the dialog
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
169
     *
170
     * @return void
171
     */
172
    public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = [])
173
    {
174
        // Show the modal dialog
175
        $this->addCommand('dialog.modal.show',
176
            $this->xLibraryManager->show($sTitle, $sContent, $aButtons, $aOptions));
177
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
178
179
    /**
180
     * Hide the modal dialog.
181
     *
182
     * @return void
183
     */
184
    public function hide()
185
    {
186
        // Hide the modal dialog
187
        $this->addCommand('dialog.modal.hide', $this->xLibraryManager->hide());
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
189
190
    /**
191
     * Set the title of the next message.
192
     *
193
     * @param string $sTitle     The title of the message
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
194
     *
195
     * @return DialogPlugin
196
     */
197
    public function title(string $sTitle): DialogPlugin
198
    {
199
        $this->xLibraryManager->title($sTitle);
200
        return $this;
201
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
202
203
    /**
204
     * Show a success message.
205
     *
206
     * @param string $sMessage  The text of the message
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
207
     * @param array $aArgs      The message arguments
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 6 found
Loading history...
208
     *
209
     * @return void
210
     */
211
    public function success(string $sMessage, array $aArgs = [])
212
    {
213
        $this->addCommand('dialog.message', $this->xLibraryManager->success($sMessage, $aArgs));
214
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
215
216
    /**
217
     * Show an information message.
218
     *
219
     * @param string $sMessage  The text of the message
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
220
     * @param array $aArgs      The message arguments
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 6 found
Loading history...
221
     *
222
     * @return void
223
     */
224
    public function info(string $sMessage, array $aArgs = [])
225
    {
226
        $this->addCommand('dialog.message', $this->xLibraryManager->info($sMessage, $aArgs));
227
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
228
229
    /**
230
     * Show a warning message.
231
     *
232
     * @param string $sMessage  The text of the message
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
233
     * @param array $aArgs      The message arguments
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 6 found
Loading history...
234
     *
235
     * @return void
236
     */
237
    public function warning(string $sMessage, array $aArgs = [])
238
    {
239
        $this->addCommand('dialog.message', $this->xLibraryManager->warning($sMessage, $aArgs));
240
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
241
242
    /**
243
     * Show an error message.
244
     *
245
     * @param string $sMessage  The text of the message
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
246
     * @param array $aArgs      The message arguments
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 6 found
Loading history...
247
     *
248
     * @return void
249
     */
250
    public function error(string $sMessage, array $aArgs = [])
251
    {
252
        $this->addCommand('dialog.message', $this->xLibraryManager->error($sMessage, $aArgs));
253
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
254
}
255