Passed
Push — master ( b6cd05...9f5262 )
by Gaetano
05:39
created

Response::faultCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace PhpXmlRpc;
4
5
use PhpXmlRpc\Traits\CharsetEncoderAware;
6
7
/**
8
 * This class provides the representation of the response of an XML-RPC server.
9
 * Server-side, a server method handler will construct a Response and pass it as its return value.
10
 * An identical Response object will be returned by the result of an invocation of the send() method of the Client class.
11
 *
12
 * @property array $hdrs deprecated, use $httpResponse['headers']
13
 * @property array _cookies deprecated, use $httpResponse['cookies']
14
 * @property string $raw_data deprecated, use $httpResponse['raw_data']
15
 */
16
class Response
17
{
18
    use CharsetEncoderAware;
19
20
    /// @todo: do these need to be public?
21
    /** @internal */
22
    public $val = 0;
23
    /** @internal */
24
    public $valtyp;
25
    /** @internal */
26
    public $errno = 0;
27
    /** @internal */
28
    public $errstr = '';
29
    public $payload;
30
    /** @var string */
31
    public $content_type = 'text/xml';
32
    protected $httpResponse = array('headers' => array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null);
33
34
    /**
35
     * @param Value|string|mixed $val either a Value object, a php value or the xml serialization of an xml-rpc value (a string)
36
     * @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded
37
     * @param string $fString the error string, in case of an error response
38
     * @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let
39
     *                        the code guess the correct type.
40
     * @param array|null $httpResponse this should be set when the response is being built out of data received from
41
     *                                 http (i.e. not when programmatically building a Response server-side). Array
42
     *                                 keys should include, if known: headers, cookies, raw_data, status_code
43
     *
44
     * @todo add check that $val / $fCode / $fString is of correct type???
45
     *       NB: as of now we do not do it, since it might be either an xml-rpc value or a plain php val, or a complete
46
     *       xml chunk, depending on usage of Client::send() inside which the constructor is called...
47
     */
48
    public function __construct($val, $fCode = 0, $fString = '', $valType = '', $httpResponse = null)
49
    {
50
        if ($fCode != 0) {
51
            // error response
52
            $this->errno = $fCode;
53
            $this->errstr = $fString;
54
        } else {
55
            // successful response
56
            $this->val = $val;
57
            if ($valType == '') {
58 716
                // user did not declare type of response value: try to guess it
59
                if (is_object($this->val) && is_a($this->val, 'PhpXmlRpc\Value')) {
60 716
                    $this->valtyp = 'xmlrpcvals';
61
                } elseif (is_string($this->val)) {
62 184
                    $this->valtyp = 'xml';
63 184
                } else {
64
                    $this->valtyp = 'phpvals';
65
                }
66 682
            } else {
67 682
                // user declares type of resp value: believe him
68
                $this->valtyp = $valType;
69 564
            }
70 564
        }
71
72
        if (is_array($httpResponse)) {
73
            $this->httpResponse = array_merge(array('headers' => array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null), $httpResponse);
74 27
        }
75
    }
76
77
    /**
78 681
     * Returns the error code of the response.
79
     *
80
     * @return integer the error code of this response (0 for not-error responses)
81
     */
82 716
    public function faultCode()
83 711
    {
84
        return $this->errno;
85 716
    }
86
87
    /**
88
     * Returns the error code of the response.
89
     *
90
     * @return string the error string of this response ('' for not-error responses)
91
     */
92 702
    public function faultString()
93
    {
94 702
        return $this->errstr;
95
    }
96
97
    /**
98
     * Returns the value received by the server. If the Response's faultCode is non-zero then the value returned by this
99
     * method should not be used (it may not even be an object).
100
     *
101
     * @return Value|string|mixed the Value object returned by the server. Might be an xml string or plain php value
102 560
     *                            depending on the convention adopted when creating the Response
103
     */
104 560
    public function value()
105
    {
106
        return $this->val;
107
    }
108
109
    /**
110
     * Returns an array with the cookies received from the server.
111
     * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 => $val2, ...)
112
     * with attributes being e.g. 'expires', 'path', domain'.
113
     * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array.
114 679
     * It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back
115
     * with the next request to the server (using $client->setCookie) or not.
116 679
     * The values are filled in at constructor time, and might not be set for specific debug values used.
117
     *
118
     * @return array[] array of cookies received from the server
119
     */
120
    public function cookies()
121
    {
122
        return $this->httpResponse['cookies'];
123
    }
124
125
    /**
126
     * Returns an array with info about the http response received from the server.
127
     * The values are filled in at constructor time, and might not be set for specific debug values used.
128
     *
129 22
     * @return array array with keys 'headers', 'cookies', 'raw_data' and 'status_code'.
130
     */
131 22
    public function httpResponse()
132
    {
133
        return $this->httpResponse;
134
    }
135
136
    /**
137 46
     * Returns xml representation of the response. XML prologue not included.
138
     *
139 46
     * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
140
     * @return string the xml representation of the response
141
     * @throws \Exception
142
     */
143
    public function serialize($charsetEncoding = '')
144
    {
145
        if ($charsetEncoding != '') {
146
            $this->content_type = 'text/xml; charset=' . $charsetEncoding;
147
        } else {
148
            $this->content_type = 'text/xml';
149
        }
150
        if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
151 565
            $result = "<methodResponse xmlns:ex=\"" . PhpXmlRpc::$xmlrpc_null_apache_encoding_ns . "\">\n";
152
        } else {
153 565
            $result = "<methodResponse>\n";
154 52
        }
155
        if ($this->errno) {
156 513
            // Let non-ASCII response messages be tolerated by clients by xml-encoding non ascii chars
157
            $result .= "<fault>\n" .
158 565
                "<value>\n<struct><member><name>faultCode</name>\n<value><int>" . $this->errno .
159 2
                "</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>" .
160
                $this->getCharsetEncoder()->encodeEntities($this->errstr, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</string></value>\n</member>\n" .
161 564
                "</struct>\n</value>\n</fault>";
162
        } else {
163 565
            if (!is_object($this->val) || !is_a($this->val, 'PhpXmlRpc\Value')) {
0 ignored issues
show
introduced by
The condition is_object($this->val) is always false.
Loading history...
164
                if (is_string($this->val) && $this->valtyp == 'xml') {
0 ignored issues
show
introduced by
The condition is_string($this->val) is always false.
Loading history...
165
                    $result .= "<params>\n<param>\n" .
166 89
                        $this->val .
167 89
                        "</param>\n</params>";
168 89
                } else {
169 89
                    /// @todo try to build something serializable using the Encoder...
170
                    throw new \Exception('cannot serialize xmlrpc response objects whose content is native php values');
171 561
                }
172
            } else {
173
                $result .= "<params>\n<param>\n" .
174
                    $this->val->serialize($charsetEncoding) .
175
                    "</param>\n</params>";
176
            }
177
        }
178
        $result .= "\n</methodResponse>";
179
        $this->payload = $result;
180
181
        return $result;
182 561
    }
183 561
184
    // BC layer
185
186 565
    public function __get($name)
187 565
    {
188
        //trigger_error('getting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
189 565
190
        switch ($name) {
191
            case 'hdrs':
192
                return $this->httpResponse['headers'];
193
            case '_cookies':
194 2
                return $this->httpResponse['cookies'];
195
            case 'raw_data':
196
                return $this->httpResponse['raw_data'];
197
            default:
198 2
                $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
199 2
                trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
200
                return null;
201 2
        }
202
    }
203 2
204 2
    public function __set($name, $value)
205
    {
206
        //trigger_error('setting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
207
208
        switch ($name) {
209
            case 'hdrs':
210
                $this->httpResponse['headers'] = $value;
211
                break;
212 561
            case '_cookies':
213
                $this->httpResponse['cookies'] = $value;
214
                break;
215
            case 'raw_data':
216 561
                $this->httpResponse['raw_data'] = $value;
217 561
                break;
218
            default:
219
                $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
220 561
                trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
221
        }
222
    }
223 561
224 561
    public function __isset($name)
225 561
    {
226
        //trigger_error('checking property Response::' . $name . ' is deprecated', E_USER_DEPRECATED);
227
228
        switch ($name) {
229
            case 'hdrs':
230 561
                return isset($this->httpResponse['headers']);
231
            case '_cookies':
232
                return isset($this->httpResponse['cookies']);
233
            case 'raw_data':
234
                return isset($this->httpResponse['raw_data']);
235
            default:
236
                return false;
237
        }
238
    }
239
240
    public function __unset($name)
241
    {
242
        switch ($name) {
243
            case 'hdrs':
244
                unset($this->httpResponse['headers']);
245
                break;
246
            case '_cookies':
247
                unset($this->httpResponse['cookies']);
248
                break;
249
            case 'raw_data':
250
                unset($this->httpResponse['raw_data']);
251
                break;
252
            default:
253
                $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
254
                trigger_error('Undefined property via __unset(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
255
        }
256
    }
257
}
258