Passed
Push — master ( 7c83a0...cd350b )
by Thierry
02:08
created

ResponseManager::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * ResponseManager.php - Jaxon ResponsePlugin PluginManager
5
 *
6
 * This class stores and tracks the response that will be returned after processing a request.
7
 * The Response Manager represents a single point of contact for working with <ResponsePlugin> objects.
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\Jaxon;
25
use Jaxon\Request\Handler\ArgumentManager;
26
use Jaxon\Utils\Config\Config;
27
use Jaxon\Utils\Translation\Translator;
28
29
use function trim;
30
use function header;
31
use function strlen;
32
use function gmdate;
33
use function get_class;
34
35
class ResponseManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ResponseManager
Loading history...
36
{
37
    /**
38
     * @var Jaxon
39
     */
40
    private $jaxon;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
41
42
    /**
43
     * @var Config
44
     */
45
    private $xConfig;
46
47
    /**
48
     * @var ArgumentManager
49
     */
50
    private $xArgumentManager;
51
52
    /**
53
     * @var Translator
54
     */
55
    protected $xTranslator;
56
57
    /**
58
     * The current response object that will be sent back to the browser
59
     * once the request processing phase is complete
60
     *
61
     * @var AbstractResponse
62
     */
63
    private $xResponse = null;
64
65
    /**
66
     * The debug messages
67
     *
68
     * @var array
69
     */
70
    private $aDebugMessages;
71
72
    /**
73
     * The class constructor
74
     *
75
     * @param Jaxon $jaxon
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
76
     * @param Config $xConfig
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
77
     * @param ArgumentManager $xArgumentManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
78
     * @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...
79
     */
80
    public function __construct(Jaxon $jaxon, Config $xConfig, ArgumentManager $xArgumentManager, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
81
    {
82
        $this->jaxon = $jaxon;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
83
        $this->xConfig = $xConfig;
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...
84
        $this->xArgumentManager = $xArgumentManager;
85
        $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...
86
        $this->aDebugMessages = [];
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...
87
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
88
89
    /**
90
     * Clear the current response
91
     *
92
     * A new response will need to be appended before the request processing is complete.
93
     *
94
     * @return void
95
     */
96
    public function clear()
97
    {
98
        $this->xResponse = null;
99
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
100
101
    /**
102
     * Get the response to the Jaxon request
103
     *
104
     * @return AbstractResponse
105
     */
106
    public function getResponse(): ?AbstractResponse
107
    {
108
        return $this->xResponse;
109
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
110
111
    /**
112
     * Append one response object onto the end of another
113
     *
114
     * You cannot append a given response onto the end of a response of different type.
115
     * If no prior response has been appended, this response becomes the main response
116
     * object to which other response objects will be appended.
117
     *
118
     * @param AbstractResponse $xResponse    The response object to be appended
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
119
     *
120
     * @return void
121
     */
122
    public function append(AbstractResponse $xResponse)
123
    {
124
        if(!$this->xResponse)
125
        {
126
            $this->xResponse = $xResponse;
127
        }
128
        elseif(get_class($this->xResponse) === get_class($xResponse))
129
        {
130
            if($this->xResponse !== $xResponse)
131
            {
132
                $this->xResponse->appendResponse($xResponse);
133
            }
134
        }
135
        else
136
        {
137
            $this->debug($this->xTranslator->trans('errors.mismatch.types', ['class' => get_class($xResponse)]));
138
        }
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
    /**
142
     * Appends a debug message on the end of the debug message queue
143
     *
144
     * Debug messages will be sent to the client with the normal response
145
     * (if the response object supports the sending of debug messages, see: <ResponsePlugin>)
146
     *
147
     * @param string $sMessage    The debug message
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
148
     *
149
     * @return void
150
     */
151
    public function debug(string $sMessage)
152
    {
153
        $this->aDebugMessages[] = $sMessage;
154
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
155
156
    /**
157
     * Clear the response and appends a debug message on the end of the debug message queue
158
     *
159
     * @param string $sMessage    The debug message
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
160
     *
161
     * @return void
162
     */
163
    public function error(string $sMessage)
164
    {
165
        $this->clear();
166
        $this->append($this->jaxon->newResponse());
167
        $this->debug($sMessage);
168
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
169
170
    /**
171
     * Prints the debug messages into the current response object
172
     *
173
     * @return void
174
     */
175
    public function printDebug()
176
    {
177
        if(($this->xResponse))
178
        {
179
            foreach($this->aDebugMessages as $sMessage)
180
            {
181
                $this->xResponse->debug($sMessage);
182
            }
183
            $this->aDebugMessages = [];
184
        }
185
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
186
187
    /**
188
     * Used internally to generate the response headers
189
     *
190
     * @return void
191
     */
192
    private function _sendHeaders()
193
    {
194
        if($this->xArgumentManager->requestMethodIsGet())
195
        {
196
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
197
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
198
            header("Cache-Control: no-cache, must-revalidate");
199
            header("Pragma: no-cache");
200
        }
201
202
        $sCharacterSet = '';
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...
203
        $sCharacterEncoding = trim($this->xConfig->getOption('core.encoding', ''));
204
        if(strlen($sCharacterEncoding) > 0)
205
        {
206
            $sCharacterSet = '; charset="' . $sCharacterEncoding . '"';
207
        }
208
209
        header('content-type: ' . $this->xResponse->getContentType() . ' ' . $sCharacterSet);
210
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
211
212
    /**
213
     * Sends the HTTP headers back to the browser
214
     *
215
     * @return void
216
     */
217
    public function sendHeaders()
218
    {
219
        if(($this->xResponse))
220
        {
221
            $this->_sendHeaders();
222
        }
223
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
224
225
    /**
226
     * Get the JSON output of the response
227
     *
228
     * @return string
229
     */
230
    public function getOutput(): string
231
    {
232
        if(($this->xResponse))
233
        {
234
            return $this->xResponse->getOutput();
235
        }
236
        return '';
237
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
238
239
    /**
240
     * Prints the response object to the output stream, thus sending the response to the browser
241
     *
242
     * @return void
243
     */
244
    public function sendOutput()
245
    {
246
        if(($this->xResponse))
247
        {
248
            $this->_sendHeaders();
249
            $this->xResponse->printOutput();
250
        }
251
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
252
}
253