Completed
Push — master ( df586c...625f62 )
by Sergey
04:49 queued 02:14
created

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