Completed
Pull Request — master (#9)
by Jean-Baptiste
02:39
created

Guzzle5::getBaseUri()   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 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 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 View Code Duplication
    public function setDefaultAuth($username, $password, $type = 'basic')
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...
59
    {
60
        $auth = [$username, $password];
61
        if ($type !== 'basic') {
62
            $auth[] = $type;
63
        }
64
65
        $this->setDefaultAuth('auth', $auth);
0 ignored issues
show
Documentation introduced by
$auth is of type array<integer,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
    }
67
68
    /**
69
     * @return Client
70
     */
71
    public function getClient()
72
    {
73
        return $this->client;
74
    }
75
76
    /**
77
     * @param Client $client
78
     *
79
     * @return self
80
     */
81
    public function setClient(Client $client)
82
    {
83
        $this->client = $client;
84
85
        return $this;
86
    }
87
88
    /**
89
     * {@InheritDoc}
90
     */
91 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...
92
    {
93
        $guzzleRequest = $this->client->createRequest(
94
            $request->getMethod(),
95
            $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, GuzzleHttp\Client::createRequest() does only seem to accept string|array|object<GuzzleHttp\Url>|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...
96
            ['headers' => $request->getHeaders()]
97
        );
98
99
        $guzzleRequest->setBody(Stream::factory($request->getBody()));
100
101
        $guzzleResponse = $this->getClient()->send($guzzleRequest);
102
103
        $response = new Response(
104
            $guzzleResponse->getStatusCode(),
105
            $guzzleResponse->getHeaders(),
106
            $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...
107
        );
108
109
        return $response;
110
    }
111
112
    /**
113
     * Used to mock client
114
     *
115
     * @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface
116
     */
117
    public function getEmitter()
118
    {
119
        return $this->client->getEmitter();
120
    }
121
}
122