|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Meng\AsyncSoap\Guzzle; |
|
4
|
|
|
|
|
5
|
|
|
use function GuzzleHttp\Promise\coroutine; |
|
6
|
|
|
use Meng\AsyncSoap\SoapClientInterface; |
|
7
|
|
|
use Meng\Soap\HttpBinding\HttpBinding; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
10
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
class SoapClient implements SoapClientInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private $httpBindingPromise; |
|
16
|
|
|
private $client; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(ClientInterface $client, PromiseInterface $httpBindingPromise) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->httpBindingPromise = $httpBindingPromise; |
|
21
|
|
|
$this->client = $client; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function __call($name, $arguments) |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->callAsync($name, $arguments); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function call($name, array $arguments, array $options = null, $inputHeaders = null, array &$outputHeaders = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$callPromise = $this->callAsync($name, $arguments, $options, $inputHeaders, $outputHeaders); |
|
32
|
|
|
return $callPromise->wait(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function callAsync($name, array $arguments, array $options = null, $inputHeaders = null, array &$outputHeaders = null) |
|
36
|
|
|
{ |
|
37
|
|
|
return coroutine( |
|
38
|
|
|
function () use ($name, $arguments, $options, $inputHeaders, &$outputHeaders) { |
|
39
|
|
|
/** @var HttpBinding $httpBinding */ |
|
40
|
|
|
$httpBinding = (yield $this->httpBindingPromise); |
|
41
|
|
|
$request = $httpBinding->request($name, $arguments, $options, $inputHeaders); |
|
42
|
|
|
$requestOptions = isset($options['request_options']) ? $options['request_options'] : []; |
|
43
|
|
|
|
|
44
|
|
|
try { |
|
45
|
|
|
$response = (yield $this->client->sendAsync($request, $requestOptions)); |
|
46
|
|
|
yield $this->interpretResponse($httpBinding, $response, $name, $outputHeaders); |
|
47
|
|
|
} catch (RequestException $exception) { |
|
48
|
|
|
if ($exception->hasResponse()) { |
|
49
|
|
|
$response = $exception->getResponse(); |
|
50
|
|
|
yield $this->interpretResponse($httpBinding, $response, $name, $outputHeaders); |
|
|
|
|
|
|
51
|
|
|
} else { |
|
52
|
|
|
throw $exception; |
|
53
|
|
|
} |
|
54
|
|
|
} finally { |
|
55
|
|
|
$request->getBody()->close(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function interpretResponse(HttpBinding $httpBinding, ResponseInterface $response, $name, &$outputHeaders) |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
|
|
return $httpBinding->response($response, $name, $outputHeaders); |
|
65
|
|
|
} finally { |
|
66
|
|
|
$response->getBody()->close(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: