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\Exception\GuzzleException; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use WBW\Library\Pexels\Api\PaginateResponseInterface; |
17
|
|
|
use WBW\Library\Pexels\Request\AbstractRequest; |
18
|
|
|
use WBW\Library\Pexels\Response\AbstractResponse; |
19
|
|
|
use WBW\Library\Pexels\Response\PhotosResponse; |
20
|
|
|
use WBW\Library\Pexels\Response\VideosResponse; |
21
|
|
|
use WBW\Library\Provider\Exception\ApiException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* API provider. |
25
|
|
|
* |
26
|
|
|
* @author webeweb <https://github.com/webeweb> |
27
|
|
|
* @package WBW\Library\Pexels\Provider |
28
|
|
|
*/ |
29
|
|
|
class ApiProvider extends AbstractProvider { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Populate a response. |
33
|
|
|
* |
34
|
|
|
* @param AbstractResponse $response The response. |
35
|
|
|
* @return AbstractResponse Returns the response. |
36
|
|
|
*/ |
37
|
|
|
protected function populateResponse(AbstractResponse $response): AbstractResponse { |
38
|
|
|
|
39
|
|
|
$response->setLimit($this->getLimit()); |
40
|
|
|
$response->setRemaining($this->getRemaining()); |
41
|
|
|
$response->setReset($this->getReset()); |
42
|
|
|
|
43
|
|
|
return $response; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
/** |
47
|
|
|
* Request a next page. |
48
|
5 |
|
* |
49
|
5 |
|
* @param PaginateResponseInterface $response The response. |
50
|
5 |
|
* @return PhotosResponse|VideosResponse Returns the response. |
51
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
52
|
5 |
|
* @throws GuzzleException Throws a Guzzle exception if an error occurs. |
53
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
54
|
|
|
*/ |
55
|
|
|
public function requestNextPage(PaginateResponseInterface $response): AbstractResponse { |
56
|
|
|
|
57
|
|
|
$rawResponse = $this->callApiWithResponse($response); |
58
|
|
|
|
59
|
|
|
/** @var PhotosResponse|VideosResponse */ |
60
|
|
|
return $this->populateResponse($response->deserializeResponse($rawResponse)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
10 |
|
* Request a previous page. |
65
|
|
|
* |
66
|
10 |
|
* @param PaginateResponseInterface $response The response. |
67
|
|
|
* @return PhotosResponse|VideosResponse Returns the response. |
68
|
10 |
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
69
|
|
|
* @throws GuzzleException Throws a Guzzle exception if an error occurs. |
70
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
71
|
|
|
*/ |
72
|
|
|
public function requestPrevPage(PaginateResponseInterface $response): AbstractResponse { |
73
|
|
|
|
74
|
|
|
$rawResponse = $this->callApiWithResponse($response, false); |
75
|
|
|
|
76
|
|
|
/** @var PhotosResponse|VideosResponse */ |
77
|
|
|
return $this->populateResponse($response->deserializeResponse($rawResponse)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Send a request. |
82
|
10 |
|
* |
83
|
|
|
* @param AbstractRequest $request The request. |
84
|
10 |
|
* @return AbstractResponse Returns the response. |
85
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
86
|
|
|
* @throws GuzzleException Throws a Guzzle exception if an error occurs. |
87
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
88
|
|
|
*/ |
89
|
|
|
public function sendRequest(AbstractRequest $request): AbstractResponse { |
90
|
|
|
|
91
|
|
|
$queryData = $request->serializeRequest(); |
92
|
|
|
$rawResponse = $this->callApiWithRequest($request, $queryData); |
93
|
|
|
|
94
|
|
|
return $this->populateResponse($request->deserializeResponse($rawResponse)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|