Completed
Push — master ( 8f4643...4ef224 )
by Gaetano
07:48 queued 06:06
created

Response   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.72%

Importance

Changes 0
Metric Value
dl 0
loc 147
ccs 36
cts 43
cp 0.8372
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A faultCode() 0 4 1
A faultString() 0 4 1
A value() 0 4 1
A cookies() 0 4 1
B __construct() 0 24 6
B serialize() 0 41 8
1
<?php
2
3
namespace PhpXmlRpc;
4
5
use PhpXmlRpc\Helper\Charset;
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
class Response
13
{
14
    /// @todo: do these need to be public?
15
    public $val = 0;
16
    public $valtyp;
17
    public $errno = 0;
18
    public $errstr = '';
19
    public $payload;
20
    public $hdrs = array();
21
    public $_cookies = array();
22
    public $content_type = 'text/xml';
23
    public $raw_data = '';
24
25
    /**
26
     * @param mixed $val either a Value object, a php value or the xml serialization of an xmlrpc value (a string)
27
     * @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded
28
     * @param string $fString the error string, in case of an error response
29
     * @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let
30
     *                        the code guess the correct type.
31
     *
32
     * @todo add check that $val / $fCode / $fString is of correct type???
33
     *       NB: as of now we do not do it, since it might be either an xmlrpc value or a plain php val, or a complete
34
     *       xml chunk, depending on usage of Client::send() inside which creator is called...
35
     */
36 603
    public function __construct($val, $fCode = 0, $fString = '', $valType = '')
37
    {
38 603
        if ($fCode != 0) {
39
            // error response
40 191
            $this->errno = $fCode;
41 191
            $this->errstr = $fString;
42
        } else {
43
            // successful response
44 534
            $this->val = $val;
45 534
            if ($valType == '') {
46
                // user did not declare type of response value: try to guess it
47 441
                if (is_object($this->val) && is_a($this->val, 'PhpXmlRpc\Value')) {
48 441
                    $this->valtyp = 'xmlrpcvals';
49
                } elseif (is_string($this->val)) {
50
                    $this->valtyp = 'xml';
51
                } else {
52 23
                    $this->valtyp = 'phpvals';
53
                }
54
            } else {
55
                // user declares type of resp value: believe him
56 533
                $this->valtyp = $valType;
57
            }
58
        }
59 603
    }
60
61
    /**
62
     * Returns the error code of the response.
63
     *
64
     * @return integer the error code of this response (0 for not-error responses)
65
     */
66 591
    public function faultCode()
67
    {
68 591
        return $this->errno;
69
    }
70
71
    /**
72
     * Returns the error code of the response.
73
     *
74
     * @return string the error string of this response ('' for not-error responses)
75
     */
76 485
    public function faultString()
77
    {
78 485
        return $this->errstr;
79
    }
80
81
    /**
82
     * Returns the value received by the server. If the Response's faultCode is non-zero then the value returned by this
83
     * method should not be used (it may not even be an object).
84
     *
85
     * @return Value|string|mixed the Value object returned by the server. Might be an xml string or plain php value
86
     *                            depending on the convention adopted when creating the Response
87
     */
88 531
    public function value()
89
    {
90 531
        return $this->val;
91
    }
92
93
    /**
94
     * Returns an array with the cookies received from the server.
95
     * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 => $val2, ...)
96
     * with attributes being e.g. 'expires', 'path', domain'.
97
     * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array.
98
     * It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back
99
     * with the next request to the server (using Client::setCookie) or not.
100
     *
101
     * @return array array of cookies received from the server
102
     */
103 18
    public function cookies()
104
    {
105 18
        return $this->_cookies;
106
    }
107
108
    /**
109
     * Returns xml representation of the response. XML prologue not included.
110
     *
111
     * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed
112
     *
113
     * @return string the xml representation of the response
114
     *
115
     * @throws \Exception
116
     */
117 441
    public function serialize($charsetEncoding = '')
118
    {
119 441
        if ($charsetEncoding != '') {
120 50
            $this->content_type = 'text/xml; charset=' . $charsetEncoding;
121
        } else {
122 391
            $this->content_type = 'text/xml';
123
        }
124 441
        if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
125 1
            $result = "<methodResponse xmlns:ex=\"" . PhpXmlRpc::$xmlrpc_null_apache_encoding_ns . "\">\n";
126
        } else {
127 441
            $result = "<methodResponse>\n";
128
        }
129 441
        if ($this->errno) {
130
            // G. Giunta 2005/2/13: let non-ASCII response messages be tolerated by clients
131
            // by xml-encoding non ascii chars
132
            $result .= "<fault>\n" .
133 71
                "<value>\n<struct><member><name>faultCode</name>\n<value><int>" . $this->errno .
134
                "</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>" .
135 71
                Charset::instance()->encodeEntities($this->errstr, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</string></value>\n</member>\n" .
136 71
                "</struct>\n</value>\n</fault>";
137
        } else {
138 439
            if (!is_object($this->val) || !is_a($this->val, 'PhpXmlRpc\Value')) {
139
                if (is_string($this->val) && $this->valtyp == 'xml') {
140
                    $result .= "<params>\n<param>\n" .
141
                        $this->val .
142
                        "</param>\n</params>";
143
                } else {
144
                    /// @todo try to build something serializable?
145
                    throw new \Exception('cannot serialize xmlrpc response objects whose content is native php values');
146
                }
147
            } else {
148
                $result .= "<params>\n<param>\n" .
149 439
                    $this->val->serialize($charsetEncoding) .
150 439
                    "</param>\n</params>";
151
            }
152
        }
153 441
        $result .= "\n</methodResponse>";
154 441
        $this->payload = $result;
155
156 441
        return $result;
157
    }
158
}
159