Passed
Push — master ( 41d5a5...e82656 )
by Thierry
03:02
created

Response::appendResponse()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 5
b 0
f 0
nc 5
nop 2
dl 0
loc 19
rs 9.9332
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
 * @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 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...
11
 * @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...
12
 * @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...
13
 * @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...
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @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...
16
 * @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...
17
 * @copyright 2016 Thierry Feuzeu <[email protected]>
18
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19
 * @link https://github.com/jaxon-php/jaxon-core
20
 */
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...
21
22
namespace Jaxon\Response;
23
24
use Jaxon\Plugin\Manager\PluginManager;
25
use Jaxon\Plugin\ResponsePlugin;
26
use Jaxon\Request\Handler\ParameterReader;
27
use Jaxon\Response\Plugin\DataBag\DataBagContext;
28
use Jaxon\Response\Plugin\JQuery\DomSelector;
29
use Jaxon\Utils\Translation\Translator;
30
31
use function array_filter;
32
use function array_map;
33
use function is_array;
34
use function is_integer;
35
use function json_encode;
36
use function trim;
37
38
class Response implements ResponseInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Response
Loading history...
39
{
40
    use Traits\CommandTrait;
41
    use Traits\DomTrait;
42
    use Traits\JsTrait;
43
44
    /**
45
     * @var Translator
46
     */
47
    protected $xTranslator;
48
49
    /**
50
     * @var PluginManager
51
     */
52
    protected $xPluginManager;
53
54
    /**
55
     * The parameter reader
56
     *
57
     * @var ParameterReader
58
     */
59
    protected $xParameterReader;
60
61
    /**
62
     * The constructor
63
     *
64
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
65
     * @param PluginManager $xPluginManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
66
     * @param ParameterReader $xParameterReader
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
67
     */
68
    public function __construct(Translator $xTranslator, PluginManager $xPluginManager, ParameterReader $xParameterReader)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
69
    {
70
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
71
        $this->xPluginManager = $xPluginManager;
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...
72
        $this->xParameterReader = $xParameterReader;
73
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
74
75
    /**
76
     * @inheritDoc
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public function getContentType(): string
79
    {
80
        return 'application/json';
81
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
82
83
    /**
84
     * @inheritDoc
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    public function getOutput(): string
87
    {
88
        return json_encode(['jxnobj' => $this->aCommands]);
89
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
90
91
    /**
92
     * Provides access to registered response plugins
93
     *
94
     * Pass the plugin name as the first argument and the plugin object will be returned.
95
     *
96
     * @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...
97
     *
98
     * @return null|ResponsePlugin
99
     */
100
    public function plugin(string $sName): ?ResponsePlugin
101
    {
102
        return $this->xPluginManager->getResponsePlugin($sName, $this);
103
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
104
105
    /**
106
     * Magic PHP function
107
     *
108
     * Used to permit plugins to be called as if they were native members of the Response instance.
109
     *
110
     * @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...
111
     *
112
     * @return null|ResponsePlugin
113
     */
114
    public function __get(string $sPluginName)
115
    {
116
        return $this->plugin($sPluginName);
117
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
118
119
    /**
120
     * Create a JQuery DomSelector, and link it to the current response.
121
     *
122
     * This is a shortcut to the JQuery plugin.
123
     *
124
     * @param string $sPath    The jQuery selector path
125
     * @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...
126
     *
127
     * @return DomSelector
128
     */
129
    public function jq(string $sPath = '', string $sContext = ''): DomSelector
130
    {
131
        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

131
        return $this->plugin('jquery')->/** @scrutinizer ignore-call */ selector($sPath, $sContext);
Loading history...
132
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
133
134
    /**
135
     * Get the databag with a given name
136
     *
137
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     *
139
     * @return DataBagContext
140
     */
141
    public function bag(string $sName): DataBagContext
142
    {
143
        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

143
        return $this->plugin('bags')->/** @scrutinizer ignore-call */ bag($sName);;
Loading history...
144
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
145
146
    /**
147
     * Add a response command to the array of commands that will be sent to the browser
148
     *
149
     * @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...
150
     * @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...
151
     *
152
     * @return Response
153
     */
154
    public function addCommand(array $aAttributes, $mData): Response
155
    {
156
        $aAttributes = array_map(function($xAttribute) {
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 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...
157
            return is_integer($xAttribute) ? $xAttribute : trim((string)$xAttribute, " \t");
158
        }, $aAttributes);
159
        $aAttributes['data'] = $mData;
160
        $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...
161
162
        return $this;
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 string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
169
     * @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 type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
170
     * @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...
171
     * @param bool $bRemoveEmpty    If true, remove empty attributes
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
172
     *
173
     * @return Response
174
     */
175
    protected function _addCommand(string $sName, array $aAttributes, $mData, bool $bRemoveEmpty = false): Response
176
    {
177
        $mData = is_array($mData) ? array_map(function($sData) {
178
            return trim((string)$sData, " \t\n");
179
        }, $mData) : trim((string)$mData, " \t\n");
180
        if($bRemoveEmpty)
181
        {
182
            $aAttributes = array_filter($aAttributes, function($xValue) {
183
                return $xValue === '';
184
            });
185
        }
186
        $aAttributes['cmd'] = $sName;
187
        return $this->addCommand($aAttributes, $mData);
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
189
190
    /**
191
     * Add a response command that is generated by a plugin
192
     *
193
     * @param ResponsePlugin $xPlugin    The plugin object
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
194
     * @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...
195
     * @param mixed $mData    The data to be sent with this command
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter name; 4 found
Loading history...
196
     *
197
     * @return Response
198
     */
199
    public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, $mData): Response
200
    {
201
        $aAttributes['plg'] = $xPlugin->getName();
202
        return $this->addCommand($aAttributes, $mData);
203
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
204
}
205