Completed
Push — master ( 7d4223...27be2f )
by Sergey
08:26 queued 05:47
created

Request::addAuthHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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