GuzzleHttpClient   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 31.36 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 37
loc 118
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 parseResponse() 0 8 1
A getResponseHeaders() 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 seregazhuk\Favro;
4
5
use GuzzleHttp\Client;
6
use Psr\Http\Message\ResponseInterface;
7
use seregazhuk\Favro\Contracts\HttpClient;
8
9
class GuzzleHttpClient implements HttpClient
10
{
11
    /**
12
     * @var Client
13
     */
14
    protected $client;
15
16
    /**
17
     * @var array
18
     */
19
    protected $responseHeaders;
20
21
    /**
22
     * @param Client $client
23
     */
24
    public function __construct(Client $client)
25
    {
26
        $this->client = $client;
27
    }
28
29
    /**
30
     * @param string $uri
31
     * @param array $params
32
     * @param array $headers
33
     * @return array
34
     */
35
    public function get($uri, array $params = [], array $headers = [])
36
    {
37
        if (!empty($params)) {
38
            $uri .= '?' . http_build_query($params);
39
        }
40
41
        $response = $this
42
            ->client
43
            ->get($uri, ['headers' => $headers]);
44
45
        return $this->parseResponse($response);
46
    }
47
48
    /**
49
     * @param string $uri
50
     * @param array $body
51
     * @param array $headers
52
     * @return array
53
     */
54 View Code Duplication
    public function post($uri, array $body = [], array $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...
55
    {
56
        $response = $this
57
            ->client
58
            ->post(
59
                $uri, [
60
                    'headers'     => $headers,
61
                    'form_params' => $body,
62
                ]
63
            );
64
65
        return $this->parseResponse($response);
66
    }
67
68
    /**
69
     * @param string $uri
70
     * @param array $body
71
     * @param array $headers
72
     * @return mixed
73
     */
74 View Code Duplication
    public function put($uri, array $body = [], array $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...
75
    {
76
        $response = $this
77
            ->client
78
            ->put(
79
                $uri, [
80
                    'headers'     => $headers,
81
                    'form_params' => $body,
82
                ]
83
            );
84
85
        return $this->parseResponse($response);
86
    }
87
88
    /**
89
     * @param string $uri
90
     * @param array $body
91
     * @param array $headers
92
     * @return mixed
93
     */
94 View Code Duplication
    public function delete($uri, array $body = [], array $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...
95
    {
96
        $response = $this
97
            ->client
98
            ->delete($uri, [
99
                'headers'     => $headers,
100
                'form_params' => $body,
101
            ]);
102
103
        return $this->parseResponse($response);
104
    }
105
106
    /**
107
     * @param ResponseInterface $response
108
     * @return array|null
109
     */
110
    protected function parseResponse(ResponseInterface $response)
111
    {
112
        $responseContents = $response->getBody()->getContents();
113
114
        $this->responseHeaders = $response->getHeaders();
115
116
        return json_decode($responseContents, true);
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function getResponseHeaders()
123
    {
124
        return $this->responseHeaders;
125
    }
126
}
127