1
|
|
|
<?php |
2
|
|
|
namespace ArianRashidi\PocketApi; |
3
|
|
|
|
4
|
|
|
use ArianRashidi\PocketApi\Api\Add; |
5
|
|
|
use ArianRashidi\PocketApi\Api\Authentication; |
6
|
|
|
use ArianRashidi\PocketApi\Api\Modify; |
7
|
|
|
use ArianRashidi\PocketApi\Api\Retrieve; |
8
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Pocket |
12
|
|
|
* |
13
|
|
|
* @package ArianRashidi\PocketApi |
14
|
|
|
*/ |
15
|
|
|
class Pocket |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Consumer key. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $consumerKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Access token. |
26
|
|
|
* |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
protected $accessToken = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Http client. |
33
|
|
|
* |
34
|
|
|
* @var GuzzleClient|null |
35
|
|
|
*/ |
36
|
|
|
protected $httpClient = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Pocket's authentication api. |
40
|
|
|
* |
41
|
|
|
* @var Authentication|null |
42
|
|
|
*/ |
43
|
|
|
protected $authenticationApi = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Pocket's add api. |
47
|
|
|
* |
48
|
|
|
* @var Add|null |
49
|
|
|
*/ |
50
|
|
|
protected $addApi = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Pocket's modify api. |
54
|
|
|
* |
55
|
|
|
* @var Modify|null |
56
|
|
|
*/ |
57
|
|
|
protected $modifyApi = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Pocket's retrieve api. |
61
|
|
|
* |
62
|
|
|
* @var Retrieve|null |
63
|
|
|
*/ |
64
|
|
|
protected $retrieveApi = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Pocket constructor. |
68
|
|
|
* |
69
|
|
|
* @param string $consumerKey |
70
|
|
|
* @param string|null $accessToken |
71
|
|
|
* @param GuzzleClient|null $httpClient |
72
|
|
|
*/ |
73
|
|
|
public function __construct(string $consumerKey, string $accessToken = null, GuzzleClient $httpClient = null) |
74
|
|
|
{ |
75
|
|
|
$this->checkKey($consumerKey, 'Consumer key'); |
76
|
|
|
|
77
|
|
|
if (!is_null($accessToken)) { |
78
|
|
|
$this->checkKey($accessToken, 'Access token'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->consumerKey = $consumerKey; |
82
|
|
|
$this->accessToken = $accessToken; |
83
|
|
|
$this->setHttpClient($httpClient); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Pocket's authentication api. |
88
|
|
|
* |
89
|
|
|
* @return Authentication |
90
|
|
|
*/ |
91
|
|
|
public function authenticationApi(): Authentication |
92
|
|
|
{ |
93
|
|
|
if (is_null($this->authenticationApi)) { |
94
|
|
|
$this->authenticationApi = new Authentication($this); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->authenticationApi; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Pocket's add api. |
102
|
|
|
* |
103
|
|
|
* @return Add |
104
|
|
|
*/ |
105
|
|
|
public function addApi(): Add |
106
|
|
|
{ |
107
|
|
|
$this->checkAccessToken(); |
108
|
|
|
|
109
|
|
|
if (is_null($this->addApi)) { |
110
|
|
|
$this->addApi = new Add($this); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->addApi; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Pocket's modify api. |
118
|
|
|
* |
119
|
|
|
* @return Modify |
120
|
|
|
*/ |
121
|
|
|
public function modifyApi(): Modify |
122
|
|
|
{ |
123
|
|
|
$this->checkAccessToken(); |
124
|
|
|
|
125
|
|
|
if (is_null($this->modifyApi)) { |
126
|
|
|
$this->modifyApi = new Modify($this); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->modifyApi; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Pocket's retrieve api. |
134
|
|
|
* |
135
|
|
|
* @return Retrieve |
136
|
|
|
*/ |
137
|
|
|
public function retrieveApi(): Retrieve |
138
|
|
|
{ |
139
|
|
|
$this->checkAccessToken(); |
140
|
|
|
|
141
|
|
|
if (is_null($this->retrieveApi)) { |
142
|
|
|
$this->retrieveApi = new Retrieve($this); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->retrieveApi; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the consumer key. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getConsumerKey(): string |
154
|
|
|
{ |
155
|
|
|
$this->checkKey($this->consumerKey, 'Consumer key'); |
156
|
|
|
return $this->consumerKey; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set the consumer key. |
161
|
|
|
* |
162
|
|
|
* @param string $consumerKey |
163
|
|
|
*/ |
164
|
|
|
public function setConsumerKey(string $consumerKey) |
165
|
|
|
{ |
166
|
|
|
$this->checkKey($consumerKey, 'Consumer key'); |
167
|
|
|
$this->consumerKey = $consumerKey; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the access token. |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getAccessToken(): string |
176
|
|
|
{ |
177
|
|
|
$this->checkKey($this->accessToken, 'Access token'); |
178
|
|
|
return $this->accessToken; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Set the access token. |
183
|
|
|
* |
184
|
|
|
* @param string $accessToken |
185
|
|
|
*/ |
186
|
|
|
public function setAccessToken(string $accessToken) |
187
|
|
|
{ |
188
|
|
|
$this->checkKey($accessToken, 'Access token'); |
189
|
|
|
$this->accessToken = $accessToken; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the Guzzle http client. |
194
|
|
|
* |
195
|
|
|
* @return GuzzleClient |
196
|
|
|
*/ |
197
|
|
|
public function getHttpClient(): GuzzleClient |
198
|
|
|
{ |
199
|
|
|
return $this->httpClient; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set the Guzzle http client. |
204
|
|
|
* |
205
|
|
|
* @param GuzzleClient|null $httpClient |
206
|
|
|
*/ |
207
|
|
|
public function setHttpClient(GuzzleClient $httpClient = null) |
208
|
|
|
{ |
209
|
|
|
if (is_null($httpClient)) { |
210
|
|
|
$httpClient = new GuzzleClient(['base_uri' => 'https://getpocket.com']); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$this->httpClient = $httpClient; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Check a key. |
218
|
|
|
* |
219
|
|
|
* @param $key |
220
|
|
|
* @param string $name |
221
|
|
|
* |
222
|
|
|
* @throws PocketException |
223
|
|
|
*/ |
224
|
|
|
protected function checkKey($key, string $name) |
225
|
|
|
{ |
226
|
|
|
if (empty($key)) { |
227
|
|
|
throw new PocketException($name . ' is not defined.'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (strlen($key) < 30) { |
231
|
|
|
throw new PocketException($name . ' is to short.'); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Check if the access token is set. |
237
|
|
|
* |
238
|
|
|
* @throws PocketException |
239
|
|
|
*/ |
240
|
|
|
protected function checkAccessToken() |
241
|
|
|
{ |
242
|
|
|
if (is_null($this->accessToken)) { |
243
|
|
|
throw new PocketException('No Access Token is set. Use setAccessToken().'); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|