Request::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\HeadHunterApi;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Request as GuzzleRequest;
7
8
class Request
9
{
10
    const BASE_URL = 'https://api.hh.ru/';
11
12
    /**
13
     * @var Client
14
     */
15
    protected $client;
16
17
    /**
18
     * @var array
19
     */
20
    protected $headers = [];
21
22
    /**
23
     * @var string
24
     */
25
    protected $locale = 'RU';
26
27
    /**
28
     * @var string
29
     */
30
    protected $host = 'hh.ru';
31
32
    /**
33
     * @param string $token
34
     */
35
    public function __construct($token = null)
36
    {
37
        $this->client = new Client([
38
            'base_uri'    => self::BASE_URL,
39
            'http_errors' => false,
40
        ]);
41
42
        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...
43
    }
44
45
    /**
46
     * @param string $uri
47
     * @param array $params
48
     * @return array|null
49
     */
50
    public function get($uri, $params = [])
51
    {
52
        $uri = $this->makeUriWithQuery($uri, $params);
53
54
        return $this->executeRequest('GET', $uri);
55
    }
56
57
    /**
58
     * @param string $uri
59
     * @param array $params
60
     * @return array|null
61
     */
62
    public function post($uri, $params = [])
63
    {
64
        return $this->executeRequest(
65
            'POST', $uri, ['query' => $params]
66
        );
67
    }
68
69
    /**
70
     * @param string $uri
71
     * @param array $params
72
     * @return array|null
73
     */
74
    public function postJson($uri, $params = [])
75
    {
76
        return $this->executeRequest(
77
            'POST', $uri, ['json' => $params]
78
        );
79
    }
80
81
    /**
82
     * @param string $uri
83
     * @param array $params
84
     * @return array|null
85
     */
86
    public function postFile($uri, $params = [])
87
    {
88
        return $this->executeRequest(
89
            'POST', $uri, ['multipart' => $params]
90
        );
91
    }
92
93
    /**
94
     * @param string $uri
95
     * @param array $params
96
     * @return array|null
97
     */
98
    public function put($uri, $params = [])
99
    {
100
        return $this->executeRequest(
101
            'PUT', $uri, ['query' => $params]
102
        );
103
    }
104
105
    /**
106
     * @param string $uri
107
     * @param array $params
108
     * @return array|null
109
     */
110
    public function putJson($uri, $params = [])
111
    {
112
        return $this->executeRequest(
113
            'PUT', $uri, ['json' => $params]
114
        );
115
    }
116
117
    /**
118
     * @param string $uri
119
     * @param array $params
120
     * @return array|null
121
     */
122
    public function delete($uri, $params = [])
123
    {
124
        $uri = $this->makeUriWithQuery($uri, $params);
125
126
        return $this->executeRequest('DELETE', $uri);
127
    }
128
129
    /**
130
     * @param string $method
131
     * @param string $uri
132
     * @param array $options
133
     * @return array|null
134
     */
135
    protected function executeRequest($method, $uri, array $options = [])
136
    {
137
        $request = new GuzzleRequest($method, $uri, $this->headers);
138
139
        $response = $this->client->send($request, $options);
140
141
	    return json_decode($response->getBody(), true);
142
    }
143
144
    /**
145
     * @param string $locale
146
     * @return $this
147
     */
148
    public function setLocale($locale)
149
    {
150
        $this->locale = $locale;
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param $params
157
     * @return string
158
     */
159
    protected function makeQueryString($params = [])
160
    {
161
        $customOptions = [
162
            'host'   => $this->host,
163
            'locale' => $this->locale,
164
        ];
165
166
        // Merge specified params with defaults
167
        $params = array_merge(
168
            $customOptions,
169
            $params
170
        );
171
172
        return http_build_query($params);
173
    }
174
175
    /**
176
     * @param string $host
177
     * @return $this
178
     */
179
    public function setHost($host)
180
    {
181
        $this->host = $host;
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param string $token
188
     * @return $this
189
     */
190
    public function setToken($token)
191
    {
192
	    $this->headers = ['Authorization' => 'Bearer ' . $token];
193
194
        return $this;
195
    }
196
197
    /**
198
     * @param string $uri
199
     * @param array $params
200
     * @return string
201
     */
202
    protected function makeUriWithQuery($uri, array $params = [])
203
    {
204
        if (!empty($params)) {
205
            $uri .= '?' . $this->makeQueryString($params);
206
        }
207
208
        return $uri;
209
    }
210
}
211