Passed
Push — master ( 0336f4...58f541 )
by Gaetano
05:32
created

PhpXmlRpcProxy::__call()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 10
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.9332
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 PhpXmlRpcProxy
14
{
15
    protected $client;
16
    protected $prefix;
17
18 1
    public function __construct(PhpXmlRpc\Client $client, $prefix = 'examples.')
19
    {
20 1
        $this->client = $client;
21 1
        $this->prefix = $prefix;
22 1
    }
23
24
    /**
25
     * Translates any method call to an xmlrpc call.
26
     *
27
     * @author Toth Istvan
28
     *
29
     * @param string $name remote function name. Will be prefixed
30
     * @param array $arguments
31
     *
32
     * @return mixed
33
     *
34
     * @throws Exception
35
     */
36 1
    public function __call($name, $arguments)
37
    {
38 1
        $encoder = new PhpXmlRpc\Encoder();
39 1
        $valueArray = array();
40 1
        foreach ($arguments as $parameter) {
41 1
            $valueArray[] = $encoder->encode($parameter);
42
        }
43
44
        // just in case this was set to something else
45 1
        $this->client->return_type = 'phpvals';
46
47 1
        $resp = $this->client->send(new PhpXmlRpc\Request($this->prefix.$name, $valueArray));
48
49 1
        if ($resp->faultCode()) {
50
            throw new Exception($resp->faultString(), $resp->faultCode());
51
        } else {
52 1
            return $resp->value();
53
        }
54
    }
55
56
    /**
57
     * In case the remote method name has characters which are not valid as php method names, use this.
58
     *
59
     * @param string $name remote function name. Will be prefixed
60
     * @param array $arguments
61
     *
62
     * @return mixed
63
     *
64
     * @throws Exception
65
     */
66
    public function call($name, $arguments)
67
    {
68
        return $this->__call($name, $arguments);
69
    }
70
}
71
72 1
$stateNo = rand(1, 51);
73 1
$proxy = new PhpXmlRpcProxy(new PhpXmlRpc\Client(XMLRPCSERVER));
74 1
$stateName = $proxy->getStateName($stateNo);
0 ignored issues
show
Bug introduced by
The method getStateName() does not exist on PhpXmlRpcProxy. 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

74
$stateName = $proxy->/** @scrutinizer ignore-call */ getStateName($stateNo);
Loading history...
75
76 1
output("State $stateNo is ".htmlspecialchars($stateName));
77
78 1
output("</body></html>\n");
79
80
require_once __DIR__ . "/_append.php";
81