1
|
|
|
<?php namespace Comodojo\RpcClient\Processor; |
2
|
|
|
|
3
|
|
|
use \Psr\Log\LoggerInterface; |
4
|
|
|
use \Comodojo\Exception\RpcException; |
5
|
|
|
use \Comodojo\RpcClient\RpcRequest; |
6
|
|
|
use \Comodojo\Xmlrpc\XmlrpcEncoder; |
7
|
|
|
use \Comodojo\Xmlrpc\XmlrpcDecoder; |
8
|
|
|
use \Exception; |
9
|
|
|
|
10
|
|
|
class XmlProcessor implements ProcessorInterface { |
11
|
|
|
|
12
|
|
|
private $encoding; |
13
|
|
|
|
14
|
|
|
private $logger; |
15
|
|
|
|
16
|
|
|
private $encoder; |
17
|
|
|
|
18
|
|
|
private $decoder; |
19
|
|
|
|
20
|
|
|
private $requests; |
|
|
|
|
21
|
|
|
|
22
|
|
|
private $isMulticall = false; |
23
|
|
|
|
24
|
|
|
public function __construct($encoding, LoggerInterface $logger) { |
25
|
|
|
|
26
|
|
|
$this->encoding = $encoding; |
27
|
|
|
|
28
|
|
|
$this->logger = $logger; |
29
|
|
|
|
30
|
|
|
$this->encoder = new XmlrpcEncoder(); |
31
|
|
|
|
32
|
|
|
$this->decoder = new XmlrpcDecoder(); |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function encode($requests) { |
37
|
|
|
|
38
|
|
|
$requests = array_values($requests); |
39
|
|
|
|
40
|
|
|
$this->isMulticall = sizeof($requests) > 1 ? true : false; |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
|
44
|
|
|
$payload = $this->isMulticall ? $this->encodeMulticall($requests) : $this->encodeSingleCall($requests[0]); |
45
|
|
|
|
46
|
|
|
} catch (XmlrpcException $xe) { |
|
|
|
|
47
|
|
|
|
48
|
|
|
throw $xe; |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $payload; |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function decode($response) { |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
|
60
|
|
|
$content = $this->decoder->decodeResponse($response); |
61
|
|
|
|
62
|
|
|
if ( $this->decoder->isFault() ) throw new RpcException($content['faultString'], $content['faultCode']); |
63
|
|
|
|
64
|
|
|
} catch (XmlrpcException $xe) { |
|
|
|
|
65
|
|
|
|
66
|
|
|
throw $xe; |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->isMulticall ? self::normalizeContent($content) : $content; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function encodeSingleCall(RpcRequest $request) { |
75
|
|
|
|
76
|
|
|
$this->logger->notice("Performing a single XML call"); |
77
|
|
|
|
78
|
|
|
$this->logger->debug("Data dump before encoding", $request->toArray()); |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
|
82
|
|
|
// encoding |
83
|
|
|
|
84
|
|
|
foreach ( $request->getSpecialTypes() as $key => $value ) { |
85
|
|
|
|
86
|
|
|
$this->encoder->setValueType($key, $value); |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$encoded_request = $this->encoder->encodeCall($request->getMethod(), $request->getParameters()); |
91
|
|
|
|
92
|
|
|
} catch (XmlrpcException $xe) { |
|
|
|
|
93
|
|
|
|
94
|
|
|
throw $xe; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->logger->debug("Data dump after encoding: ".$encoded_request); |
99
|
|
|
|
100
|
|
|
return $encoded_request; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Perform an xml multicall |
106
|
|
|
* |
107
|
|
|
* @param array $requests |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
* |
111
|
|
|
* @throws \Comodojo\Exception\RpcException |
112
|
|
|
* @throws \Comodojo\Exception\HttpException |
113
|
|
|
* @throws \Comodojo\Exception\XmlrpcException |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
|
|
private function encodeMulticall($requests) { |
117
|
|
|
|
118
|
|
|
$composed_requests = array(); |
119
|
|
|
|
120
|
|
|
$this->logger->notice("Performing an XML multicall"); |
121
|
|
|
|
122
|
|
|
$this->logger->debug("Data dump before encoding", $requests); |
123
|
|
|
|
124
|
|
|
foreach ($requests as $request) { |
125
|
|
|
|
126
|
|
|
$composed_requests[] = array($request->getMethod(), $request->getParameters()); |
127
|
|
|
|
128
|
|
|
foreach ( $request->getSpecialTypes() as $key => $value ) { |
129
|
|
|
|
130
|
|
|
$this->encoder->setValueType($key, $value); |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
|
138
|
|
|
$encoded_requests = $this->encoder->setEncoding($this->encoding)->encodeMulticall($composed_requests); |
139
|
|
|
|
140
|
|
|
} catch (XmlrpcException $xe) { |
|
|
|
|
141
|
|
|
|
142
|
|
|
throw $xe; |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $encoded_requests; |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private static function normalizeContent($content) { |
151
|
|
|
|
152
|
|
|
return array_map(function($value) { |
153
|
|
|
|
154
|
|
|
if ( |
155
|
|
|
is_array($value) && |
156
|
|
|
sizeof($value) == 1 && |
157
|
|
|
isset($value[0]) |
158
|
|
|
) { |
159
|
|
|
return $value[0]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $value; |
163
|
|
|
|
164
|
|
|
}, $content); |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.