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

SoapClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 57
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 4 1
A call() 0 5 1
B callAsync() 0 25 4
A interpretResponse() 0 8 1
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