Passed
Push — master ( b34d93...aea763 )
by Thierry
03:02 queued 24s
created

DialogPlugin::success()   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 2
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-dialogs
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-dialogs" is not valid; consider "Jaxondialogs" 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\Config\ConfigManager;
19
use Jaxon\Di\Container;
20
use Jaxon\Plugin\ResponsePlugin;
21
use Jaxon\Response\Response;
22
use Jaxon\Ui\Dialog\Library\DialogLibraryManager;
23
use Jaxon\Ui\Dialog\MessageInterface;
24
use Jaxon\Ui\Dialog\ModalInterface;
25
26
use function array_reduce;
27
use function trim;
28
29
class DialogPlugin extends ResponsePlugin implements ModalInterface, MessageInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DialogPlugin
Loading history...
30
{
31
    /**
32
     * @const The plugin name
33
     */
34
    const NAME = 'dialog';
35
36
    /**
37
     * Dependency Injection manager
38
     *
39
     * @var Container
40
     */
41
    protected $di;
42
43
    /**
44
     * @var DialogLibraryManager
45
     */
46
    protected $xLibraryManager;
47
48
    /**
49
     * @var ConfigManager
50
     */
51
    protected $xConfigManager;
52
53
    /**
54
     * @var array
55
     */
56
    protected $aLibraries = [];
57
58
    /**
59
     * The constructor
60
     *
61
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
62
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
63
     * @param DialogLibraryManager $xLibraryManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
64
     */
65
    public function __construct(Container $di, ConfigManager $xConfigManager, DialogLibraryManager $xLibraryManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
66
    {
67
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 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...
68
        $this->xConfigManager = $xConfigManager;
69
        $this->xLibraryManager = $xLibraryManager;
70
71
        $aLibraries = $this->xConfigManager->getOption('dialogs.libraries', []);
72
        foreach($aLibraries as $sClassName => $sName)
73
        {
74
            $this->registerLibrary($sClassName, $sName);
75
        }
76
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
77
78
    /**
79
     * @inheritDoc
80
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
81
    public function getName(): string
82
    {
83
        return self::NAME;
84
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
85
86
    /**
87
     * @inheritDoc
88
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
89
    public function getHash(): string
90
    {
91
        return '4.0.0'; // The version number is used as hash
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Register a javascript dialog library adapter.
96
     *
97
     * @param string $sClassName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
98
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
99
     *
100
     * @return void
101
     */
102
    public function registerLibrary(string $sClassName, string $sName)
103
    {
104
        $this->aLibraries[] = $sName;
105
        $this->di->registerDialogLibrary($sClassName, $sName);
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * @return void
110
     */
111
    protected function setDefaultLibraries()
112
    {
113
        // Set the default modal library
114
        if(($sName = $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...
115
        {
116
            $this->xLibraryManager->setModalLibrary($sName);
117
        }
118
        // Set the default message library
119
        if(($sName = $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...
120
        {
121
            $this->xLibraryManager->setMessageLibrary($sName);
122
        }
123
        // Set the default question library
124
        if(($sName = $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...
125
        {
126
            $this->xLibraryManager->setQuestionLibrary($sName);
127
        }
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xResponse should have a doc-comment as per coding-style.
Loading history...
131
     * @inheritDoc
132
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
133
    public function setResponse(Response $xResponse)
134
    {
135
        parent::setResponse($xResponse);
136
137
        // Hack the setResponse() method, to set the default libraries on each access to this plugin.
138
        $this->setDefaultLibraries();
139
        $this->xLibraryManager->setNextLibrary('');
140
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
141
142
    /**
143
     * Set the library to use for the next call.
144
     *
145
     * @param string $sLibrary The name of the library
146
     *
147
     * @return DialogPlugin
148
     */
149
    public function with(string $sLibrary): DialogPlugin
150
    {
151
        $this->xLibraryManager->setNextLibrary($sLibrary);
152
        return $this;
153
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
154
155
    /**
156
     * Get the library adapter to use for modals.
157
     *
158
     * @return ModalInterface|null
159
     */
160
    protected function getModalLibrary(): ?ModalInterface
161
    {
162
        $xLibrary = $this->xLibraryManager->getModalLibrary();
163
        $xLibrary->setResponse($this->xResponse);
0 ignored issues
show
Bug introduced by
The method setResponse() does not exist on Jaxon\Ui\Dialog\ModalInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jaxon\Ui\Dialog\ModalInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
        $xLibrary->/** @scrutinizer ignore-call */ 
164
                   setResponse($this->xResponse);
Loading history...
164
        return $xLibrary;
165
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
166
167
    /**
168
     * Get the library adapter to use for messages.
169
     *
170
     * @return MessageInterface|null
171
     */
172
    protected function getMessageLibrary(): ?MessageInterface
173
    {
174
        $xLibrary = $this->xLibraryManager->getMessageLibrary();
175
        $xLibrary->setResponse($this->xResponse);
176
        // By default, always add commands to the response
177
        $xLibrary->setReturnCode(false);
178
        return $xLibrary;
179
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
180
181
    /**
182
     * @inheritDoc
183
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
184
    public function getJs(): string
185
    {
186
        return array_reduce($this->aLibraries, function($sCode, $sName) {
187
            $xLibrary = $this->di->g($sName);
188
            return $sCode . $xLibrary->getJs() . "\n\n";
189
        }, '');
190
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
191
192
    /**
193
     * @inheritDoc
194
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
195
    public function getCss(): string
196
    {
197
        return array_reduce($this->aLibraries, function($sCode, $sName) {
198
            $xLibrary = $this->di->g($sName);
199
            return $sCode . trim($xLibrary->getCss()) . "\n\n";
200
        }, '');
201
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
202
203
    /**
204
     * @inheritDoc
205
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
206
    public function getScript(): string
207
    {
208
        // The default scripts need to be set in the js code.
209
        $this->setDefaultLibraries();
210
        return array_reduce($this->aLibraries, function($sCode, $sName) {
211
            $xLibrary = $this->di->g($sName);
212
            return $sCode . trim($xLibrary->getScript()) . "\n\n";
213
        }, "jaxon.dialogs = {};\n");
214
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
215
216
    /**
217
     * @inheritDoc
218
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
219
    public function getReadyScript(): string
220
    {
221
        return array_reduce($this->aLibraries, function($sCode, $sName) {
222
            $xLibrary = $this->di->g($sName);
223
            return $sCode . trim($xLibrary->getReadyScript()) . "\n\n";
224
        }, '');
225
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
226
227
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sTitle should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sContent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $aButtons should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $aOptions should have a doc-comment as per coding-style.
Loading history...
228
     * @inheritDoc
229
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
230
    public function show(string $sTitle, string $sContent, array $aButtons = [], array $aOptions = [])
231
    {
232
        $this->getModalLibrary()->show($sTitle, $sContent, $aButtons, $aOptions);
233
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
234
235
    /**
236
     * @inheritDoc
237
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
238
    public function hide()
239
    {
240
        $this->getModalLibrary()->hide();
241
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
242
243
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sTitle should have a doc-comment as per coding-style.
Loading history...
244
     * @inheritDoc
245
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
246
    public function success(string $sMessage, string $sTitle = ''): string
247
    {
248
        return $this->getMessageLibrary()->success($sMessage, $sTitle);
249
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
250
251
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sTitle should have a doc-comment as per coding-style.
Loading history...
252
     * @inheritDoc
253
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
254
    public function info(string $sMessage, string $sTitle = ''): string
255
    {
256
        return $this->getMessageLibrary()->info($sMessage, $sTitle);
257
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
258
259
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sTitle should have a doc-comment as per coding-style.
Loading history...
260
     * @inheritDoc
261
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
262
    public function warning(string $sMessage, string $sTitle = ''): string
263
    {
264
        return $this->getMessageLibrary()->warning($sMessage, $sTitle);
265
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
266
267
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sTitle should have a doc-comment as per coding-style.
Loading history...
268
     * @inheritDoc
269
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
270
    public function error(string $sMessage, string $sTitle = ''): string
271
    {
272
        return $this->getMessageLibrary()->error($sMessage, $sTitle);
273
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
274
}
275