1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
use LogicException; |
7
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
8
|
|
|
use seregazhuk\PinterestBot\Helpers\Pagination; |
9
|
|
|
use seregazhuk\PinterestBot\Helpers\Providers\Traits\FollowersTrait; |
10
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlHelper; |
11
|
|
|
use seregazhuk\PinterestBot\Helpers\Requests\PinnerHelper; |
12
|
|
|
use seregazhuk\PinterestBot\Helpers\Providers\Traits\SearchTrait; |
13
|
|
|
use seregazhuk\PinterestBot\Helpers\Providers\Traits\FollowTrait; |
14
|
|
|
|
15
|
|
|
class Pinners extends Provider |
16
|
|
|
{ |
17
|
|
|
use SearchTrait, FollowTrait, FollowersTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get different user data, for example, followers, following, pins. |
21
|
|
|
* Collects data while paginating with bookmarks through pinterest results. |
22
|
|
|
* Return array. Key data - for results and key bookmarks - for pagination. |
23
|
|
|
* |
24
|
|
|
* @param string $username |
25
|
|
|
* @param string $url |
26
|
|
|
* @param string $sourceUrl |
27
|
|
|
* @param array $bookmarks |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function getUserData($username, $url, $sourceUrl, $bookmarks = []) |
31
|
|
|
{ |
32
|
|
|
$data = ['options' => ['username' => $username]]; |
33
|
|
|
$get = Request::createRequestData($data, $sourceUrl, $bookmarks); |
34
|
|
|
$getString = UrlHelper::buildRequestString($get); |
35
|
|
|
$response = $this->request->exec($url.'?'.$getString); |
36
|
|
|
|
37
|
|
|
return $this->response->getPaginationData($response); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get user info |
42
|
|
|
* If username param is not specified, will |
43
|
|
|
* return info for logged user |
44
|
|
|
* |
45
|
|
|
* @param string $username |
46
|
|
|
* @return null|array |
47
|
|
|
*/ |
48
|
|
|
public function info($username) |
49
|
|
|
{ |
50
|
|
|
$res = $this->getUserData($username, UrlHelper::RESOURCE_USER_INFO, "/$username/"); |
51
|
|
|
|
52
|
|
|
return isset($res['data']) ? $res['data'] : null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get pinner followers |
57
|
|
|
* |
58
|
|
|
* @param string $username |
59
|
|
|
* @param int $batchesLimit |
60
|
|
|
* @return Iterator |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function followers($username, $batchesLimit = 0) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$data = [ |
65
|
|
|
['username' => $username], |
66
|
|
|
UrlHelper::RESOURCE_USER_FOLLOWERS, |
67
|
|
|
"/$username/followers/" |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
return Pagination::getPaginatedData([$this, 'getData'], $data, $batchesLimit); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get pinner following other pinners |
75
|
|
|
* |
76
|
|
|
* @param string $username |
77
|
|
|
* @param int $batchesLimit |
78
|
|
|
* @return Iterator |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function following($username, $batchesLimit = 0) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$data = [ |
83
|
|
|
['username' => $username], |
84
|
|
|
UrlHelper::RESOURCE_USER_FOLLOWING, |
85
|
|
|
"/$username/following/" |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
return Pagination::getPaginatedData([$this, 'getData'], $data, $batchesLimit); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get pinner pins |
93
|
|
|
* |
94
|
|
|
* @param string $username |
95
|
|
|
* @param int $batchesLimit |
96
|
|
|
* @return Iterator |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function pins($username, $batchesLimit = 0) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$data = [ |
101
|
|
|
['username' => $username], |
102
|
|
|
UrlHelper::RESOURCE_USER_PINS, |
103
|
|
|
"/$username/$username/" |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
return Pagination::getPaginatedData([$this, 'getData'], $data, $batchesLimit); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Login as pinner |
111
|
|
|
* |
112
|
|
|
* @param string $username |
113
|
|
|
* @param string $password |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function login($username, $password) |
117
|
|
|
{ |
118
|
|
|
if ($this->request->isLoggedIn()) { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->_checkCredentials($username, $password); |
123
|
|
|
|
124
|
|
|
$post = PinnerHelper::createLoginRequest($username, $password); |
125
|
|
|
$postString = UrlHelper::buildRequestString($post); |
126
|
|
|
$this->request->clearToken(); |
127
|
|
|
$result = $this->response->checkErrorInResponse($this->request->exec(UrlHelper::RESOURCE_LOGIN, $postString)); |
128
|
|
|
if ($result) { |
129
|
|
|
$this->request->setLoggedIn(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $result; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Validates password and login |
137
|
|
|
* @param string $username |
138
|
|
|
* @param string $password |
139
|
|
|
*/ |
140
|
|
|
protected function _checkCredentials($username, $password) |
141
|
|
|
{ |
142
|
|
|
if ( ! $username || ! $password) { |
143
|
|
|
throw new LogicException('You must set username and password to login.'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Search scope |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function getScope() |
153
|
|
|
{ |
154
|
|
|
return 'people'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function getEntityIdName() |
158
|
|
|
{ |
159
|
|
|
return Request::PINNER_ENTITY_ID; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Follow resource |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
protected function getFollowUrl() |
168
|
|
|
{ |
169
|
|
|
return UrlHelper::RESOURCE_FOLLOW_USER; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* UnFollow resource |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
protected function getUnfFollowUrl() |
177
|
|
|
{ |
178
|
|
|
return UrlHelper::RESOURCE_UNFOLLOW_USER; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
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.