Completed
Pull Request — master (#32)
by Sergey
09:01 queued 05:17
created

Pinners::_checkCredentials()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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