Completed
Pull Request — master (#14)
by Sergey
02:29
created

GuzzleHttpAdapter::parseResponse()   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
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\HeadHunterApi\Adapters;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Request;
7
use GuzzleHttp\Psr7\Response;
8
use seregazhuk\HeadHunterApi\Contracts\HttpInterface;
9
10
class GuzzleHttpAdapter implements HttpInterface
11
{
12
    /**
13
     * @var Client
14
     */
15
    protected $client;
16
17
    /**
18
     * @var
19
     */
20
    protected $headers;
21
22
    public function __construct($baseUrl)
23
    {
24
        $this->client = new Client(['base_uri' => $baseUrl]);
25
    }
26
27
    /**
28
     * @param mixed $headers
29
     * @return GuzzleHttpAdapter
30
     */
31
    public function setHeaders($headers)
32
    {
33
        $this->headers = $headers;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @param string $uri
40
     * @param array $params
41
     * @return array|null
42
     */
43
    public function get($uri, $params = [])
44
    {
45
        if(!empty($params)){
46
            $uri .= '?'. http_build_query($params);
47
        }
48
49
        return $this->executeRequest('GET', $uri);
50
    }
51
52
    /**
53
     * @param string $uri
54
     * @param array $params
55
     * @return array|null
56
     */
57
    public function post($uri, $params = [])
58
    {
59
        return $this->executeRequest(
60
            'POST', $uri, ['query' => $params]
61
        );
62
    }
63
64
    /**
65
     * @param string $uri
66
     * @param array $params
67
     * @return array|null
68
     */
69
    public function postJson($uri, $params = [])
70
    {
71
        return $this->executeRequest(
72
            'POST', $uri, ['json' => $params]
73
        );
74
    }
75
76
    /**
77
     * @param string $uri
78
     * @param array $params
79
     * @return array|null
80
     */
81
    public function put($uri, $params = [])
82
    {
83
        return $this->executeRequest(
84
            'PUT', $uri, ['query' => $params]
85
        );
86
    }
87
88
    /**
89
     * @param string $uri
90
     * @param array $params
91
     * @return array|null
92
     */
93
    public function putJson($uri, $params = [])
94
    {
95
        return $this->executeRequest(
96
            'PUT', $uri, ['json' => $params]
97
        );
98
    }
99
100
    /**
101
     * @param string $uri
102
     * @param null $headers
103
     * @return array|null
104
     */
105
    public function delete($uri, $headers = null)
106
    {
107
        return $this->executeRequest('DELETE', $uri);
108
    }
109
110
    /**
111
     * @param Response $response
112
     * @return array|null
113
     */
114
    private function parseResponse(Response $response)
115
    {
116
        return json_decode($response->getBody(), true);
117
    }
118
119
    /**
120
     * @param string $method
121
     * @param string $uri
122
     * @param array $options
123
     * @return array|null
124
     */
125
    protected function executeRequest($method, $uri, array $options = [])
126
    {
127
        $request = new Request($method, $uri, $this->headers);
128
129
        $response = $this->client->send($request, $options);
130
131
        return $this->parseResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
132
    }
133
}