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

Guzzle5::getEmitter()   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 GuzzleHttp\Stream\Stream;
8
use GuzzleHttp\Client;
9
use CanalTP\AbstractGuzzle\Guzzle;
10
11
class Guzzle5 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 Guzzle5 client with base url.
30
     */
31
    public function initClient()
32
    {
33
        $client = new Client(array(
34
            'base_url' => $this->getBaseUri(),
35
            'stream' => false,
36
            'http_errors' => false,
37
        ));
38
39
        $client->setDefaultOption('exceptions', false);
40
41
        $this->setClient($client);
42
    }
43
44
    /**
45
     * @return Client
46
     */
47
    public function getClient()
48
    {
49
        return $this->client;
50
    }
51
52
    /**
53
     * @param Client $client
54
     *
55
     * @return self
56
     */
57
    public function setClient(Client $client)
58
    {
59
        $this->client = $client;
60
61
        return $this;
62
    }
63
64
    /**
65
     * {@InheritDoc}
66
     */
67 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...
68
    {
69
        $guzzleRequest = $this->client->createRequest(
70
            $request->getMethod(),
71
            $request->getUri(),
72
            ['headers' => $request->getHeaders()]
73
        );
74
75
        $guzzleRequest->setBody(Stream::factory($request->getBody()));
76
77
        $guzzleResponse = $this->getClient()->send($guzzleRequest);
78
79
        $response = new Response(
80
            $guzzleResponse->getStatusCode(),
81
            $guzzleResponse->getHeaders(),
82
            $guzzleResponse->getBody(true)
83
        );
84
85
        return $response;
86
    }
87
88
    /**
89
     * Used to mock client
90
     *
91
     * @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface
92
     */
93
    public function getEmitter()
94
    {
95
        return $this->client->getEmitter();
96
    }
97
}
98