Completed
Pull Request — master (#9)
by Jean-Baptiste
02:39
created

Guzzle3::getClient()   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
cc 1
eloc 2
nc 1
nop 0
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 View Code Duplication
    public function __construct($baseUri, $options = [])
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...
22
    {
23
        $this->defaultOptions['request.options'] = array_merge(
24
            ['exceptions' => false]
25
            , $options);
26
27
        $this->client = new Client($baseUri, $this->defaultOptions);
28
    }
29
30
    public function setBaseUri($baseUri)
31
    {
32
        $this->client->setBaseUrl($baseUri);
33
    }
34
35
    public function getBaseUri()
36
    {
37
        return $this->client->getBaseUrl();
38
    }
39
40
    public function setDefaultOptions($options = [])
41
    {
42
        $this->setConfig($options);
0 ignored issues
show
Bug introduced by
The method setConfig() does not seem to exist on object<CanalTP\AbstractGuzzle\Version\Guzzle3>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
    }
44
45
    public function getDefaultOptions()
46
    {
47
        return $this->client->getConfig();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function setDefaultAuth($username, $password, $type = 'Basic')
54
    {
55
        $this->client->setDefaultOption('auth', [$username, $password, $type]);
56
    }
57
58
    /**
59
     * @return Client
60
     */
61
    public function getClient()
62
    {
63
        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...
64
    }
65
66
    /**
67
     * @param Client $client
68
     *
69
     * @return self
70
     */
71
    public function setClient(Client $client)
72
    {
73
        $this->client = $client;
74
75
        return $this;
76
    }
77
78
    /**
79
     * {@InheritDoc}
80
     */
81 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...
82
    {
83
        $guzzleRequest = $this->client->createRequest(
84
            $request->getMethod(),
85
            $request->getUri(),
86
            $request->getHeaders(),
87
            $request->getBody()
88
        );
89
90
        $guzzleResponse = $guzzleRequest->send();
91
92
        $response = new Response(
93
            $guzzleResponse->getStatusCode(),
94
            $guzzleResponse->getHeaders()->toArray(),
95
            $guzzleResponse->getBody(true)
96
        );
97
98
        return $response;
99
    }
100
101
    /**
102
     * use to mock client
103
     *
104
     * @param EventSubscriberInterface $subscriber
105
     */
106
    public function addSubscriber(EventSubscriberInterface $subscriber)
107
    {
108
        $this->client->addSubscriber($subscriber);
109
    }
110
}
111