|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
|
6
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
|
7
|
|
|
use seregazhuk\PinterestBot\Helpers\Pagination; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Provider. |
|
11
|
|
|
* @property string entityIdName |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Provider |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $returnData = true; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* List of methods that require logged status. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $loginRequiredFor = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Instance of the API Request. |
|
30
|
|
|
* |
|
31
|
|
|
* @var Request |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $request; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Response |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $response; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Request $request |
|
42
|
|
|
* @param Response $response |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Request $request, Response $response) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->request = $request; |
|
47
|
|
|
$this->response = $response; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Executes a POST request to Pinterest API. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $requestOptions |
|
54
|
|
|
* @param string $resourceUrl |
|
55
|
|
|
* @param bool $returnResponse |
|
56
|
|
|
* |
|
57
|
|
|
* @return Response|bool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function execPostRequest($requestOptions, $resourceUrl, $returnResponse = false) |
|
60
|
|
|
{ |
|
61
|
|
|
$postString = Request::createQuery($requestOptions); |
|
62
|
|
|
|
|
63
|
|
|
$this->execute($resourceUrl, $postString); |
|
64
|
|
|
|
|
65
|
|
|
return $returnResponse ? $this->response : $this->response->isOk(); |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Executes a GET request to Pinterest API. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $requestOptions |
|
73
|
|
|
* @param string $resourceUrl |
|
74
|
|
|
* @return array|bool |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
protected function execGetRequest(array $requestOptions = [], $resourceUrl = '') |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$query = Request::createQuery($requestOptions); |
|
79
|
|
|
|
|
80
|
|
|
$this->execute($resourceUrl . "?{$query}"); |
|
81
|
|
|
|
|
82
|
|
|
return $this->response->getResponseData(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Executes a GET request to Pinterest API with pagination. |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $requestOptions |
|
89
|
|
|
* @param string $resourceUrl |
|
90
|
|
|
* @param array $bookmarks |
|
91
|
|
|
* @return Response |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $bookmarks = []) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$query = Request::createQuery($requestOptions, $bookmarks); |
|
96
|
|
|
|
|
97
|
|
|
$this->execute($resourceUrl . "?{$query}"); |
|
98
|
|
|
|
|
99
|
|
|
return $this->response; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $url |
|
104
|
|
|
* @param string $postString |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function execute($url, $postString = "") |
|
108
|
|
|
{ |
|
109
|
|
|
$result = $this->request->exec($url, $postString); |
|
110
|
|
|
|
|
111
|
|
|
$this->processResult($result); |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getEntityIdName() |
|
120
|
|
|
{ |
|
121
|
|
|
return property_exists($this, 'entityIdName') ? $this->entityIdName : ''; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Executes pagination GET request. |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $data |
|
128
|
|
|
* @param string $url |
|
129
|
|
|
* @param array $bookmarks |
|
130
|
|
|
* @return Response |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getPaginatedData(array $data, $url, $bookmarks = []) |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->execGetRequestWithPagination($data, $url, $bookmarks); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $method |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
public function checkMethodRequiresLogin($method) |
|
143
|
|
|
{ |
|
144
|
|
|
return in_array($method, $this->loginRequiredFor); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
|
|
public function isLoggedIn() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->request->isLoggedIn(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param array $params |
|
157
|
|
|
* @param int $limit |
|
158
|
|
|
* @param string $method |
|
159
|
|
|
* @return mixed |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function getPaginatedResponse(array $params, $limit, $method = 'getPaginatedData') |
|
162
|
|
|
{ |
|
163
|
|
|
return (new Pagination($this))->paginateOver($method, $params, $limit); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $res |
|
168
|
|
|
* @return Response |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function processResult($res) |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->response->fill(json_decode($res, true)); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.