Completed
Push — master ( 34bc1a...41d5a5 )
by Thierry
02:48 queued 02:48
created

ResponseManager::_sendHeaders()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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