Completed
Push — master ( 457d5c...f99e05 )
by Sergey
05:31 queued 02:43
created

Request::makeUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace seregazhuk\HeadHunterApi;
4
5
use GuzzleHttp\Client;
6
use Psr\Http\Message\ResponseInterface;
7
use GuzzleHttp\Psr7\Request as GuzzleRequest;
8
9
class Request
10
{
11
    const BASE_URL = 'https://api.hh.ru/';
12
13
    /**
14
     * @var \GuzzleHttp\Client
15
     */
16
    protected $client;
17
18
    /**
19
     * @var array
20
     */
21
    protected $headers = [];
22
23
    /**
24
     * @var string
25
     */
26
    protected $locale = 'RU';
27
28
    /**
29
     * @var string
30
     */
31
    protected $host = 'hh.ru';
32
33
    /**
34
     * @param string $token
35
     */
36
    public function __construct($token = null)
37
    {
38
        $this->client = new Client([
39
            'base_uri'    => self::BASE_URL,
40
            'http_errors' => false,
41
        ]);
42
43
        if ($token) $this->setToken($token);
0 ignored issues
show
Bug Best Practice introduced by
The expression $token of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
44
    }
45
46
    /**
47
     * @param string $uri
48
     * @param array $params
49
     * @return array|null
50
     */
51
    public function get($uri, $params = [])
52
    {
53
        $uri = $this->makeUriWithQuery($uri, $params);
54
55
        return $this->executeRequest('GET', $uri);
56
    }
57
58
    /**
59
     * @param string $uri
60
     * @param array $params
61
     * @return array|null
62
     */
63
    public function post($uri, $params = [])
64
    {
65
        return $this->executeRequest(
66
            'POST', $uri, ['query' => $params]
67
        );
68
    }
69
70
    /**
71
     * @param string $uri
72
     * @param array $params
73
     * @return array|null
74
     */
75
    public function postJson($uri, $params = [])
76
    {
77
        return $this->executeRequest(
78
            'POST', $uri, ['json' => $params]
79
        );
80
    }
81
82
    /**
83
     * @param string $uri
84
     * @param array $params
85
     * @return array|null
86
     */
87
    public function postFile($uri, $params = [])
88
    {
89
        return $this->executeRequest(
90
            'POST', $uri, ['multipart' => $params]
91
        );
92
    }
93
94
    /**
95
     * @param string $uri
96
     * @param array $params
97
     * @return array|null
98
     */
99
    public function put($uri, $params = [])
100
    {
101
        return $this->executeRequest(
102
            'PUT', $uri, ['query' => $params]
103
        );
104
    }
105
106
    /**
107
     * @param string $uri
108
     * @param array $params
109
     * @return array|null
110
     */
111
    public function putJson($uri, $params = [])
112
    {
113
        return $this->executeRequest(
114
            'PUT', $uri, ['json' => $params]
115
        );
116
    }
117
118
    /**
119
     * @param string $uri
120
     * @param array $params
121
     * @return array|null
122
     */
123
    public function delete($uri, $params = [])
124
    {
125
        $uri = $this->makeUriWithQuery($uri, $params);
126
127
        return $this->executeRequest('DELETE', $uri);
128
    }
129
130
    /**
131
     * @param ResponseInterface $response
132
     * @return array|null
133
     */
134
    protected function parseResponse(ResponseInterface $response)
135
    {
136
        return json_decode($response->getBody(), true);
137
    }
138
139
    /**
140
     * @param string $method
141
     * @param string $uri
142
     * @param array $options
143
     * @return array|null
144
     */
145
    protected function executeRequest($method, $uri, array $options = [])
146
    {
147
        $request = new GuzzleRequest($method, $uri, $this->headers);
148
149
        $response = $this->client->send($request, $options);
150
151
        return $this->parseResponse($response);
152
    }
153
154
    /**
155
     * @param string $locale
156
     * @return Request
157
     */
158
    public function setLocale($locale)
159
    {
160
        $this->locale = $locale;
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param $params
167
     * @return string
168
     */
169
    protected function makeQueryString($params = [])
170
    {
171
        $customOptions = [
172
            'host'   => $this->host,
173
            'locale' => $this->locale,
174
        ];
175
176
        $params = array_merge(
177
            $params, $customOptions
178
        );
179
180
        return http_build_query($params);
181
    }
182
183
    /**
184
     * @param string $host
185
     * @return Request
186
     */
187
    public function setHost($host)
188
    {
189
        $this->host = $host;
190
191
        return $this;
192
    }
193
194
    /**
195
     * @param string $token
196
     * @return $this
197
     */
198
    public function setToken($token)
199
    {
200
        $this->addAuthHeader($token);
201
202
        return $this;
203
    }
204
205
    /**
206
     * @param string $token
207
     */
208
    protected function addAuthHeader($token)
209
    {
210
        $this->headers = ['Authorization' => 'Bearer ' . $token];
211
    }
212
213
    /**
214
     * @param $uri
215
     * @param $params
216
     * @return string
217
     */
218
    protected function makeUriWithQuery($uri, $params)
219
    {
220
        if (!empty($params)) {
221
            $uri .= '?' . $this->makeQueryString($params);
222
        }
223
224
        return $uri;
225
    }
226
}