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

Guzzle5::setDefaultOptions()   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 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 = array_merge([
24
            'base_url' => $baseUri,
25
            'defaults' => [
26
                'exceptions' => false,
27
            ]
28
        ], $options);
29
30
        $this->client = new Client($this->defaultOptions);
31
    }
32
33
    /**
34
     * We have to recreate client to modify baseUri
35
     *
36
     * @param string $baseUri
37
     */
38
    public function setBaseUri($baseUri)
39
    {
40
        $this->setDefaultOptions(array_merge($this->defaultOptions, ['base_url' => $baseUri]));
41
    }
42
43
    public function getBaseUri()
44
    {
45
        return $this->client->getBaseUrl();
46
    }
47
48
    public function setDefaultOptions($options = [])
49
    {
50
        $this->__construct($this->getBaseUri(), $options);
51
    }
52
53
    public function getDefaultOptions()
54
    {
55
        return $this->client->getDefaultOption();
56
    }
57
58
    /**
59
     * @return Client
60
     */
61
    public function getClient()
62
    {
63
        return $this->client;
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(
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...
84
            $request->getMethod(),
85
            $request->getUri(),
86
            ['headers' => $request->getHeaders()]
87
        );
88
89
        $guzzleRequest->setBody(Stream::factory($request->getBody()));
90
91
        $guzzleResponse = $this->getClient()->send($guzzleRequest);
92
93
        $response = new Response(
94
            $guzzleResponse->getStatusCode(),
95
            $guzzleResponse->getHeaders(),
96
            $guzzleResponse->getBody(true)
97
        );
98
99
        return $response;
100
    }
101
102
    /**
103
     * Used to mock client
104
     *
105
     * @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface
106
     */
107
    public function getEmitter()
108
    {
109
        return $this->client->getEmitter();
110
    }
111
}
112