Completed
Push — master ( 03622e...118594 )
by Meng
03:10
created

SoapClient::interpretResponse()   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 2
nop 4
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);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $exception->getResponse() on line 49 can be null; however, Meng\AsyncSoap\Guzzle\So...nt::interpretResponse() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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