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 WBW\Library\Pexels\API\SubstituteRequestInterface; |
18
|
|
|
use WBW\Library\Pexels\Exception\APIException; |
19
|
|
|
use WBW\Library\Pexels\Model\AbstractRequest; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Abstract provider. |
23
|
|
|
* |
24
|
|
|
* @author webeweb <https://github.com/webeweb/> |
25
|
|
|
* @package WBW\Library\Pexels\Provider |
26
|
|
|
* @abstract |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractProvider { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Endpoint path. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
const ENDPOINT_PATH = "https://api.pexels.com"; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Debug. |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $debug; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
*/ |
47
|
|
|
public function __construct() { |
48
|
|
|
$this->setDebug(false); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Build a resource path. |
53
|
|
|
* |
54
|
|
|
* @param AbstractRequest $request The request. |
55
|
|
|
* @return string Returns the resource path. |
56
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
57
|
|
|
*/ |
58
|
|
|
private function buildResourcePath(AbstractRequest $request) { |
59
|
|
|
|
60
|
|
|
if (false === ($request instanceof SubstituteRequestInterface)) { |
61
|
|
|
return $request->getResourcePath(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (null === $request->getSubstituteValue()) { |
65
|
|
|
throw new InvalidArgumentException(sprintf("The substitute value %s is missing", $request->getSubstituteName())); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return str_replace($request->getSubstituteName(), $request->getSubstituteValue(), $request->getResourcePath()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Call the API. |
73
|
|
|
* |
74
|
|
|
* @param AbstractRequest $request The request. |
75
|
|
|
* @param array $queryData The query data. |
76
|
|
|
* @return string Returns the raw response. |
77
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
78
|
|
|
*/ |
79
|
|
|
protected function callAPI(AbstractRequest $request, array $queryData) { |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
|
83
|
|
|
$client = new Client([ |
84
|
|
|
"base_uri" => self::ENDPOINT_PATH . "/", |
85
|
|
|
"debug" => $this->getDebug(), |
86
|
|
|
"headers" => [ |
87
|
|
|
"Accept" => "application/json", |
88
|
|
|
"User-Agent" => "webeweb/pexels-library", |
89
|
|
|
"Authorization" => $request->getAuthorization(), |
90
|
|
|
], |
91
|
|
|
"synchronous" => true, |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$uri = substr($this->buildResourcePath($request), 1); |
95
|
|
|
$options = [ |
96
|
|
|
"query" => $queryData, |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$response = $client->request("GET", $uri, $options); |
100
|
|
|
|
101
|
|
|
return $response->getBody()->getContents(); |
102
|
|
|
} catch (InvalidArgumentException $ex) { |
103
|
|
|
|
104
|
|
|
throw $ex; |
105
|
|
|
} catch (Exception $ex) { |
106
|
|
|
|
107
|
|
|
throw new APIException("Call Pexels API failed", $ex); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the debug. |
113
|
|
|
* |
114
|
|
|
* @return bool Returns the debug. |
115
|
|
|
*/ |
116
|
|
|
public function getDebug() { |
117
|
|
|
return $this->debug; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the debug. |
122
|
|
|
* |
123
|
|
|
* @param bool $debug The debug. |
124
|
|
|
* @return AbstractProvider Returns this provider. |
125
|
|
|
*/ |
126
|
|
|
public function setDebug($debug) { |
127
|
|
|
$this->debug = $debug; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|