Passed
Push — v5.x ( 4cf44b...6eb5e2 )
by Thierry
02:02
created

Response   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 22
Bugs 1 Features 0
Metric Value
wmc 12
eloc 33
c 22
b 1
f 0
dl 0
loc 173
rs 10

11 Methods

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

193
        return $xPlugin->/** @scrutinizer ignore-call */ paginator($nCurrentPage, $nItemsPerPage, $nTotalItems);
Loading history...
194
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
195
196
    /**
197
     * Convert this response to a PSR7 response object
198
     *
199
     * @return PsrResponseInterface
200
     */
201
    public function toPsr(): PsrResponseInterface
202
    {
203
        $xPsrResponse = $this->xPsr17Factory->createResponse(200);
204
        if($this->xRequest->getMethod() === 'GET')
205
        {
206
            $xPsrResponse = $xPsrResponse
207
                ->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...
208
                ->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...
209
                ->withHeader('Cache-Control', 'no-cache, must-revalidate')
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
210
                ->withHeader('Pragma', 'no-cache');
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
211
        }
212
        return $xPsrResponse
213
            ->withHeader('content-type', $this->getContentType())
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
214
            ->withBody(Stream::create($this->getOutput()));
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
215
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
216
}
217