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

ScriptTrait::confirmCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
/**
4
 * ScriptTrait.php
5
 *
6
 * Provides javascript related commands for the Response
7
 *
8
 * @author Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
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...
12
13
namespace Jaxon\Response\Traits;
14
15
use Jaxon\App\Dialog\DialogManager;
16
use Jaxon\Request\Call\JsCall;
17
use Jaxon\Response\ResponseInterface;
18
use Closure;
19
use JsonSerializable;
20
21
use function Jaxon\jaxon;
22
use function func_get_args;
23
use function array_shift;
24
25
trait ScriptTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait ScriptTrait
Loading history...
26
{
27
     /**
28
     * Add a response command to the array of commands that will be sent to the browser
29
     *
30
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
31
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
32
     *
33
     * @return ResponseInterface
34
     */
35
    abstract public function addCommand(string $sName, array|JsonSerializable $aOptions): ResponseInterface;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
36
37
    /**
38
     * Convert to string
39
     *
40
     * @param mixed $xData
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
41
     *
42
     * @return string
43
     */
44
    abstract protected function str($xData): string;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
45
46
    /**
47
     * @return DialogManager
48
     */
49
    abstract protected function dialog(): DialogManager;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
50
51
    /**
52
     * Add a command to call the specified javascript function with the given (optional) parameters
53
     *
54
     * @param string $sFunc    The name of the function to call
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
55
     *
56
     * @return ResponseInterface
57
     */
58
    public function call(string $sFunc): ResponseInterface
59
    {
60
        $aArgs = func_get_args();
61
        array_shift($aArgs);
62
        return $this->addCommand('script.call', ['func' => $this->str($sFunc),'args' => $aArgs]);
63
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
64
65
    /**
66
     * Response command that prompts user with [ok] [cancel] style message box
67
     *
68
     * The provided closure will be called with a response object as unique parameter.
69
     * If the user clicks cancel, the response commands defined in the closure will be skipped.
70
     *
71
     * @param Closure $fCalls    A closure that defines the commands that can be skipped
72
     * @param string $sQuestion  The question to ask to the user
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
73
     * @param array $aArgs       The arguments for the placeholders in the question
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 7 found
Loading history...
74
     *
75
     * @return ResponseInterface
76
     */
77
    public function confirm(Closure $fCalls, string $sQuestion, array $aArgs = []): ResponseInterface
78
    {
79
        $xResponse = jaxon()->newResponse();
80
        $fCalls($xResponse);
81
        if(($nCommandCount = $xResponse->getCommandCount()) > 0)
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
82
        {
83
            $this->addCommand('script.confirm', [
84
                'count' => $nCommandCount,
85
                'question' => $this->dialog()->confirm($this->str($sQuestion), $aArgs),
86
            ]);
87
88
            // Append the provided commands
89
            $this->appendResponse($xResponse);
0 ignored issues
show
Bug introduced by
It seems like appendResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

89
            $this->/** @scrutinizer ignore-call */ 
90
                   appendResponse($xResponse);
Loading history...
90
        }
91
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Response\Traits\ScriptTrait which is incompatible with the type-hinted return Jaxon\Response\ResponseInterface.
Loading history...
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Add a command to display an alert message to the user
96
     *
97
     * @param string $sMessage    The message to be displayed
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
98
     * @param array $aArgs      The arguments for the placeholders in the message
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...
99
     *
100
     * @return ResponseInterface
101
     */
102
    public function alert(string $sMessage, array $aArgs = []): ResponseInterface
103
    {
104
        $this->addCommand('dialog.message', $this->dialog()->info($this->str($sMessage), $aArgs));
105
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Response\Traits\ScriptTrait which is incompatible with the type-hinted return Jaxon\Response\ResponseInterface.
Loading history...
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Add a command to display a debug message to the user
110
     *
111
     * @param string $sMessage    The message to be displayed
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
112
     *
113
     * @return ResponseInterface
114
     */
115
    public function debug(string $sMessage): ResponseInterface
116
    {
117
        return $this->addCommand('script.debug', ['message' => $this->str($sMessage)]);
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Add a command to ask the browser to navigate to the specified URL
122
     *
123
     * @param string $sURL    The relative or fully qualified URL
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
124
     * @param integer $nDelay    Number of seconds to delay before the redirect occurs
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
125
     *
126
     * @return ResponseInterface
127
     */
128
    public function redirect(string $sURL, int $nDelay = 0): ResponseInterface
129
    {
130
        return $this->addCommand('script.redirect', [
131
            'delay' => $nDelay,
132
            'url' => $this->xPluginManager->getParameterReader()->parseUrl($sURL),
133
        ]);
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Add a command to make Jaxon to pause execution of the response commands,
138
     * returning control to the browser so it can perform other commands asynchronously.
139
     *
140
     * After the specified delay, Jaxon will continue execution of the response commands.
141
     *
142
     * @param integer $tenths    The number of 1/10ths of a second to sleep
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
143
     *
144
     * @return ResponseInterface
145
     */
146
    public function sleep(int $tenths): ResponseInterface
147
    {
148
        return $this->addCommand('script.sleep', ['duration' => $tenths]);
149
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
150
151
    /**
152
     * Add a command to set an event handler on the specified element
153
     * This handler can take custom parameters, and is is executed in a specific context.
154
     *
155
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
156
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
157
     * @param JsCall $xCall    The event handler
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
158
     *
159
     * @return ResponseInterface
160
     */
161
    public function setEventHandler(string $sTarget, string $sEvent, JsCall $xCall): ResponseInterface
162
    {
163
        return $this->addCommand('handler.event.set', [
164
            'id' => $this->str($sTarget),
165
            'event' => $this->str($sEvent),
166
            'func' => $xCall,
167
        ]);
168
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
169
170
    /**
171
     * Add a command to set a click handler on the browser
172
     *
173
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
174
     * @param JsCall $xCall    The event handler
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
175
     *
176
     * @return ResponseInterface
177
     */
178
    public function onClick(string $sTarget, JsCall $xCall): ResponseInterface
179
    {
180
        return $this->setEventHandler($sTarget, 'onclick', $xCall);
181
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
182
183
    /**
184
     * Add a command to add an event handler on the specified element
185
     * This handler can take custom parameters, and is is executed in a specific context.
186
     *
187
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
188
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
189
     * @param JsCall $xCall    The event handler
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
190
     *
191
     * @return ResponseInterface
192
     */
193
    public function addEventHandler(string $sTarget, string $sEvent, JsCall $xCall): ResponseInterface
194
    {
195
        return $this->addCommand('handler.event.add', [
196
            'id' => $this->str($sTarget),
197
            'event' => $this->str($sEvent),
198
            'func' => $xCall,
199
        ]);
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     * Add a command to install an event handler on the specified element
204
     *
205
     * You can add more than one event handler to an element's event using this method.
206
     *
207
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
208
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
209
     * @param string $sHandler    The name of the javascript function to call when the event is fired
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
210
     *
211
     * @return ResponseInterface
212
     */
213
    public function addHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface
214
    {
215
        return $this->addCommand('handler.add', [
216
            'id' => $this->str($sTarget),
217
            'event' => $this->str($sEvent),
218
            'func' => $this->str($sHandler),
219
        ]);
220
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
221
222
    /**
223
     * Add a command to remove an event handler from an element
224
     *
225
     * @param string $sTarget    The id of the element
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
226
     * @param string $sEvent    The name of the event
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
227
     * @param string $sHandler    The name of the javascript function called when the event is fired
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
228
     *
229
     * @return ResponseInterface
230
     */
231
    public function removeHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface
232
    {
233
        return $this->addCommand('handler.remove', [
234
            'id' => $this->str($sTarget),
235
            'event' => $this->str($sEvent),
236
            'func' => $this->str($sHandler),
237
        ]);
238
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
239
}
240