Passed
Push — master ( 6f7a23...2cf817 )
by Thierry
02:29
created

Response::toPsr()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\Di\Container;
25
use Jaxon\Plugin\Manager\PluginManager;
26
use Jaxon\Plugin\Response\DataBag\DataBagContext;
27
use Jaxon\Plugin\Response\JQuery\DomSelector;
28
use Jaxon\Plugin\ResponsePlugin;
29
use Nyholm\Psr7\Factory\Psr17Factory;
30
use Nyholm\Psr7\Stream;
31
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
32
33
use function array_filter;
34
use function array_map;
35
use function gmdate;
36
use function is_array;
37
use function is_integer;
38
use function json_encode;
39
use function trim;
40
41
class Response implements ResponseInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Response
Loading history...
42
{
43
    use Traits\CommandTrait;
44
    use Traits\DomTrait;
45
    use Traits\JsTrait;
46
47
    /**
48
     * The container
49
     *
50
     * @var Container
51
     */
52
    protected $di;
53
54
    /**
55
     * @var PluginManager
56
     */
57
    protected $xPluginManager;
58
59
    /**
60
     * @var Psr17Factory
61
     */
62
    protected $xPsr17Factory;
63
64
    /**
65
     * The constructor
66
     *
67
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
68
     * @param PluginManager $xPluginManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
69
     * @param Psr17Factory $xPsr17Factory
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...
70
     */
71
    public function __construct(Container $di, PluginManager $xPluginManager, Psr17Factory $xPsr17Factory)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
72
    {
73
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 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...
74
        $this->xPluginManager = $xPluginManager;
75
        $this->xPsr17Factory = $xPsr17Factory;
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 getContentType(): string
82
    {
83
        return 'application/json';
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 getOutput(): string
90
    {
91
        return json_encode(['jxnobj' => $this->aCommands]);
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Provides access to registered response plugins
96
     *
97
     * Pass the plugin name as the first argument and the plugin object will be returned.
98
     *
99
     * @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...
100
     *
101
     * @return null|ResponsePlugin
102
     */
103
    public function plugin(string $sName): ?ResponsePlugin
104
    {
105
        return $this->xPluginManager->getResponsePlugin($sName, $this);
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Magic PHP function
110
     *
111
     * Used to permit plugins to be called as if they were native members of the Response instance.
112
     *
113
     * @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...
114
     *
115
     * @return null|ResponsePlugin
116
     */
117
    public function __get(string $sPluginName)
118
    {
119
        return $this->plugin($sPluginName);
120
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
121
122
    /**
123
     * Create a JQuery DomSelector, and link it to the current response.
124
     *
125
     * This is a shortcut to the JQuery plugin.
126
     *
127
     * @param string $sPath    The jQuery selector path
128
     * @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...
129
     *
130
     * @return DomSelector
131
     */
132
    public function jq(string $sPath = '', string $sContext = ''): DomSelector
133
    {
134
        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\Plugin\Response\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

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

146
        return $this->plugin('bags')->/** @scrutinizer ignore-call */ bag($sName);;
Loading history...
147
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
148
149
    /**
150
     * Add a response command to the array of commands that will be sent to the browser
151
     *
152
     * @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...
153
     * @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...
154
     *
155
     * @return Response
156
     */
157
    public function addCommand(array $aAttributes, $mData): Response
158
    {
159
        $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...
160
            return is_integer($xAttribute) ? $xAttribute : trim((string)$xAttribute, " \t");
161
        }, $aAttributes);
162
        $aAttributes['data'] = $mData;
163
        $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...
164
        return $this;
165
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
166
167
    /**
168
     * Add a response command to the array of commands that will be sent to the browser
169
     *
170
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
171
     * @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...
172
     * @param mixed $mData    The data to be associated with this command
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
173
     * @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...
174
     *
175
     * @return Response
176
     */
177
    protected function _addCommand(string $sName, array $aAttributes, $mData, bool $bRemoveEmpty = false): Response
178
    {
179
        $mData = is_array($mData) ? array_map(function($sData) {
180
            return trim((string)$sData, " \t\n");
181
        }, $mData) : trim((string)$mData, " \t\n");
182
        if($bRemoveEmpty)
183
        {
184
            $aAttributes = array_filter($aAttributes, function($xValue) {
185
                return $xValue === '';
186
            });
187
        }
188
        $aAttributes['cmd'] = $sName;
189
        return $this->addCommand($aAttributes, $mData);
190
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
191
192
    /**
193
     * Add a response command that is generated by a plugin
194
     *
195
     * @param ResponsePlugin $xPlugin    The plugin object
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
196
     * @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...
197
     * @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...
198
     *
199
     * @return Response
200
     */
201
    public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, $mData): Response
202
    {
203
        $aAttributes['plg'] = $xPlugin->getName();
204
        return $this->addCommand($aAttributes, $mData);
205
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
206
207
    /**
208
     * Convert this response to a PSR7 response object
209
     *
210
     * @return PsrResponseInterface
211
     */
212
    public function toPsr(): PsrResponseInterface
213
    {
214
        $xPsrResponse = $this->xPsr17Factory->createResponse(200);
215
        if($this->di->getRequest()->getMethod() === 'GET')
216
        {
217
            $xPsrResponse = $xPsrResponse
218
                ->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
219
                ->withHeader('Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT')
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
220
                ->withHeader('Cache-Control', 'no-cache, must-revalidate')
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
221
                ->withHeader('Pragma', 'no-cache');
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
222
        }
223
        return $xPsrResponse
224
            ->withHeader('content-type', $this->getContentType())
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
225
            ->withBody(Stream::create($this->getOutput()));
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
226
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
227
}
228