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