Passed
Push — master ( 41d5a5...e82656 )
by Thierry
03:02
created

ResponseManager::printDebug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
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\Manager;
23
24
use Jaxon\Di\Container;
25
use Jaxon\Exception\RequestException;
26
use Jaxon\Response\ResponseInterface;
27
use Jaxon\Utils\Translation\Translator;
28
29
use function get_class;
30
31
class ResponseManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ResponseManager
Loading history...
32
{
33
    /**
34
     * @var Container
35
     */
36
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
37
38
    /**
39
     * @var Translator
40
     */
41
    protected $xTranslator;
42
43
    /**
44
     * @var string
45
     */
46
    private $sCharacterEncoding;
47
48
    /**
49
     * The current response object that will be sent back to the browser
50
     * once the request processing phase is complete
51
     *
52
     * @var ResponseInterface
53
     */
54
    private $xResponse;
55
56
    /**
57
     * The debug messages
58
     *
59
     * @var array
60
     */
61
    private $aDebugMessages = [];
62
63
    /**
64
     * @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...
65
     * @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...
66
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
67
     */
68
    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...
69
    {
70
        $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...
71
        $this->sCharacterEncoding = $sCharacterEncoding;
72
        $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...
73
        $this->xResponse = $di->getResponse(); // By default, use the global response;
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...
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
75
76
    /**
77
     * Clear the current response
78
     *
79
     * @return void
80
     */
81
    public function clear()
82
    {
83
        $this->xResponse->clearCommands();
84
        $this->di->getResponse()->clearCommands();
85
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
86
87
    /**
88
     * Get the response to the Jaxon request
89
     *
90
     * @return ResponseInterface
91
     */
92
    public function getResponse(): ResponseInterface
93
    {
94
        return $this->xResponse;
95
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
96
97
    /**
98
     * Append one response object onto the end of another
99
     *
100
     * You cannot append a given response onto the end of a response of different type.
101
     * If no prior response has been appended, this response becomes the main response
102
     * object to which other response objects will be appended.
103
     *
104
     * @param ResponseInterface $xResponse The response object to be appended
105
     *
106
     * @return void
107
     * @throws RequestException
108
     */
109
    public function append(ResponseInterface $xResponse)
110
    {
111
        if($this->xResponse->getCommandCount() === 0)
112
        {
113
            $this->xResponse = $xResponse;
114
            return;
115
        }
116
        if(get_class($this->xResponse) !== get_class($xResponse))
117
        {
118
            throw new RequestException($this->xTranslator->trans('errors.mismatch.types',
119
                ['class' => get_class($xResponse)]));
120
        }
121
        if($this->xResponse !== $xResponse)
122
        {
123
            $this->xResponse->appendResponse($xResponse);
124
        }
125
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
126
127
    /**
128
     * Appends a debug message on the end of the debug message queue
129
     *
130
     * Debug messages will be sent to the client with the normal response
131
     * (if the response object supports the sending of debug messages, see: <ResponsePlugin>)
132
     *
133
     * @param string $sMessage    The debug message
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
134
     *
135
     * @return void
136
     */
137
    public function debug(string $sMessage)
138
    {
139
        $this->aDebugMessages[] = $sMessage;
140
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
141
142
    /**
143
     * Clear the response and appends a debug message on the end of the debug message queue
144
     *
145
     * @param string $sMessage The debug message
146
     *
147
     * @return void
148
     */
149
    public function error(string $sMessage)
150
    {
151
        $this->clear();
152
        $this->debug($sMessage);
153
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
154
155
    /**
156
     * Prints the debug messages into the current response object
157
     *
158
     * @return void
159
     */
160
    public function printDebug()
161
    {
162
        foreach($this->aDebugMessages as $sMessage)
163
        {
164
            $this->xResponse->debug($sMessage);
165
        }
166
        $this->aDebugMessages = [];
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * Get the content type of the HTTP response
171
     *
172
     * @return string
173
     */
174
    public function getContentType(): string
175
    {
176
        return empty($this->sCharacterEncoding) ? $this->xResponse->getContentType() :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
177
            $this->xResponse->getContentType() . '; charset="' . $this->sCharacterEncoding . '"';
178
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
179
180
    /**
181
     * Get the JSON output of the response
182
     *
183
     * @return string
184
     */
185
    public function getOutput(): string
186
    {
187
        return $this->xResponse->getCommandCount() === 0 ? '' : $this->xResponse->getOutput();
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
189
}
190