|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers\Core; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
|
6
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
|
7
|
|
|
use function seregazhuk\PinterestBot\class_uses_recursive; |
|
8
|
|
|
use seregazhuk\PinterestBot\Helpers\Pagination; |
|
9
|
|
|
use seregazhuk\PinterestBot\Api\ProvidersContainer; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Provider. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class Provider |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* List of methods that require logged status. |
|
18
|
|
|
* |
|
19
|
|
|
* @return array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $loginRequiredFor = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Instance of the API Request. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Request |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $request; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Response |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $response; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ProvidersContainer |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $container; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param ProvidersContainer $container |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(ProvidersContainer $container) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->container = $container; |
|
46
|
|
|
$this->request = $container->getRequest(); |
|
47
|
|
|
$this->response = $container->getResponse(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Executes a POST request to Pinterest API. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $resourceUrl |
|
54
|
|
|
* @param array $requestOptions |
|
55
|
|
|
* @param bool $returnData |
|
56
|
|
|
* @return bool|array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function post($resourceUrl, array $requestOptions = [], $returnData = false) |
|
59
|
|
|
{ |
|
60
|
|
|
$postString = $this->request->createQuery($requestOptions); |
|
61
|
|
|
|
|
62
|
|
|
// When executing POST request we need a csrf-token. |
|
63
|
|
|
$this->initTokenIfRequired(); |
|
64
|
|
|
|
|
65
|
|
|
$this->execute($resourceUrl, $postString); |
|
66
|
|
|
|
|
67
|
|
|
return $returnData ? $this->response->getResponseData() : $this->response->isOk(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Executes a GET request to Pinterest API. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $resourceUrl |
|
74
|
|
|
* @param array $requestOptions |
|
75
|
|
|
* @return array|bool |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function get($resourceUrl = '', array $requestOptions = []) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->execute($resourceUrl . $this->makeQueryString($requestOptions)); |
|
80
|
|
|
|
|
81
|
|
|
return $this->response->getResponseData(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $url |
|
86
|
|
|
* @param string $postString |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function execute($url, $postString = '') |
|
90
|
|
|
{ |
|
91
|
|
|
$result = $this->request->exec($url, $postString); |
|
92
|
|
|
|
|
93
|
|
|
$this->response->fillFromJson($result); |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $method |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function checkMethodRequiresLogin($method) |
|
104
|
|
|
{ |
|
105
|
|
|
$methodsThatRequireLogin = array_merge($this->loginRequiredFor, $this->requiresLoginFor()); |
|
106
|
|
|
|
|
107
|
|
|
return in_array($method, $methodsThatRequireLogin); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function requiresLoginFor() |
|
114
|
|
|
{ |
|
115
|
|
|
$loginRequired = []; |
|
116
|
|
|
|
|
117
|
|
|
foreach (class_parents($this) + class_uses_recursive($this) as $traitOrParent) { |
|
118
|
|
|
$class = basename(str_replace('\\', '/', $traitOrParent)); |
|
119
|
|
|
|
|
120
|
|
|
if (method_exists($traitOrParent, $method = 'requiresLoginFor' . $class)) { |
|
121
|
|
|
$loginRequired = array_merge($loginRequired, forward_static_call([$this, $method])); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $loginRequired; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
public function isLoggedIn() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->request->isLoggedIn(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $resourceUrl |
|
138
|
|
|
* @param mixed $data |
|
139
|
|
|
* @param int $limit |
|
140
|
|
|
* @return Pagination |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function paginate($resourceUrl, $data, $limit = Pagination::DEFAULT_LIMIT) |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->paginateCustom( |
|
145
|
|
|
function () use ($data, $resourceUrl) { |
|
146
|
|
|
$this->get($resourceUrl, $data); |
|
147
|
|
|
return $this->response; |
|
148
|
|
|
} |
|
149
|
|
|
)->take($limit); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Accepts callback which should return PaginatedResponse object. |
|
154
|
|
|
* |
|
155
|
|
|
* @param callable $callback |
|
156
|
|
|
* @param int $limit |
|
157
|
|
|
* @return Pagination |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function paginateCustom(callable $callback, $limit = Pagination::DEFAULT_LIMIT) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->response->clear(); |
|
162
|
|
|
|
|
163
|
|
|
return (new Pagination)->paginateOver($callback)->take($limit); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return Response |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getResponse() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->response; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return Request |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getRequest() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->request; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
protected function initTokenIfRequired() |
|
183
|
|
|
{ |
|
184
|
|
|
if ($this->request->hasToken()) { |
|
185
|
|
|
return; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// Simply visit main page to fill the cookies |
|
189
|
|
|
// and parse a token from them |
|
190
|
|
|
$this->get(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param array $requestOptions |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function makeQueryString(array $requestOptions) |
|
198
|
|
|
{ |
|
199
|
|
|
if (empty($requestOptions)) { |
|
200
|
|
|
return ''; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return '?' . $this->request->createQuery( |
|
204
|
|
|
$requestOptions, $this->response->getBookmarks() |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|