Completed
Push — master ( af83f4...b4262b )
by Sergey
03:54 queued 01:20
created

Pinners::getEntityIdName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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