Completed
Pull Request — master (#32)
by Sergey
04:49 queued 01:06
created

Pinners::info()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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