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 Psr\Http\Message\ResponseInterface; |
8
|
|
|
use React\Promise\PromiseInterface; |
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
|
2 |
|
public function __construct(RequestService $requestService) |
24
|
|
|
{ |
25
|
2 |
|
$this->requestService = $requestService; |
26
|
2 |
|
} |
27
|
|
|
|
28
|
2 |
|
public function callRaw(string $method, array $arguments = []): PromiseInterface |
29
|
|
|
{ |
30
|
|
|
$xml = [ |
31
|
|
|
'methodCall' => [ |
32
|
2 |
|
'methodName' => $method, |
33
|
|
|
], |
34
|
|
|
]; |
35
|
|
|
|
36
|
2 |
|
if (count($arguments) > 0) { |
37
|
|
|
$xml['methodCall']['params'] = $arguments; |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
return $this->requestService->request(new Request( |
41
|
2 |
|
'POST', |
42
|
2 |
|
'', |
43
|
2 |
|
[], |
44
|
2 |
|
new XmlStream($xml) |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public function call(string $method, array $arguments = []): PromiseInterface |
49
|
|
|
{ |
50
|
2 |
|
$params = []; |
51
|
2 |
|
foreach ($arguments as $argument) { |
52
|
|
|
$params[] = [ |
53
|
|
|
'param' => [ |
54
|
|
|
'value' => [ |
55
|
|
|
gettype($argument) => $argument, |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return $this->callRaw($method, $params)->then(function (ResponseInterface $response) { |
62
|
2 |
|
$xml = $response->getBody()->getParsedContents(); |
|
|
|
|
63
|
2 |
|
$xml = $xml['methodResponse']; |
64
|
|
|
|
65
|
2 |
|
if (isset($xml['fault'])) { |
66
|
1 |
|
return $this->handleFault($xml); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $this->handleSuccess($xml); |
70
|
2 |
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
private function handleFault(array $xml): PromiseInterface |
74
|
|
|
{ |
75
|
1 |
|
$fault = XmlRpcPayloadParser::parse($xml['fault']['value']); |
76
|
|
|
|
77
|
1 |
|
return reject( |
78
|
1 |
|
new XmlRpcError( |
79
|
1 |
|
$fault['faultString'], |
80
|
1 |
|
$fault['faultCode'] |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
private function handleSuccess(array $xml): PromiseInterface |
86
|
|
|
{ |
87
|
1 |
|
return resolve( |
88
|
1 |
|
XmlRpcPayloadParser::parse( |
89
|
1 |
|
$xml['params']['param']['value'] |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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: