Passed
Push — master ( 888408...eb1365 )
by Thierry
02:11
created

Response::getCommandData()   B

Complexity

Conditions 11
Paths 7

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 15
c 0
b 0
f 0
nc 7
nop 2
dl 0
loc 29
rs 7.3166

1 Method

Rating   Name   Duplication   Size   Complexity  
A Response::addCommand() 0 10 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Response.php - The Jaxon Response
5
 *
6
 * This class collects commands to be sent back to the browser in response to a jaxon request.
7
 * Commands are encoded and packaged in json format.
8
 *
9
 * Common commands include:
10
 * - <Response->assign>: Assign a value to an element's attribute.
11
 * - <Response->append>: Append a value on to an element's attribute.
12
 * - <Response->script>: Execute a portion of javascript code.
13
 * - <Response->call>: Execute an existing javascript function.
14
 * - <Response->alert>: Display an alert dialog to the user.
15
 *
16
 * Elements are identified by the value of the HTML id attribute.
17
 *
18
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
19
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
20
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
21
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
22
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
23
 * @author Thierry Feuzeu <[email protected]>
24
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
25
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
26
 * @copyright 2016 Thierry Feuzeu <[email protected]>
27
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
28
 * @link https://github.com/jaxon-php/jaxon-core
29
 */
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...
30
31
namespace Jaxon\Response;
32
33
use Jaxon\Plugin\PluginManager;
34
use Jaxon\Plugin\ResponsePlugin;
35
use Jaxon\Response\Plugin\DataBag\DataBagContext;
36
use Jaxon\Response\Plugin\JQuery\DomSelector;
37
use Jaxon\Utils\Translation\Translator;
38
use Jaxon\Exception\RequestException;
39
40
use function array_keys;
41
use function array_map;
42
use function array_merge;
43
use function count;
44
use function is_array;
45
use function is_integer;
46
use function json_encode;
47
use function trim;
48
49
class Response extends AbstractResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Response
Loading history...
50
{
51
    use Traits\DomTrait;
52
    use Traits\JsTrait;
53
    use Traits\DomTreeTrait;
54
55
    /**
56
     * @var Translator
57
     */
58
    protected $xTranslator;
59
60
    /**
61
     * @var PluginManager
62
     */
63
    protected $xPluginManager;
64
65
    /**
66
     * The commands that will be sent to the browser in the response
67
     *
68
     * @var array
69
     */
70
    protected $aCommands = [];
71
72
    /**
73
     * A string, array or integer value to be returned to the caller when using 'synchronous' mode requests.
74
     * See <jaxon->setMode> for details.
75
     *
76
     * @var mixed
77
     */
78
    protected $xReturnValue;
79
80
    /**
81
     * The constructor
82
     *
83
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @param PluginManager $xPluginManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     */
86
    public function __construct(Translator $xTranslator, PluginManager $xPluginManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
87
    {
88
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
89
        $this->xPluginManager = $xPluginManager;
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Create a new Jaxon response object
94
     *
95
     * @return Response
96
     */
97
    public function newResponse(): Response
98
    {
99
        return new Response($this->xTranslator, $this->xPluginManager);
100
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
101
102
    /**
103
     * Get the content type, which is always set to 'application/json'
104
     *
105
     * @return string
106
     */
107
    public function getContentType(): string
108
    {
109
        return 'application/json';
110
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
111
112
    /**
113
     * Provides access to registered response plugins
114
     *
115
     * Pass the plugin name as the first argument and the plugin object will be returned.
116
     *
117
     * @param string $sName    The name of the plugin
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
118
     *
119
     * @return null|ResponsePlugin
120
     */
121
    public function plugin(string $sName): ?ResponsePlugin
122
    {
123
        return $this->xPluginManager->getResponsePlugin($sName, $this);
124
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
125
126
    /**
127
     * Magic PHP function
128
     *
129
     * Used to permit plugins to be called as if they were native members of the Response instance.
130
     *
131
     * @param string $sPluginName    The name of the plugin
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
132
     *
133
     * @return null|ResponsePlugin
134
     */
135
    public function __get(string $sPluginName)
136
    {
137
        return $this->plugin($sPluginName);
138
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
139
140
    /**
141
     * Create a JQuery DomSelector, and link it to the current response.
142
     *
143
     * This is a shortcut to the JQuery plugin.
144
     *
145
     * @param string $sPath    The jQuery selector path
146
     * @param string $sContext    A context associated to the selector
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
147
     *
148
     * @return DomSelector
149
     */
150
    public function jq(string $sPath = '', string $sContext = ''): DomSelector
151
    {
152
        return $this->plugin('jquery')->selector($sPath, $sContext);
0 ignored issues
show
Bug introduced by
The method selector() does not exist on Jaxon\Plugin\ResponsePlugin. It seems like you code against a sub-type of Jaxon\Plugin\ResponsePlugin such as Jaxon\Response\Plugin\JQuery\JQueryPlugin. ( Ignorable by Annotation )

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

152
        return $this->plugin('jquery')->/** @scrutinizer ignore-call */ selector($sPath, $sContext);
Loading history...
153
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
154
155
    /**
156
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
157
     *
158
     * @return DataBagContext
159
     */
160
    public function bag(string $sName): DataBagContext
161
    {
162
        return $this->plugin('bags')->bag($sName);;
0 ignored issues
show
Bug introduced by
The method bag() does not exist on Jaxon\Plugin\ResponsePlugin. It seems like you code against a sub-type of Jaxon\Plugin\ResponsePlugin such as Jaxon\Response\Plugin\DataBag\DataBagPlugin. ( Ignorable by Annotation )

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

162
        return $this->plugin('bags')->/** @scrutinizer ignore-call */ bag($sName);;
Loading history...
163
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
164
165
    /**
166
     * Add a response command to the array of commands that will be sent to the browser
167
     *
168
     * @param array $aAttributes    Associative array of attributes that will describe the command
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
169
     * @param mixed $mData    The data to be associated with this command
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 4 found
Loading history...
170
     *
171
     * @return Response
172
     */
173
    public function addCommand(array $aAttributes, $mData): Response
174
    {
175
        $aAttributes = array_map(function($xAttribute) {
176
            return is_integer($xAttribute) ? $xAttribute : trim((string)$xAttribute, " \t");
177
        }, $aAttributes);
178
179
        $aAttributes['data'] = $mData;
180
        $this->aCommands[] = $aAttributes;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
181
182
        return $this;
183
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
184
185
    /**
186
     * Add a response command to the array of commands that will be sent to the browser
187
     *
188
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
189
     * @param array $aAttributes    Associative array of attributes that will describe the command
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
190
     * @param mixed $mData    The data to be associated with this command
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
191
     * @param bool $bRemoveEmpty    If true, remove empty attributes
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
192
     *
193
     * @return Response
194
     */
195
    protected function _addCommand(string $sName, array $aAttributes, $mData, bool $bRemoveEmpty = false): Response
196
    {
197
        $mData = is_array($mData) ? array_map(function($sData) {
198
            return trim((string)$sData, " \t\n");
199
        }, $mData) : trim((string)$mData, " \t\n");
200
201
        if($bRemoveEmpty)
202
        {
203
            foreach(array_keys($aAttributes) as $sAttr)
204
            {
205
                if($aAttributes[$sAttr] === '')
206
                {
207
                    unset($aAttributes[$sAttr]);
208
                }
209
            }
210
        }
211
212
        $aAttributes['cmd'] = $sName;
213
        return $this->addCommand($aAttributes, $mData);
214
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
215
216
    /**
217
     * Clear all the commands already added to the response
218
     *
219
     * @return Response
220
     */
221
    public function clearCommands(): Response
222
    {
223
        $this->aCommands = [];
224
        return $this;
225
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
226
227
    /**
228
     * Add a response command that is generated by a plugin
229
     *
230
     * @param ResponsePlugin $xPlugin    The plugin object
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
231
     * @param array $aAttributes    The attributes for this response command
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
232
     * @param mixed $mData    The data to be sent with this command
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
233
     *
234
     * @return Response
235
     */
236
    public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, $mData): Response
237
    {
238
        $aAttributes['plg'] = $xPlugin->getName();
239
        return $this->addCommand($aAttributes, $mData);
240
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
241
242
    /**
243
     * Merge the response commands from the specified <Response> object with
244
     * the response commands in this <Response> object
245
     *
246
     * @param Response|array $mCommands    The <Response> object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
247
     * @param bool $bBefore    Add the new commands to the beginning of the list
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
248
     *
249
     * @return void
250
     * @throws RequestException
251
     */
252
    public function appendResponse($mCommands, bool $bBefore = false)
253
    {
254
        if($mCommands instanceof Response)
255
        {
256
            $this->xReturnValue = $mCommands->xReturnValue;
257
            $aCommands = $mCommands->aCommands;
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...
258
        }
259
        elseif(is_array($mCommands))
0 ignored issues
show
introduced by
The condition is_array($mCommands) is always true.
Loading history...
260
        {
261
            $aCommands = $mCommands;
262
        }
263
        else
264
        {
265
            throw new RequestException($this->xTranslator->trans('errors.response.data.invalid'));
266
        }
267
268
        $this->aCommands = ($bBefore) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
269
            array_merge($aCommands, $this->aCommands) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
270
            array_merge($this->aCommands, $aCommands);
271
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
272
273
    /**
274
     * Get the commands in the response
275
     *
276
     * @return array
277
     */
278
    public function getCommands(): array
279
    {
280
        return $this->aCommands;
281
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
282
283
    /**
284
     * Get the number of commands in the response
285
     *
286
     * @return int
287
     */
288
    public function getCommandCount(): int
289
    {
290
        return count($this->aCommands);
291
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
292
293
    /**
294
     * Stores a value that will be passed back as part of the response
295
     *
296
     * When making synchronous requests, the calling javascript can obtain this value
297
     * immediately as the return value of the <jaxon.call> javascript function
298
     *
299
     * @param mixed $value    Any value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
300
     *
301
     * @return Response
302
     */
303
    public function setReturnValue($value): Response
304
    {
305
        $this->xReturnValue = $value;
306
        return $this;
307
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
308
309
    /**
310
     * Return the output, generated from the commands added to the response, that will be sent to the browser
311
     *
312
     * @return string
313
     */
314
    public function getOutput(): string
315
    {
316
        $aResponse = ['jxnobj' => []];
317
        if(($this->xReturnValue))
318
        {
319
            $aResponse['jxnrv'] = $this->xReturnValue;
320
        }
321
        foreach($this->aCommands as $xCommand)
322
        {
323
            $aResponse['jxnobj'][] = $xCommand;
324
        }
325
326
        return json_encode($aResponse);
327
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
328
}
329