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 GuzzleHttp\Client; |
||
15 | use GuzzleHttp\Exception\GuzzleException; |
||
16 | use InvalidArgumentException; |
||
17 | use Psr\Log\LoggerInterface; |
||
18 | use Throwable; |
||
19 | use WBW\Library\Pexels\Api\PaginateResponseInterface; |
||
20 | use WBW\Library\Pexels\Request\AbstractRequest; |
||
21 | use WBW\Library\Provider\AbstractProvider as BaseProvider; |
||
22 | use WBW\Library\Provider\Exception\ApiException; |
||
23 | use WBW\Library\Traits\Compounds\CompoundRateLimitTrait; |
||
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 CompoundRateLimitTrait; |
||
35 | 1 | ||
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|null $authorization The authorization. |
||
54 | * @param LoggerInterface|null $logger The logger. |
||
55 | */ |
||
56 | public function __construct(string $authorization = null, LoggerInterface $logger = null) { |
||
57 | 80 | parent::__construct($logger); |
|
58 | 80 | ||
59 | 80 | $this->setAuthorization($authorization); |
|
60 | 80 | } |
|
61 | 80 | ||
62 | /** |
||
63 | * Build the configuration. |
||
64 | * |
||
65 | * @return array<string,mixed> Returns the configuration. |
||
66 | */ |
||
67 | private function buildConfiguration(): array { |
||
68 | 50 | ||
69 | return [ |
||
70 | 50 | "debug" => $this->getDebug(), |
|
71 | "headers" => [ |
||
72 | 50 | "Accept" => "application/json", |
|
73 | 50 | "User-Agent" => "webeweb/pexels-library", |
|
74 | 50 | "Authorization" => $this->getAuthorization(), |
|
75 | ], |
||
76 | "synchronous" => true, |
||
77 | ]; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Call the API. |
||
82 | * |
||
83 | * @param string $uri The URI. |
||
84 | * @param array<string,mixed> $queryData The query data. |
||
85 | * @return string Returns the raw response. |
||
86 | * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
||
87 | 60 | * @throws GuzzleException Throws a Guzzle exception if an error occurs. |
|
88 | * @throws ApiException Throws an API exception if an error occurs. |
||
89 | 60 | */ |
|
90 | 45 | private function callApi(string $uri, array $queryData): string { |
|
91 | |||
92 | if (null === $this->getAuthorization()) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
93 | 15 | throw $this->newMandatoryParameterException("authorization"); |
|
94 | 5 | } |
|
95 | |||
96 | try { |
||
97 | 10 | ||
98 | $config = $this->buildConfiguration(); |
||
99 | $client = new Client($config); |
||
100 | |||
101 | $method = "GET"; |
||
102 | $options = 0 < count($queryData) ? ["query" => $queryData] : []; |
||
103 | |||
104 | $this->logInfo(sprintf("Call Pexels API %s %s", $method, $uri), ["config" => $config, "options" => $options]); |
||
105 | |||
106 | $response = $client->request($method, $uri, $options); |
||
107 | |||
108 | $this->setLimit(intval($response->getHeaderLine("X-Ratelimit-Limit"))); |
||
109 | $this->setRemaining(intval($response->getHeaderLine("X-Ratelimit-Remaining"))); |
||
110 | 55 | ||
111 | return $response->getBody()->getContents(); |
||
112 | 55 | } catch (Throwable $ex) { |
|
113 | 5 | ||
114 | throw new ApiException("Call Pexels API failed", 500, $ex); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | 50 | /** |
|
119 | * Call the API with a request. |
||
120 | 50 | * |
|
121 | * @param AbstractRequest $request The request. |
||
122 | 50 | * @param array<string,mixed> $queryData The query data. |
|
123 | 50 | * @return string Returns the raw response. |
|
124 | * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
||
125 | 50 | * @throws GuzzleException Throws a Guzzle exception if an error occurs. |
|
126 | * @throws ApiException Throws an API exception if an error occurs. |
||
127 | 50 | */ |
|
128 | protected function callApiWithRequest(AbstractRequest $request, array $queryData): string { |
||
129 | |||
130 | $uri = self::ENDPOINT_PATH . $this->buildResourcePath($request); |
||
131 | |||
132 | return $this->callApi($uri, $queryData); |
||
133 | 50 | } |
|
134 | |||
135 | 50 | /** |
|
136 | * Call the API with a response. |
||
137 | * |
||
138 | * @param PaginateResponseInterface $response The request. |
||
139 | * @param bool $nextPage Next page ? |
||
140 | * @return string Returns the raw response. |
||
141 | * @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
||
142 | * @throws GuzzleException Throws a Guzzle exception if an error occurs. |
||
143 | * @throws ApiException Throws an API exception if an error occurs. |
||
144 | */ |
||
145 | protected function callApiWithResponse(PaginateResponseInterface $response, bool $nextPage = true): string { |
||
146 | |||
147 | $uri = true === $nextPage ? $response->getNextPage() : $response->getPrevPage(); |
||
148 | if (null === $uri) { |
||
149 | 60 | return ""; |
|
150 | } |
||
151 | |||
152 | return $this->callApi($uri, []); |
||
153 | 60 | } |
|
154 | |||
155 | 55 | /** |
|
156 | 60 | * Get the authorization. |
|
157 | * |
||
158 | 10 | * @return string|null Returns the authorization. |
|
159 | */ |
||
160 | public function getAuthorization(): ?string { |
||
161 | return $this->authorization; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set the authorization. |
||
166 | * |
||
167 | * @param string|null $authorization The authorization. |
||
168 | * @return AbstractProvider Returns this provider. |
||
169 | */ |
||
170 | public function setAuthorization(?string $authorization): AbstractProvider { |
||
171 | $this->authorization = $authorization; |
||
172 | return $this; |
||
173 | } |
||
174 | } |
||
175 |