|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stevenmaguire\Yelp\Tool; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
7
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
8
|
|
|
use GuzzleHttp\Psr7\Request; |
|
9
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
|
11
|
|
|
use Stevenmaguire\Yelp\Exception\ClientConfigurationException; |
|
12
|
|
|
use Stevenmaguire\Yelp\Exception\HttpException; |
|
13
|
|
|
|
|
14
|
|
|
trait HttpTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* API host url |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $apiHost; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* HTTP client |
|
25
|
|
|
* |
|
26
|
|
|
* @var \GuzzleHttp\Client |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $httpClient; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Prepares and appends parameters, if provided, to the given url. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $url |
|
34
|
|
|
* @param array $parameters |
|
35
|
|
|
* @param array $options |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
16 |
|
protected function appendParametersToUrl($url, array $parameters = array(), array $options = array()) |
|
40
|
|
|
{ |
|
41
|
16 |
|
$url = rtrim($url, '?'); |
|
42
|
16 |
|
$queryString = $this->prepareQueryParams($parameters, $options); |
|
43
|
|
|
|
|
44
|
16 |
|
if ($queryString) { |
|
45
|
14 |
|
$uri = new Uri($url); |
|
46
|
14 |
|
$existingQuery = $uri->getQuery(); |
|
47
|
14 |
|
$updatedQuery = empty($existingQuery) ? $queryString : $existingQuery . '&' . $queryString; |
|
48
|
14 |
|
$url = (string) $uri->withQuery($updatedQuery); |
|
49
|
7 |
|
} |
|
50
|
|
|
|
|
51
|
16 |
|
return $url; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the yelp client's http client to the given http client. Client. |
|
56
|
|
|
* |
|
57
|
|
|
* @return GuzzleHttp\Client|null |
|
58
|
|
|
*/ |
|
59
|
38 |
|
public function getHttpClient() |
|
60
|
|
|
{ |
|
61
|
38 |
|
return $this->httpClient; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Creates a PSR-7 Request instance. |
|
66
|
|
|
* |
|
67
|
|
|
* @param null|string $method HTTP method for the request. |
|
68
|
|
|
* @param null|string $uri URI for the request. |
|
69
|
|
|
* @param array $headers Headers for the message. |
|
70
|
|
|
* @param string|resource|StreamInterface $body Message body. |
|
71
|
|
|
* @param string $version HTTP protocol version. |
|
72
|
|
|
* |
|
73
|
|
|
* @return GuzzleHttp\Psr7\Request |
|
74
|
|
|
*/ |
|
75
|
18 |
|
public function getRequest( |
|
76
|
|
|
$method, |
|
77
|
|
|
$uri, |
|
78
|
|
|
array $headers = [], |
|
79
|
|
|
$body = null, |
|
80
|
|
|
$version = '1.1' |
|
81
|
|
|
) { |
|
82
|
18 |
|
$uri = new Uri($uri); |
|
83
|
|
|
|
|
84
|
18 |
|
if (!$uri->getHost()) { |
|
85
|
18 |
|
$uri = $uri->withHost($this->apiHost); |
|
86
|
9 |
|
} |
|
87
|
|
|
|
|
88
|
18 |
|
if (!$uri->getScheme()) { |
|
89
|
18 |
|
$uri = $uri->withScheme('https'); |
|
90
|
9 |
|
} |
|
91
|
|
|
|
|
92
|
18 |
|
return new Request($method, $uri, $headers, $body, $version); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Sends a request instance and returns a response instance. |
|
97
|
|
|
* |
|
98
|
|
|
* WARNING: This method does not attempt to catch exceptions caused by HTTP |
|
99
|
|
|
* errors! It is recommended to wrap this method in a try/catch block. |
|
100
|
|
|
* |
|
101
|
|
|
* @param RequestInterface $request |
|
102
|
|
|
* @return ResponseInterface |
|
103
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
104
|
|
|
*/ |
|
105
|
14 |
|
public function getResponse(RequestInterface $request) |
|
106
|
|
|
{ |
|
107
|
|
|
try { |
|
108
|
14 |
|
return $this->getHttpClient()->send($request); |
|
109
|
2 |
|
} catch (BadResponseException $e) { |
|
110
|
2 |
|
$exception = new HttpException($e->getMessage()); |
|
111
|
|
|
|
|
112
|
2 |
|
throw $exception->setResponseBody($e->getResponse()->getBody()); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Updates query params array to apply yelp specific formatting rules. |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $params |
|
120
|
|
|
* @param array $options |
|
|
|
|
|
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function prepareQueryParams($params = [], $csvParams = []) |
|
125
|
|
|
{ |
|
126
|
32 |
|
$updateParam = function ($value, $key) use (&$params, $csvParams) { |
|
127
|
22 |
|
if (is_bool($value)) { |
|
128
|
4 |
|
$params[$key] = $value ? 'true' : 'false'; |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
22 |
|
if (in_array($key, $csvParams) && is_array($value)) { |
|
132
|
2 |
|
$params[$key] = implode(',', $value); |
|
133
|
1 |
|
} |
|
134
|
32 |
|
}; |
|
135
|
|
|
|
|
136
|
32 |
|
array_walk($params, $updateParam); |
|
137
|
|
|
|
|
138
|
32 |
|
return http_build_query($params); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Makes a request to the Yelp API and returns the response |
|
143
|
|
|
* |
|
144
|
|
|
* @param Psr\Http\Message\RequestInterface $request |
|
145
|
|
|
* |
|
146
|
|
|
* @return stdClass The JSON response from the request |
|
147
|
|
|
* @throws Stevenmaguire\Yelp\Exception\ClientConfigurationException |
|
148
|
|
|
* @throws Stevenmaguire\Yelp\Exception\HttpException |
|
149
|
|
|
*/ |
|
150
|
14 |
|
protected function processRequest(RequestInterface $request) |
|
151
|
|
|
{ |
|
152
|
14 |
|
$response = $this->getResponse($request); |
|
153
|
|
|
|
|
154
|
12 |
|
return json_decode($response->getBody()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Updates the yelp client's http client to the given http client. Client. |
|
159
|
|
|
* |
|
160
|
|
|
* @param GuzzleHttp\Client $client |
|
161
|
|
|
* |
|
162
|
|
|
* @return mixed |
|
163
|
|
|
*/ |
|
164
|
44 |
|
public function setHttpClient(HttpClient $client) |
|
165
|
|
|
{ |
|
166
|
44 |
|
$this->httpClient = $client; |
|
167
|
|
|
|
|
168
|
44 |
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.