1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pexels-library package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Library\Pexels\Provider; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use Exception; |
16
|
|
|
use GuzzleHttp\Client; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use WBW\Library\Pexels\API\PaginateResponseInterface; |
19
|
|
|
use WBW\Library\Pexels\API\SubstituteRequestInterface; |
20
|
|
|
use WBW\Library\Pexels\Exception\APIException; |
21
|
|
|
use WBW\Library\Pexels\Model\AbstractRequest; |
22
|
|
|
use WBW\Library\Pexels\Traits\RateLimitTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Abstract provider. |
26
|
|
|
* |
27
|
|
|
* @author webeweb <https://github.com/webeweb/> |
28
|
|
|
* @package WBW\Library\Pexels\Provider |
29
|
|
|
* @abstract |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractProvider { |
32
|
|
|
|
33
|
|
|
use RateLimitTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Endpoint path. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
const ENDPOINT_PATH = "https://api.pexels.com"; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Authorization. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $authorization; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Debug. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
private $debug; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor. |
58
|
|
|
*/ |
59
|
|
|
public function __construct() { |
60
|
|
|
$this->setDebug(false); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Build a resource path. |
65
|
|
|
* |
66
|
|
|
* @param AbstractRequest $request The request. |
67
|
|
|
* @return string Returns the resource path. |
68
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
69
|
|
|
*/ |
70
|
|
|
private function buildResourcePath(AbstractRequest $request) { |
71
|
|
|
|
72
|
|
|
if (false === ($request instanceof SubstituteRequestInterface)) { |
73
|
|
|
return $request->getResourcePath(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (null === $request->getSubstituteValue()) { |
77
|
|
|
throw new InvalidArgumentException(sprintf("The substitute value %s is missing", $request->getSubstituteName())); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return str_replace($request->getSubstituteName(), $request->getSubstituteValue(), $request->getResourcePath()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Call the API. |
85
|
|
|
* |
86
|
|
|
* @param string $uri The URI. |
87
|
|
|
* @param array $queryData The query data. |
88
|
|
|
* @return string Returns the raw response. |
89
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
90
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
91
|
|
|
*/ |
92
|
|
|
private function callAPI($uri, array $queryData) { |
93
|
|
|
|
94
|
|
|
if (null === $this->getAuthorization()) { |
95
|
|
|
throw new InvalidArgumentException("The mandatory parameter \"authorization\" is missing"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
|
100
|
|
|
$client = new Client([ |
101
|
|
|
"debug" => $this->getDebug(), |
102
|
|
|
"headers" => [ |
103
|
|
|
"Accept" => "application/json", |
104
|
|
|
"User-Agent" => "webeweb/pexels-library", |
105
|
|
|
"Authorization" => $this->getAuthorization(), |
106
|
|
|
], |
107
|
|
|
"synchronous" => true, |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$options = 0 < count($queryData) ? ["query" => $queryData] : []; |
111
|
|
|
|
112
|
|
|
$response = $client->request("GET", $uri, $options); |
113
|
|
|
|
114
|
|
|
$this->setLimit(intval($response->getHeaderLine("X-Ratelimit-Limit"))); |
115
|
|
|
$this->setRemaining(intval($response->getHeaderLine("X-Ratelimit-Remaining"))); |
116
|
|
|
$this->setReset(new DateTime("@" . $response->getHeaderLine("X-Ratelimit-Reset"))); |
117
|
|
|
|
118
|
|
|
return $response->getBody()->getContents(); |
119
|
|
|
} catch (Exception $ex) { |
120
|
|
|
|
121
|
|
|
throw new APIException("Call Pexels API failed", $ex); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Call the API. |
127
|
|
|
* |
128
|
|
|
* @param AbstractRequest $request The request. |
129
|
|
|
* @param array $queryData The query data. |
130
|
|
|
* @return string Returns the raw response. |
131
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
132
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
133
|
|
|
*/ |
134
|
|
|
protected function callAPIWithRequest(AbstractRequest $request, array $queryData) { |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
|
138
|
|
|
$uri = self::ENDPOINT_PATH . $this->buildResourcePath($request); |
139
|
|
|
|
140
|
|
|
return $this->callAPI($uri, $queryData); |
141
|
|
|
} catch (InvalidArgumentException $ex) { |
142
|
|
|
|
143
|
|
|
throw $ex; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Call the API. |
149
|
|
|
* |
150
|
|
|
* @param PaginateResponseInterface $response The request. |
151
|
|
|
* @param bool $nextPage Next page ?. |
152
|
|
|
* @return string Returns the raw response. |
153
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
154
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
155
|
|
|
*/ |
156
|
|
|
protected function callAPIWithResponse(PaginateResponseInterface $response, $nextPage) { |
157
|
|
|
|
158
|
|
|
try { |
159
|
|
|
|
160
|
|
|
$uri = false === $nextPage ? $response->getPrevPage() : $response->getNextPage(); |
161
|
|
|
if (null === $uri) { |
162
|
|
|
return ""; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this->callAPI($uri, []); |
166
|
|
|
} catch (InvalidArgumentException $ex) { |
167
|
|
|
|
168
|
|
|
throw $ex; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the authorization. |
174
|
|
|
* |
175
|
|
|
* @return string Returns the authorization. |
176
|
|
|
*/ |
177
|
|
|
public function getAuthorization() { |
178
|
|
|
return $this->authorization; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the debug. |
183
|
|
|
* |
184
|
|
|
* @return bool Returns the debug. |
185
|
|
|
*/ |
186
|
|
|
public function getDebug() { |
187
|
|
|
return $this->debug; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set the authorization. |
192
|
|
|
* |
193
|
|
|
* @param string $authorization The authorization. |
194
|
|
|
* @return AbstractProvider Returns this provider. |
195
|
|
|
*/ |
196
|
|
|
public function setAuthorization($authorization) { |
197
|
|
|
$this->authorization = $authorization; |
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set the debug. |
203
|
|
|
* |
204
|
|
|
* @param bool $debug The debug. |
205
|
|
|
* @return AbstractProvider Returns this provider. |
206
|
|
|
*/ |
207
|
|
|
public function setDebug($debug) { |
208
|
|
|
$this->debug = $debug; |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|