Completed
Pull Request — master (#9)
by Jean-Baptiste
03:46
created

Guzzle5::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 11
Ratio 91.67 %

Importance

Changes 0
Metric Value
dl 11
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
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 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 = [
24
            'base_url' => $baseUri,
25
            'defaults' => array_merge(
26
                ['exceptions' => false],
27
                $options
28
            )
29
        ];
30
31
        $this->client = new Client($this->defaultOptions);
32
    }
33
34
    /**
35
     * We have to recreate client to modify baseUri
36
     *
37
     * @param string $baseUri
38
     */
39
    public function setBaseUri($baseUri)
40
    {
41
        $this->__construct($baseUri, []);
42
    }
43
44
    public function getBaseUri()
45
    {
46
        return $this->client->getBaseUrl();
47
    }
48
49
    public function setDefaultOptions($options = [])
50
    {
51
        $this->__construct($this->getBaseUri(), $options);
52
    }
53
54
    public function getDefaultOptions()
55
    {
56
        return $this->client->getDefaultOption();
57
    }
58
59
    public function setDefaultAuth($username, $password, $type = 'basic')
60
    {
61
        $this->client->setDefaultOption('auth', [$username, $password, $type]);
62
    }
63
64
    /**
65
     * @return Client
66
     */
67
    public function getClient()
68
    {
69
        return $this->client;
70
    }
71
72
    /**
73
     * @param Client $client
74
     *
75
     * @return self
76
     */
77
    public function setClient(Client $client)
78
    {
79
        $this->client = $client;
80
81
        return $this;
82
    }
83
84
    /**
85
     * {@InheritDoc}
86
     */
87 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...
88
    {
89
        $guzzleRequest = $this->client->createRequest(
0 ignored issues
show
Bug introduced by
The method createRequest() does not exist on GuzzleHttp\Client. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
            $request->getMethod(),
91
            $request->getUri(),
92
            ['headers' => $request->getHeaders()]
93
        );
94
95
        $guzzleRequest->setBody(Stream::factory($request->getBody()));
96
97
        $guzzleResponse = $this->getClient()->send($guzzleRequest);
98
99
        $response = new Response(
100
            $guzzleResponse->getStatusCode(),
101
            $guzzleResponse->getHeaders(),
102
            $guzzleResponse->getBody(true)
103
        );
104
105
        return $response;
106
    }
107
108
    /**
109
     * Used to mock client
110
     *
111
     * @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface
112
     */
113
    public function getEmitter()
114
    {
115
        return $this->client->getEmitter();
116
    }
117
}
118