Completed
Pull Request — master (#5)
by Jean-Baptiste
01:43
created

Guzzle3::addSubscriber()   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 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)
22
    {
23
        parent::__construct($baseUri);
24
25
        $this->initClient();
26
    }
27
28
    /**
29
     * Init Guzzle3 client with base url.
30
     */
31
    public function initClient()
32
    {
33
        $this->setClient(new Client($this->getBaseUri(), [
34
            'request.options' => [
35
                'exceptions' => false,
36
                'stream' => false
37
            ]
38
        ]));
39
    }
40
41
    /**
42
     * @return Client
43
     */
44
    public function getClient()
45
    {
46
        return $this->client;
47
    }
48
49
    /**
50
     * @param Client $client
51
     *
52
     * @return self
53
     */
54
    public function setClient(Client $client)
55
    {
56
        $this->client = $client;
57
58
        return $this;
59
    }
60
61
    /**
62
     * {@InheritDoc}
63
     */
64 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...
65
    {
66
        $guzzleRequest = $this->client->createRequest(
67
            $request->getMethod(),
68
            $request->getUri(),
0 ignored issues
show
Bug introduced by
It seems like $request->getUri() targeting GuzzleHttp\Psr7\Request::getUri() can also be of type object<Psr\Http\Message\UriInterface>; however, Guzzle\Http\Client::createRequest() does only seem to accept string|array|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
69
            $request->getHeaders(),
70
            $request->getBody()
71
        );
72
73
        $guzzleResponse = $guzzleRequest->send();
74
75
        $response = new Response(
76
            $guzzleResponse->getStatusCode(),
77
            $guzzleResponse->getHeaders()->toArray(),
78
            $guzzleResponse->getBody(true)
79
        );
80
81
        return $response;
82
    }
83
84
    /**
85
     * use to mock client
86
     *
87
     * @param EventSubscriberInterface $subscriber
88
     */
89
    public function addSubscriber(EventSubscriberInterface $subscriber)
90
    {
91
        $this->client->addSubscriber($subscriber);
92
    }
93
}
94