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

Guzzle5   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 30.84 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 33
loc 107
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 12 1
A setBaseUri() 0 4 1
A getBaseUri() 0 4 1
A setDefaultOptions() 0 4 1
A getDefaultOptions() 0 4 1
A setDefaultAuth() 0 4 1
A getClient() 0 4 1
A setClient() 0 6 1
A send() 20 20 1
A getEmitter() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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