Passed
Push — v5.x ( 3c34c5...4cf44b )
by Thierry
02:14
created

DialogManager::error()   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
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * DialogManager.php
5
 *
6
 * Facade for dialogs functions.
7
 *
8
 * @author Thierry Feuzeu <[email protected]>
9
 * @copyright 2024 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;
15
16
use Jaxon\App\Dialog\Library\DialogLibraryManager;
17
use Jaxon\Request\Call\Parameter;
18
19
use function array_map;
20
21
class DialogManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DialogManager
Loading history...
22
{
23
    /**
24
     * @var DialogLibraryManager
25
     */
26
    protected $xDialogLibraryManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
27
28
    /**
29
     * The next message title
30
     *
31
     * @var string
32
     */
33
    private $sTitle = '';
34
35
    /**
36
     * The constructor
37
     *
38
     * @param DialogLibraryManager $xDialogLibraryManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
39
     */
40
    public function __construct(DialogLibraryManager $xDialogLibraryManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
41
    {
42
        $this->xDialogLibraryManager = $xDialogLibraryManager;
43
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
44
45
    /**
46
     * @param string $sStr
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
47
     * @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...
48
     *
49
     * @return array
50
     */
51
    private function phrase(string $sStr, array $aArgs = []): array
52
    {
53
        return [
54
            'str' => $sStr,
55
            'args' => array_map(fn($xArg) => Parameter::make($xArg), $aArgs),
56
        ];
57
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
58
59
    /**
60
     * Set the title of the next message.
61
     *
62
     * @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...
63
     *
64
     * @return self
65
     */
66
    public function title(string $sTitle)
67
    {
68
        $this->sTitle = $sTitle;
69
70
        return $this;
71
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
72
73
    /**
74
     * Print an alert message.
75
     *
76
     * @param string $sType     The type of the message
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 5 found
Loading history...
77
     * @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...
78
     * @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...
79
     *
80
     * @return array
81
     */
82
    private function alert(string $sType, string $sMessage, array $aArgs): array
83
    {
84
        $sTitle = $this->sTitle;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
85
        $this->sTitle = '';
86
87
        return [
88
            'lib' => $this->xDialogLibraryManager->getMessageLibrary()->getName(),
89
            'type' => $sType,
90
            'content' => [
91
                'title' => $sTitle,
92
                'phrase' => $this->phrase($sMessage, $aArgs),
93
            ],
94
        ];
95
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
96
97
    /**
98
     * Show a success message.
99
     *
100
     * @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...
101
     * @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...
102
     *
103
     * @return array
104
     */
105
    public function success(string $sMessage, array $aArgs = []): array
106
    {
107
        return $this->alert('success', $sMessage, $aArgs);
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
109
110
    /**
111
     * Show an information message.
112
     *
113
     * @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...
114
     * @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...
115
     *
116
     * @return array
117
     */
118
    public function info(string $sMessage, array $aArgs = []): array
119
    {
120
        return $this->alert('info', $sMessage, $aArgs);
121
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
122
123
    /**
124
     * Show a warning message.
125
     *
126
     * @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...
127
     * @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...
128
     *
129
     * @return array
130
     */
131
    public function warning(string $sMessage, array $aArgs = []): array
132
    {
133
        return $this->alert('warning', $sMessage, $aArgs);
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Show an error message.
138
     *
139
     * @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...
140
     * @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...
141
     *
142
     * @return array
143
     */
144
    public function error(string $sMessage, array $aArgs = []): array
145
    {
146
        return $this->alert('error', $sMessage, $aArgs);
147
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
148
149
    /**
150
     * Show a modal dialog.
151
     *
152
     * @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...
153
     * @param string $sContent The content of the dialog
154
     * @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...
155
     * @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...
156
     *
157
     * Each button is an array with the following entries:
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 24 spaces but found 1
Loading history...
158
     * - title: the text to be printed in the button
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 24 spaces but found 1
Loading history...
159
     * - class: the CSS class of the button
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 24 spaces but found 1
Loading history...
160
     * - click: the javascript function to be called when the button is clicked
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 24 spaces but found 1
Loading history...
161
     * If the click value is set to "close", then the button closes the dialog.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 24 spaces but found 1
Loading history...
162
     *
163
     * The content of the $aOptions depends on the javascript library in use.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 24 spaces but found 1
Loading history...
164
     * Check their specific documentation for more information.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 24 spaces but found 1
Loading history...
165
     *
166
     * @return array
167
     */
168
    public function show(string $sTitle, string $sContent, array $aButtons, array $aOptions = []): array
169
    {
170
        return [
171
            'lib' => $this->xDialogLibraryManager->getModalLibrary()->getName(),
172
            'dialog' => [
173
                'title' => $sTitle,
174
                'content' => $sContent,
175
                'buttons' => $aButtons,
176
                'options' => $aOptions,
177
            ],
178
        ];
179
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
180
181
    /**
182
     * Hide the modal dialog.
183
     *
184
     * @return array
185
     */
186
    public function hide(): array
187
    {
188
        return [
189
            'lib' => $this->xDialogLibraryManager->getModalLibrary()->getName(),
190
        ];
191
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
192
193
    /**
194
     * Add a confirm question to a function call.
195
     *
196
     * @param string $sQuestion
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
197
     * @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...
198
     *
199
     * @return array
200
     */
201
    public function confirm(string $sQuestion, array $aArgs = []): array
202
    {
203
        return [
204
            'lib' => $this->xDialogLibraryManager->getQuestionLibrary()->getName(),
205
            'phrase' => $this->phrase($sQuestion, $aArgs),
206
        ];
207
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
208
}
209