Completed
Push — master ( 5b3cfe...9166d3 )
by Thierry
06:16 queued 04:30
created

Manager::hasNoResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Manager.php - Jaxon Response Manager
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 <Response> objects.
8
 *
9
 * @package jaxon-core
10
 * @author Jared White
11
 * @author J. Max Wilson
12
 * @author Joseph Woolley
13
 * @author Steffen Konerow
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
16
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
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
 */
21
22
namespace Jaxon\Response;
23
24
class Manager
25
{
26
    use \Jaxon\Utils\Traits\Translator;
27
28
    /**
29
     * The current response object that will be sent back to the browser
30
     * once the request processing phase is complete
31
     *
32
     * @var \Jaxon\Response\Response
33
     */
34
    private $xResponse;
35
36
    /**
37
     * The debug messages
38
     *
39
     * @var array
40
     */
41
    private $aDebugMessages;
42
43
    public function __construct()
44
    {
45
        $this->xResponse = null;
46
        $this->aDebugMessages = array();
47
    }
48
49
    /**
50
     * Clear the current response
51
     *
52
     * A new response will need to be appended before the request processing is complete.
53
     *
54
     * @return void
55
     */
56
    public function clear()
57
    {
58
        $this->xResponse = null;
59
    }
60
61
    /**
62
     * Check if there is currently no response
63
     *
64
     * @return boolean
65
     */
66
    public function hasNoResponse()
67
    {
68
        return ($this->xResponse == null);
69
    }
70
71
    /**
72
     * Append one response object onto the end of another
73
     *
74
     * You cannot append a given response onto the end of a response of different type.
75
     * If no prior response has been appended, this response becomes the main response
76
     * object to which other response objects will be appended.
77
     *
78
     * @param Response        $xResponse            The response object to be appended
79
     *
80
     * @return void
81
     */
82
    public function append(Response $xResponse)
83
    {
84
        if(!$this->xResponse)
85
        {
86
            $this->xResponse = $xResponse;
87
        }
88
        elseif(get_class($this->xResponse) == get_class($xResponse))
89
        {
90
            if($this->xResponse != $xResponse)
91
            {
92
                $this->xResponse->appendResponse($xResponse);
93
            }
94
        }
95
        else
96
        {
97
            $this->debug($this->trans('errors.mismatch.types', array('class' => get_class($xResponse))));
98
        }
99
    }
100
101
    /**
102
     * Appends a debug message on the end of the debug message queue
103
     *
104
     * Debug messages will be sent to the client with the normal response
105
     * (if the response object supports the sending of debug messages, see: <Response>)
106
     *
107
     * @param string        $sMessage            The debug message
108
     *
109
     * @return void
110
     */
111
    public function debug($sMessage)
112
    {
113
        $this->aDebugMessages[] = $sMessage;
114
    }
115
116
    /**
117
     * Prints the debug messages into the current response object
118
     *
119
     * @return void
120
     */
121
    public function printDebug()
122
    {
123
        if(($this->xResponse))
124
        {
125
            foreach($this->aDebugMessages as $sMessage)
126
            {
127
                $this->xResponse->debug($sMessage);
128
            }
129
            $this->aDebugMessages = array();
130
        }
131
    }
132
133
    /**
134
     * Sends the HTTP headers back to the browser
135
     *
136
     * @return void
137
     */
138
    public function sendHeaders()
139
    {
140
        if(($this->xResponse))
141
        {
142
            $this->xResponse->sendHeaders();
143
        }
144
    }
145
146
    /**
147
     * Get the JSON output of the response
148
     *
149
     * @return string
150
     */
151
    public function getOutput()
152
    {
153
        if(($this->xResponse))
154
        {
155
            return $this->xResponse->getOutput();
156
        }
157
        return '';
158
    }
159
160
    /**
161
     * Prints the response object to the output stream, thus sending the response to the browser
162
     *
163
     * @return void
164
     */
165
    public function sendOutput()
166
    {
167
        if(($this->xResponse))
168
        {
169
            $this->xResponse->sendHeaders();
170
            $this->xResponse->printOutput();
171
        }
172
    }
173
}
174