Completed
Pull Request — master (#99)
by Sergey
04:35 queued 02:05
created

Pinners::paginate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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