|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\Services\XmlRpc; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Transport\Service\RequestService; |
|
6
|
|
|
use ApiClients\Middleware\Xml\XmlStream; |
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use RingCentral\Psr7\Request; |
|
10
|
|
|
use function React\Promise\reject; |
|
11
|
|
|
use function React\Promise\resolve; |
|
12
|
|
|
|
|
13
|
|
|
class XmlRpcService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var RequestService |
|
17
|
|
|
*/ |
|
18
|
|
|
private $requestService; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param RequestService $requestService |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(RequestService $requestService) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->requestService = $requestService; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function call(string $method, array $arguments = []) |
|
29
|
|
|
{ |
|
30
|
|
|
$params = []; |
|
31
|
|
|
foreach ($arguments as $argument) { |
|
32
|
|
|
$params[] = [ |
|
33
|
|
|
'param' => [ |
|
34
|
|
|
'value' => [ |
|
35
|
|
|
gettype($argument) => $argument, |
|
36
|
|
|
], |
|
37
|
|
|
], |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->callRaw($method, $params)->then(function (array $xml) { |
|
42
|
|
|
return $this->parseMessage($xml); |
|
43
|
|
|
}); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function callRaw(string $method, array $arguments = []) |
|
47
|
|
|
{ |
|
48
|
|
|
$xml = [ |
|
49
|
|
|
'methodCall' => [ |
|
50
|
|
|
'methodName' => $method, |
|
51
|
|
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
if (count($arguments) > 0) { |
|
55
|
|
|
$xml['methodCall']['params'] = $arguments; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->requestService->request(new Request( |
|
59
|
|
|
'POST', |
|
60
|
|
|
'', |
|
61
|
|
|
[], |
|
62
|
|
|
new XmlStream($xml) |
|
63
|
|
|
))->then(function (ResponseInterface $response) { |
|
64
|
|
|
$xml = $response->getBody()->getParsedContents(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
if (isset($xml['methodResponse']['fault'])) { |
|
67
|
|
|
return reject( |
|
68
|
|
|
new XmlRpcError( |
|
69
|
|
|
$xml['methodResponse']['fault']['value']['struct']['member'][1]['value']['string'], |
|
70
|
|
|
(int)$xml['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'] |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return resolve($xml['methodResponse']['params']['param']['value']); |
|
76
|
|
|
}); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function parseMessage(array $xml) |
|
80
|
|
|
{ |
|
81
|
|
|
if (isset($xml['string'])) { |
|
82
|
|
|
return $xml['string']; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (isset($xml['i4'])) { |
|
86
|
|
|
return (int)$xml['i4']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (isset($xml['int'])) { |
|
90
|
|
|
return (int)$xml['int']; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (isset($xml['double'])) { |
|
94
|
|
|
return (float)$xml['double']; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (isset($xml['boolean'])) { |
|
98
|
|
|
return (bool)$xml['boolean']; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (isset($xml['dateTime.iso8601'])) { |
|
102
|
|
|
return new DateTimeImmutable($xml['dateTime.iso8601']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $xml; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: