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