Guzzle5::getDefaultOptions()   A
last analyzed

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 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, $options = [])
22
    {
23
        $this->defaultOptions = [
24
            'base_url' => $baseUri,
25
            'defaults' => $options
26
        ];
27
28
        $this->client = new Client($this->defaultOptions);
29
    }
30
31
    /**
32
     * We have to recreate client to modify baseUri
33
     *
34
     * @param string $baseUri
35
     */
36
    public function setBaseUri($baseUri)
37
    {
38
        $this->__construct($baseUri, []);
39
    }
40
41
    public function getBaseUri()
42
    {
43
        return $this->client->getBaseUrl();
44
    }
45
46
    public function setDefaultOptions($options = [])
47
    {
48
        $this->__construct($this->getBaseUri(), $options);
49
    }
50
51
    public function getDefaultOptions()
52
    {
53
        return $this->client->getDefaultOption();
54
    }
55
56
    public function setDefaultAuth($username, $password, $type = 'basic')
57
    {
58
        $this->client->setDefaultOption('auth', [$username, $password, $type]);
59
    }
60
61
    /**
62
     * @return Client
63
     */
64
    public function getClient()
65
    {
66
        return $this->client;
67
    }
68
69
    /**
70
     * @param Client $client
71
     *
72
     * @return self
73
     */
74
    public function setClient(Client $client)
75
    {
76
        $this->client = $client;
77
78
        return $this;
79
    }
80
81
    /**
82
     * {@InheritDoc}
83
     */
84 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...
85
    {
86
        $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...
87
            $request->getMethod(),
88
            $request->getUri(),
89
            ['headers' => $request->getHeaders()]
90
        );
91
92
        $guzzleRequest->setBody(Stream::factory($request->getBody()));
93
94
        $guzzleResponse = $this->getClient()->send($guzzleRequest);
95
96
        $response = new Response(
97
            $guzzleResponse->getStatusCode(),
98
            $guzzleResponse->getHeaders(),
99
            $guzzleResponse->getBody(true)
0 ignored issues
show
Unused Code introduced by
The call to ResponseInterface::getBody() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
        );
101
102
        return $response;
103
    }
104
105
    /**
106
     * Used to mock client
107
     *
108
     * @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface
109
     */
110
    public function getEmitter()
111
    {
112
        return $this->client->getEmitter();
113
    }
114
}
115