Completed
Push — master ( 408edc...841b8f )
by Sergey
02:35
created

GuzzleHttpClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 114
Duplicated Lines 34.21 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 39
loc 114
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 12 2
A post() 13 13 1
A put() 13 13 1
A delete() 11 11 1
A setBaseUrl() 0 5 1
A parseResponse() 0 6 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 seregazhuk\Favro;
4
5
use GuzzleHttp\ClientInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use seregazhuk\Favro\Contracts\HttpClient;
8
9
class GuzzleHttpClient implements HttpClient
10
{
11
    /**
12
     * @var ClientInterface
13
     */
14
    protected $client;
15
16
    /**
17
     * @param ClientInterface $client
18
     */
19
    public function __construct(ClientInterface $client)
20
    {
21
        $this->client = $client;
22
    }
23
24
    /**
25
     * @param string $uri
26
     * @param array $params
27
     * @param array $headers
28
     * @return string
29
     */
30
    public function get($uri, $params = [], $headers = [])
31
    {
32
        if (!empty($params)) {
33
            $uri .= '?' . http_build_query($params);
34
        }
35
36
        $response = $this
37
            ->client
38
            ->get($uri, ['headers' => $headers]);
39
40
        return $this->parseResponse($response);
41
    }
42
43
    /**
44
     * @param string $uri
45
     * @param array $body
46
     * @param array $headers
47
     * @return string
48
     */
49 View Code Duplication
    public function post($uri, $body = [], $headers = [])
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...
50
    {
51
        $response = $this
52
            ->client
53
            ->post(
54
                $uri, [
55
                    'headers'     => $headers,
56
                    'form_params' => $body,
57
                ]
58
            );
59
60
        return $this->parseResponse($response);
61
    }
62
63
    /**
64
     * @param string $uri
65
     * @param array $body
66
     * @param array $headers
67
     * @return mixed
68
     */
69 View Code Duplication
    public function put($uri, $body = [], $headers = [])
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...
70
    {
71
        $response = $this
72
            ->client
73
            ->put(
74
                $uri, [
75
                    'headers'     => $headers,
76
                    'form_params' => $body,
77
                ]
78
            );
79
80
        return $this->parseResponse($response);
81
    }
82
83
    /**
84
     * @param string $uri
85
     * @param array $body
86
     * @param array $headers
87
     * @return mixed
88
     */
89 View Code Duplication
    public function delete($uri, $body = [], $headers = [])
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...
90
    {
91
        $response = $this
92
            ->client
93
            ->delete($uri, [
94
                'headers'     => $headers,
95
                'form_params' => $body,
96
            ]);
97
98
        return $this->parseResponse($response);
99
    }
100
101
    /**
102
     * @param string $url
103
     * @return $this
104
     */
105
    public function setBaseUrl($url)
106
    {
107
        $this->client->setBaseUrl($url);
0 ignored issues
show
Bug introduced by
The method setBaseUrl() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        return $this;
109
    }
110
111
    /**
112
     * @param ResponseInterface $response
113
     * @return array|null
114
     */
115
    protected function parseResponse(ResponseInterface $response)
116
    {
117
        $responseContents = $response->getBody()->getContents();
118
119
        return json_decode($responseContents, true);
120
    }
121
122
}