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