Passed
Push — master ( 1409f2...fc9029 )
by Gaetano
06:02
created

ParallelClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 84%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 54
c 1
b 0
f 1
dl 0
loc 78
ccs 42
cts 50
cp 0.84
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B sendParallel() 0 76 10
1
<?php
2
3
require_once __DIR__ . "/_prepend.php";
4
5
use PhpXmlRpc\Encoder;
6
use PhpXmlRpc\Client;
7
use PhpXmlRpc\PhpXmlRpc;
8
use PhpXmlRpc\Request;
9
use PhpXmlRpc\Response;
10
11
/**
12
 * A class taking advantage of cURL to send many requests in parallel (to a single server), for when the given server
13
 * does not support system.multicall method
14
 */
15
class ParallelClient extends Client
16
{
17 1
    public function sendParallel($requests, $timeout = 0, $method = '')
18
    {
19 1
        if ($method == '') {
20 1
            $method = $this->method;
21
        }
22
23
        /// @todo validate that $method can be handled by the current curl install
24
25 1
        $handles = array();
26 1
        $curl = curl_multi_init();
27
28 1
        foreach($requests as $k => $req) {
29 1
            $req->setDebug($this->debug);
30
31 1
            $handle = $this->prepareCurlHandle(
32 1
                $req,
33 1
                $this->server,
34 1
                $this->port,
35
                $timeout,
36 1
                $this->username,
37 1
                $this->password,
38 1
                $this->authtype,
39 1
                $this->cert,
40 1
                $this->certpass,
41 1
                $this->cacert,
42 1
                $this->cacertdir,
43 1
                $this->proxy,
44 1
                $this->proxyport,
45 1
                $this->proxy_user,
46 1
                $this->proxy_pass,
47 1
                $this->proxy_authtype,
48
                $method,
49 1
                false,
50 1
                $this->key,
51 1
                $this->keypass,
52 1
                $this->sslversion
53
            );
54 1
            curl_multi_add_handle($curl, $handle);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type true; however, parameter $multi_handle of curl_multi_add_handle() does only seem to accept CurlMultiHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

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

54
            curl_multi_add_handle(/** @scrutinizer ignore-type */ $curl, $handle);
Loading history...
55 1
            $handles[$k] = $handle;
56
        }
57
58 1
        $running = 0;
59
        do {
60 1
            curl_multi_exec($curl, $running);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type true; however, parameter $multi_handle of curl_multi_exec() does only seem to accept CurlMultiHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

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

60
            curl_multi_exec(/** @scrutinizer ignore-type */ $curl, $running);
Loading history...
61 1
        } while($running > 0);
62
63 1
        $responses = array();
64 1
        foreach($handles as $k => $h) {
65 1
            $responses[$k] = curl_multi_getcontent($handles[$k]);
66
67 1
            if ($this->debug > 1) {
68
                $message = "---CURL INFO---\n";
69
                foreach (curl_getinfo($h) as $name => $val) {
70
                    if (is_array($val)) {
71
                        $val = implode("\n", $val);
72
                    }
73
                    $message .= $name . ': ' . $val . "\n";
74
                }
75
                $message .= '---END---';
76
                $this->getLogger()->debugMessage($message);
77
            }
78
79
            //curl_close($h);
80 1
            curl_multi_remove_handle($curl, $h);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type true; however, parameter $multi_handle of curl_multi_remove_handle() does only seem to accept CurlMultiHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

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

80
            curl_multi_remove_handle(/** @scrutinizer ignore-type */ $curl, $h);
Loading history...
81
        }
82 1
        curl_multi_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type true; however, parameter $multi_handle of curl_multi_close() does only seem to accept CurlMultiHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

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

82
        curl_multi_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
83
84 1
        foreach($responses as $k => $resp) {
85 1
            if (!$resp) {
86
                $responses[$k] = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error($curl));
0 ignored issues
show
Bug introduced by
$curl of type CurlMultiHandle|true is incompatible with the type CurlHandle|resource expected by parameter $handle of curl_error(). ( Ignorable by Annotation )

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

86
                $responses[$k] = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error(/** @scrutinizer ignore-type */ $curl));
Loading history...
87
            } else {
88 1
                $responses[$k] = $requests[$k]->parseResponse($resp, true, $this->return_type);
89
            }
90
        }
91
92 1
        return $responses;
93
    }
94
}
95
96 1
$num_tests = 25;
97
98 1
$data = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
99 1
$encoder = new Encoder();
100 1
$value = $encoder->encode($data, array('auto_dates'));
101 1
$req = new Request('interopEchoTests.echoValue', array($value));
102 1
$reqs = array();
103 1
for ($i = 0; $i < $num_tests; $i++) {
104 1
    $reqs[] = $req;
105
}
106
107 1
$client = new ParallelClient(XMLRPCSERVER);
108 1
$client->no_multicall = true;
109
110 1
$t = microtime(true);
111 1
$resp = $client->send($reqs);
112 1
$t = microtime(true) - $t;
113 1
echo "Sequential send: $t secs\n";
114
115 1
$t = microtime(true);
116 1
$resp = $client->sendParallel($reqs);
117 1
$t = microtime(true) - $t;
118 1
echo "Parallel send: $t secs\n";
119
120 1
$client->no_multicall = false;
121 1
$t = microtime(true);
122 1
$resp = $client->send($reqs);
123 1
$t = microtime(true) - $t;
124 1
echo "Multicall send: $t secs\n";
125
126
require_once __DIR__ . "/_append.php";
127