Passed
Push — master ( 1a762a...7f95e5 )
by Gaetano
08:04
created

XmlRpcProxy::call()   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
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
require_once __DIR__ . "/_prepend.php";
3
4 1
output('<html lang="en">
5
<head><title>xmlrpc - Proxy demo</title></head>
6
<body>
7
<h1>proxy demo</h1>
8
<h2>Query server using a "proxy" object</h2>
9
<h3>The code demonstrates usage for the terminally lazy. For a more complete proxy, look at the Wrapper class</h3>
10
<p>You can see the source to this page here: <a href="proxy.php?showSource=1">proxy.php</a></p>
11
');
12
13
class XmlRpcProxy
14
{
15
    protected $client;
16
    protected $prefix;
17
    protected $encoder;
18 1
    protected $encodingOptions = array();
19
20 1
    /**
21 1
     * We rely on injecting a fully-formed Client, so that all the necessary http/debugging options can be set into it
22 1
     * without the need for this class to reimplement support for that configuration.
23
     */
24
    public function __construct(PhpXmlRpc\Client $client, $prefix = 'examples.', $encodingOptions = array())
25
    {
26
        $this->client = $client;
27
        $this->prefix = $prefix;
28
        $this->encodingOptions = $encodingOptions;
29
        $this->encoder = new PhpXmlRpc\Encoder();
30
    }
31
32
    /**
33
     * Translates any php method call to an xml-rpc call.
34
     * Note that the server might expose methods which can not be called directly this way, because their name includes
35
     * characters which are not allowed in a php method. That's why we implement as well method `call`
36 1
     *
37
     * @author Toth Istvan
38 1
     *
39 1
     * @param string $name remote function name. Will be prefixed
40 1
     * @param array $arguments any php value will do. For xml-rpc base64 values, wrap them in a Value object
41 1
     * @return mixed
42
     *
43
     * @throws Exception
44
     */
45 1
    public function __call($name, $arguments)
46
    {
47 1
        $arguments = array();
48
        foreach ($arguments as $parameter) {
49 1
            $arguments[] = $this->encoder->encode($parameter, $this->encodingOptions);
50
        }
51
52 1
        // just in case this was set to something else
53
        $originalReturnType = $this->client->return_type;
54
        $this->client->return_type = 'phpvals';
55
56
        $resp = $this->client->send(new PhpXmlRpc\Request($this->prefix.$name, $arguments));
57
58
        $this->client->return_type = $originalReturnType;
59
60
        if ($resp->faultCode()) {
61
            throw new \Exception($resp->faultString(), $resp->faultCode());
62
        } else {
63
            return $resp->value();
64
        }
65
    }
66
67
    /**
68
     * In case the remote method name has characters which are not valid as php method names, use this.
69
     *
70
     * @param string $name remote function name. Will be prefixed
71
     * @param array $arguments any php value will do. For xml-rpc base64 values, wrap them in a Value object
72 1
     * @return mixed
73 1
     *
74 1
     * @throws Exception
75
     */
76 1
    public function call($name, $arguments)
77
    {
78 1
        return $this->__call($name, $arguments);
79
    }
80 1
}
81
82
$proxy = new XmlRpcProxy(new PhpXmlRpc\Client(XMLRPCSERVER));
83
84
$stateNo = rand(1, 51);
85
// sadly, no IDE will be able to assist with autocompletion for this method, unless you manually add an equivalent phpdoc comment...
86
$stateName = $proxy->getStateName($stateNo);
0 ignored issues
show
Bug introduced by
The method getStateName() does not exist on XmlRpcProxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
$stateName = $proxy->/** @scrutinizer ignore-call */ getStateName($stateNo);
Loading history...
87
88
output("State $stateNo is ".htmlspecialchars($stateName));
89
90
output("</body></html>\n");
91