1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\HeadHunterApi\Adapters; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use seregazhuk\HeadHunterApi\Contracts\HttpInterface; |
9
|
|
|
|
10
|
|
|
class GuzzleHttpAdapter implements HttpInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Client |
14
|
|
|
*/ |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
protected $headers; |
21
|
|
|
|
22
|
|
|
public function __construct($baseUrl) |
23
|
|
|
{ |
24
|
|
|
$this->client = new Client(['base_uri' => $baseUrl]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed $headers |
29
|
|
|
* @return GuzzleHttpAdapter |
30
|
|
|
*/ |
31
|
|
|
public function setHeaders($headers) |
32
|
|
|
{ |
33
|
|
|
$this->headers = $headers; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $uri |
40
|
|
|
* @param array $params |
41
|
|
|
* @return array|null |
42
|
|
|
*/ |
43
|
|
|
public function get($uri, $params = []) |
44
|
|
|
{ |
45
|
|
|
if(!empty($params)){ |
46
|
|
|
$uri .= '?'. http_build_query($params); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->executeRequest('GET', $uri); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $uri |
54
|
|
|
* @param array $params |
55
|
|
|
* @param bool $useJson |
56
|
|
|
* @return array|null |
57
|
|
|
*/ |
58
|
|
|
public function post($uri, $params = [], $useJson = false) |
59
|
|
|
{ |
60
|
|
|
return $this->executeRequest( |
61
|
|
|
'POST', $uri, $this->makeOptions($params, $useJson) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $uri |
67
|
|
|
* @param array $params |
68
|
|
|
* @param bool $useJson |
69
|
|
|
* @return array|null |
70
|
|
|
*/ |
71
|
|
|
public function put($uri, $params = [], $useJson = false) |
72
|
|
|
{ |
73
|
|
|
return $this->executeRequest( |
74
|
|
|
'PUT', $uri, $this->makeOptions($params, $useJson) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $uri |
80
|
|
|
* @param null $headers |
81
|
|
|
* @return array|null |
82
|
|
|
*/ |
83
|
|
|
public function delete($uri, $headers = null) |
84
|
|
|
{ |
85
|
|
|
return $this->executeRequest('DELETE', $uri); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Response $response |
90
|
|
|
* @return array|null |
91
|
|
|
*/ |
92
|
|
|
private function parseResponse(Response $response) |
93
|
|
|
{ |
94
|
|
|
return json_decode($response->getBody(), true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $method |
99
|
|
|
* @param string $uri |
100
|
|
|
* @param array $options |
101
|
|
|
* @return array|null |
102
|
|
|
*/ |
103
|
|
|
protected function executeRequest($method, $uri, array $options = []) |
104
|
|
|
{ |
105
|
|
|
$request = new Request($method, $uri, $this->headers); |
106
|
|
|
|
107
|
|
|
$response = $this->client->send($request, $options); |
108
|
|
|
|
109
|
|
|
return $this->parseResponse($response); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $params |
114
|
|
|
* @param $json |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
protected function makeOptions(array $params = [], $json) |
118
|
|
|
{ |
119
|
|
|
$optionsKey = $json ? 'json' : 'query'; |
120
|
|
|
|
121
|
|
|
return [$optionsKey => $params]; |
122
|
|
|
} |
123
|
|
|
} |
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.