Completed
Pull Request — master (#46)
by Sergey
03:20
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 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
    public function followers($username, $batchesLimit = 0)
63
    {
64
        return $this->getPaginatedData(
65
            $username, UrlHelper::RESOURCE_USER_FOLLOWERS, "/$username/followers/", $batchesLimit
66
        );
67
    }
68
69
    /**
70
     * Get pinner following other pinners
71
     *
72
     * @param string $username
73
     * @param int $batchesLimit
74
     * @return Iterator
75
     */
76
    public function following($username, $batchesLimit = 0)
77
    {
78
        return $this->getPaginatedData(
79
            $username, UrlHelper::RESOURCE_USER_FOLLOWING, "/$username/following/", $batchesLimit
80
        );
81
    }
82
83
    /**
84
     * Get pinner pins
85
     *
86
     * @param string $username
87
     * @param int $batchesLimit
88
     * @return Iterator
89
     */
90
    public function pins($username, $batchesLimit = 0)
91
    {
92
        return $this->getPaginatedData(
93
            $username, UrlHelper::RESOURCE_USER_PINS, "/$username/$username/", $batchesLimit
94
        );
95
    }
96
97
    /**
98
     * Login as pinner
99
     *
100
     * @param string $username
101
     * @param string $password
102
     * @return bool
103
     */
104
    public function login($username, $password)
105
    {
106
        if ($this->request->isLoggedIn()) {
107
            return true;
108
        }
109
110
        $this->_checkCredentials($username, $password);
111
112
        $post = PinnerHelper::createLoginRequest($username, $password);
113
        $postString = UrlHelper::buildRequestString($post);
114
        $this->request->clearToken();
115
        $result = $this->response->checkErrorInResponse($this->request->exec(UrlHelper::RESOURCE_LOGIN, $postString));
116
        if ($result) {
117
            $this->request->setLoggedIn();
118
        }
119
120
        return $result;
121
    }
122
123
    /**
124
     * Validates password and login
125
     * @param string $username
126
     * @param string $password
127
     */
128
    protected function _checkCredentials($username, $password)
129
    {
130
        if ( ! $username || ! $password) {
131
            throw new LogicException('You must set username and password to login.');
132
        }
133
    }
134
135
    /**
136
     * Search scope
137
     *
138
     * @return string
139
     */
140
    protected function getScope()
141
    {
142
        return 'people';
143
    }
144
145
    protected function getEntityIdName()
146
    {
147
        return Request::PINNER_ENTITY_ID;
148
    }
149
150
    /**
151
     * Follow resource
152
     *
153
     * @return string
154
     */
155
    protected function getFollowUrl()
156
    {
157
        return UrlHelper::RESOURCE_FOLLOW_USER;
158
    }
159
160
    /**
161
     * UnFollow resource
162
     * @return string
163
     */
164
    protected function getUnfFollowUrl()
165
    {
166
        return UrlHelper::RESOURCE_UNFOLLOW_USER;
167
    }
168
169
    /**
170
     * @param string $username
171
     * @param string $url
172
     * @param string $sourceUrl
173
     * @param integer $batchesLimit
174
     * @return Iterator
175
     */
176
    protected function getPaginatedData($username, $url, $sourceUrl, $batchesLimit)
177
    {
178
        $data = [
179
            ['username' => $username],
180
            $url,
181
            $sourceUrl
182
        ];
183
184
        return Pagination::getPaginatedData([$this, 'getData'], $data, $batchesLimit);
185
    }
186
}
187