Passed
Push — v5.x ( 57c2ac...73bdb8 )
by Thierry
16:58 queued 05:55
created

Response   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 20
Bugs 1 Features 0
Metric Value
wmc 12
eloc 31
c 20
b 1
f 0
dl 0
loc 159
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 3 1
A __construct() 0 5 1
A __get() 0 3 1
A getOutput() 0 4 2
A plugin() 0 3 1
A bag() 0 5 1
A str() 0 3 1
A paginate() 0 5 1
A jq() 0 5 1
A toPsr() 0 14 2
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\Response\DataBag\DataBagContext;
26
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
27
use Jaxon\Plugin\Response\JQuery\DomSelector;
28
use Jaxon\Plugin\Response\JQuery\JQueryPlugin;
29
use Jaxon\Plugin\Response\Pagination\PaginatorPlugin;
30
use Jaxon\Plugin\ResponsePlugin;
31
use Jaxon\Request\Call\Call;
32
use Nyholm\Psr7\Factory\Psr17Factory;
33
use Nyholm\Psr7\Stream;
34
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
35
use Psr\Http\Message\ServerRequestInterface as PsrRequestInterface;
36
37
use function gmdate;
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\HtmlDomTrait;
45
    use Traits\ScriptTrait;
46
47
    /**
48
     * @var PluginManager
49
     */
50
    protected $xPluginManager;
51
52
    /**
53
     * @var Psr17Factory
54
     */
55
    protected $xPsr17Factory;
56
57
    /**
58
     * @var PsrRequestInterface
59
     */
60
    protected $xRequest;
61
62
    /**
63
     * The constructor
64
     *
65
     * @param PluginManager $xPluginManager
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
66
     * @param Psr17Factory $xPsr17Factory
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
67
     * @param PsrRequestInterface $xRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
68
     */
69
    public function __construct(PluginManager $xPluginManager, Psr17Factory $xPsr17Factory, PsrRequestInterface $xRequest)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
70
    {
71
        $this->xPluginManager = $xPluginManager;
72
        $this->xPsr17Factory = $xPsr17Factory;
73
        $this->xRequest = $xRequest;
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...
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
75
76
    /**
77
     * @inheritDoc
78
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
79
    public function getContentType(): string
80
    {
81
        return 'application/json';
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * @inheritDoc
86
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
87
    public function getOutput(): string
88
    {
89
        return $this->getCommandCount() === 0 ? '{}' :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
90
            json_encode(['jxnobj' => $this->aCommands]);
91
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
92
93
    /**
94
     * Provides access to registered response plugins
95
     *
96
     * Pass the plugin name as the first argument and the plugin object will be returned.
97
     *
98
     * @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...
99
     *
100
     * @return null|ResponsePlugin
101
     */
102
    public function plugin(string $sName): ?ResponsePlugin
103
    {
104
        return $this->xPluginManager->getResponsePlugin($sName, $this);
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     * Magic PHP function
109
     *
110
     * Used to permit plugins to be called as if they were native members of the Response instance.
111
     *
112
     * @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...
113
     *
114
     * @return null|ResponsePlugin
115
     */
116
    public function __get(string $sPluginName)
117
    {
118
        return $this->plugin($sPluginName);
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * Convert to string
123
     *
124
     * @param mixed $xData
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
125
     *
126
     * @return string
127
     */
128
    protected function str($xData): string
129
    {
130
        return trim((string)$xData, " \t\n");
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * Create a JQuery DomSelector, and link it to the current response.
135
     *
136
     * This is a shortcut to the JQuery plugin.
137
     *
138
     * @param string $sPath    The jQuery selector path
139
     * @param mixed $xContext    A context associated to the selector
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; 4 found
Loading history...
140
     *
141
     * @return DomSelector
142
     */
143
    public function jq(string $sPath = '', $xContext = null): DomSelector
144
    {
145
        /** @var JQueryPlugin */
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
146
        $xPlugin = $this->plugin('pg');
147
        return $xPlugin->selector($sPath, $xContext);
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * Get the databag with a given name
152
     *
153
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
154
     *
155
     * @return DataBagContext
156
     */
157
    public function bag(string $sName): DataBagContext
158
    {
159
        /** @var DataBagPlugin */
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
160
        $xPlugin = $this->plugin('pg');
161
        return $xPlugin->bag($sName);
162
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
163
164
    /**
165
     * Render an HTML pagination control.
166
     *
167
     * @param Call $xCall
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
168
     * @param int $nCurrentPage     The current page number
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
169
     * @param int $nItemsPerPage    The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
170
     * @param int $nTotalItems      The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
171
     *
172
     * @return void
173
     */
174
    public function paginate(Call $xCall, int $nCurrentPage, int $nItemsPerPage, int $nTotalItems)
175
    {
176
        /** @var PaginatorPlugin */
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
177
        $xPlugin = $this->plugin('pg');
178
        $xPlugin->render($xCall, $nCurrentPage, $nItemsPerPage, $nTotalItems);
0 ignored issues
show
Bug introduced by
The method render() 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\Pagination\PaginatorPlugin. ( Ignorable by Annotation )

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

178
        $xPlugin->/** @scrutinizer ignore-call */ 
179
                  render($xCall, $nCurrentPage, $nItemsPerPage, $nTotalItems);
Loading history...
179
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
180
181
    /**
182
     * Convert this response to a PSR7 response object
183
     *
184
     * @return PsrResponseInterface
185
     */
186
    public function toPsr(): PsrResponseInterface
187
    {
188
        $xPsrResponse = $this->xPsr17Factory->createResponse(200);
189
        if($this->xRequest->getMethod() === 'GET')
190
        {
191
            $xPsrResponse = $xPsrResponse
192
                ->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...
193
                ->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...
194
                ->withHeader('Cache-Control', 'no-cache, must-revalidate')
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
195
                ->withHeader('Pragma', 'no-cache');
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
196
        }
197
        return $xPsrResponse
198
            ->withHeader('content-type', $this->getContentType())
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
199
            ->withBody(Stream::create($this->getOutput()));
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
201
}
202