Completed
Push — master ( d612da...6cfb36 )
by Jean-Baptiste
01:48
created

Guzzle3::setBaseUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 1
1
<?php
2
3
namespace CanalTP\AbstractGuzzle\Version;
4
5
use GuzzleHttp\Psr7\Request;
6
use GuzzleHttp\Psr7\Response;
7
use Guzzle\Http\Client;
8
use CanalTP\AbstractGuzzle\Guzzle;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
class Guzzle3 extends Guzzle
12
{
13
    /**
14
     * @var Client
15
     */
16
    private $client;
17
18
    /**
19
     * {@InheritDoc}
20
     */
21
    public function __construct($baseUri, $options = [])
22
    {
23
        $this->setConfig($options);
24
25
        $this->client = new Client($baseUri, $this->defaultOptions);
26
    }
27
28
    private function setConfig($options = [])
29
    {
30
        $this->defaultOptions = array_merge([
31
            'request.options' => [
32
                'exceptions' => false
33
            ]
34
        ], $options);
35
    }
36
37
    public function setBaseUri($baseUri)
38
    {
39
        $this->client->setBaseUrl($baseUri);
40
    }
41
42
    public function getBaseUri()
43
    {
44
        return $this->client->getBaseUrl();
45
    }
46
47
    public function setDefaultOptions($options = [])
48
    {
49
        $this->setConfig($options);
50
    }
51
52
    public function getDefaultOptions()
53
    {
54
        return $this->client->getConfig();
55
    }
56
57
    /**
58
     * @return Client
59
     */
60
    public function getClient()
61
    {
62
        return $this->client;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->client; (Guzzle\Http\Client) is incompatible with the return type of the parent method CanalTP\AbstractGuzzle\Guzzle::getClient of type GuzzleHttp\Client.

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...
63
    }
64
65
    /**
66
     * @param Client $client
67
     *
68
     * @return self
69
     */
70
    public function setClient(Client $client)
71
    {
72
        $this->client = $client;
73
74
        return $this;
75
    }
76
77
    /**
78
     * {@InheritDoc}
79
     */
80 View Code Duplication
    public function send(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $guzzleRequest = $this->client->createRequest(
83
            $request->getMethod(),
84
            $request->getUri(),
85
            $request->getHeaders(),
86
            $request->getBody()
87
        );
88
89
        $guzzleResponse = $guzzleRequest->send();
90
91
        $response = new Response(
92
            $guzzleResponse->getStatusCode(),
93
            $guzzleResponse->getHeaders()->toArray(),
94
            $guzzleResponse->getBody(true)
95
        );
96
97
        return $response;
98
    }
99
100
    /**
101
     * use to mock client
102
     *
103
     * @param EventSubscriberInterface $subscriber
104
     */
105
    public function addSubscriber(EventSubscriberInterface $subscriber)
106
    {
107
        $this->client->addSubscriber($subscriber);
108
    }
109
}
110