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

Request::putJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
130
131
        return $this;
132
    }
133
}