Completed
Push — master ( 9d322b...621c9b )
by Meng
02:08
created

SoapClient::parseResponse()   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 2
nop 4
1
<?php
2
3
namespace Meng\AsyncSoap\Guzzle;
4
5
use Meng\AsyncSoap\SoapClientInterface;
6
use Meng\Soap\HttpBinding\HttpBinding;
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\RequestException;
9
use GuzzleHttp\Promise\PromiseInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
class SoapClient implements SoapClientInterface
13
{
14
    private $deferredHttpBinding;
15
    private $client;
16
17
    public function __construct(ClientInterface $client, PromiseInterface $httpBindingPromise)
18
    {
19
        $this->deferredHttpBinding = $httpBindingPromise;
20
        $this->client = $client;
21
    }
22
23
    public function __call($name, $arguments)
24
    {
25
        return $this->callAsync($name, $arguments);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->callAsync($name, $arguments); (GuzzleHttp\Promise\Promise) is incompatible with the return type declared by the interface Meng\AsyncSoap\SoapClientInterface::__call of type Meng\AsyncSoap\Promise.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
26
    }
27
28
    public function call($name, array $arguments, array $options = null, $inputHeaders = null, array &$outputHeaders = null)
29
    {
30
        $callPromise = $this->callAsync($name, $arguments, $options, $inputHeaders, $outputHeaders);
31
        return $callPromise->wait();
32
    }
33
34
    public function callAsync($name, array $arguments, array $options = null, $inputHeaders = null, array &$outputHeaders = null)
35
    {
36
        return \GuzzleHttp\Promise\coroutine(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \GuzzleHttp\Promi...y()->close(); } }); (GuzzleHttp\Promise\Promise) is incompatible with the return type declared by the interface Meng\AsyncSoap\SoapClientInterface::callAsync of type Meng\AsyncSoap\Promise.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
37
            function () use ($name, $arguments, $options, $inputHeaders, &$outputHeaders) {
38
                /** @var HttpBinding $httpBinding */
39
                $httpBinding = (yield $this->deferredHttpBinding);
40
                $request = $httpBinding->request($name, $arguments, $options, $inputHeaders);
41
42
                try {
43
                    $response = (yield $this->client->sendAsync($request));
44
                    yield $this->parseResponse($httpBinding, $response, $name, $outputHeaders);
45
                } catch (RequestException $exception) {
46
                    if ($exception->hasResponse()) {
47
                        $response = $exception->getResponse();
48
                        yield $this->parseResponse($httpBinding, $response, $name, $outputHeaders);
49
                    } else {
50
                        throw $exception;
51
                    }
52
                } finally {
53
                    $request->getBody()->close();
54
                }
55
            }
56
        );
57
    }
58
59
    private function parseResponse(HttpBinding $httpBinding, ResponseInterface $response, $name, &$outputHeaders)
60
    {
61
        try {
62
            return $httpBinding->response($response, $name, $outputHeaders);
63
        } finally {
64
            $response->getBody()->close();
65
        }
66
    }
67
}