Completed
Push — master ( 523146...d3886c )
by Sergey
03:44
created

Pinners::checkCredentials()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
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\Exceptions\AuthException;
9
use seregazhuk\PinterestBot\Helpers\Pagination;
10
use seregazhuk\PinterestBot\Helpers\Providers\Traits\FollowersTrait;
11
use seregazhuk\PinterestBot\Helpers\UrlHelper;
12
use seregazhuk\PinterestBot\Helpers\Requests\PinnerHelper;
13
use seregazhuk\PinterestBot\Helpers\Providers\Traits\SearchTrait;
14
use seregazhuk\PinterestBot\Helpers\Providers\Traits\FollowTrait;
15
16
class Pinners extends Provider
17
{
18
    use SearchTrait, FollowTrait, FollowersTrait;
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
     * @return null|array
32
     */
33
    public function info($username)
34
    {
35
        $res = $this->getPaginatedData($username, UrlHelper::RESOURCE_USER_INFO, "/$username/", 1);
36
        $res = iterator_to_array($res);
37
38
        return ! empty($res) ? $res[0] : null;
39
    }
40
41
    /**
42
     * Get pinner followers
43
     *
44
     * @param string $username
45
     * @param int $batchesLimit
46
     * @return Iterator
47
     */
48
    public function followers($username, $batchesLimit = 0)
49
    {
50
        return $this->getPaginatedData(
51
            $username, UrlHelper::RESOURCE_USER_FOLLOWERS, "/$username/followers/", $batchesLimit
52
        );
53
    }
54
55
    /**
56
     * Get pinner following other pinners
57
     *
58
     * @param string $username
59
     * @param int $batchesLimit
60
     * @return Iterator
61
     */
62
    public function following($username, $batchesLimit = 0)
63
    {
64
        return $this->getPaginatedData(
65
            $username, UrlHelper::RESOURCE_USER_FOLLOWING, "/$username/following/", $batchesLimit
66
        );
67
    }
68
69
    /**
70
     * Get pinner pins
71
     *
72
     * @param string $username
73
     * @param int $batchesLimit
74
     * @return Iterator
75
     */
76
    public function pins($username, $batchesLimit = 0)
77
    {
78
        return $this->getPaginatedData(
79
            $username, UrlHelper::RESOURCE_USER_PINS, "/$username/$username/", $batchesLimit
80
        );
81
    }
82
83
    /**
84
     * Login as pinner
85
     *
86
     * @param string $username
87
     * @param string $password
88
     * @return bool
89
     * @throws AuthException
90
     */
91
    public function login($username, $password)
92
    {
93
        if ($this->request->isLoggedIn()) {
94
            return true;
95
        }
96
97
        $this->checkCredentials($username, $password);
98
99
        $post = PinnerHelper::createLoginRequest($username, $password);
100
        $postString = UrlHelper::buildRequestString($post);
101
        $this->request->clearToken();
102
103
        $response = $this->request->exec(UrlHelper::RESOURCE_LOGIN, $postString);
104
        $result = $this->response->checkErrorInResponse($response);
105
        if ( ! $result) {
106
            throw new AuthException($this->response->getLastError()['message']);
107
        }
108
        $this->request->setLoggedIn();
109
        return $result;
110
    }
111
112
    /**
113
     * Validates password and login
114
     *
115
     * @param string $username
116
     * @param string $password
117
     */
118
    protected function checkCredentials($username, $password)
119
    {
120
        if ( ! $username || ! $password) {
121
            throw new LogicException('You must set username and password to login.');
122
        }
123
    }
124
125
    /**
126
     * Search scope
127
     *
128
     * @return string
129
     */
130
    protected function getScope()
131
    {
132
        return 'people';
133
    }
134
135
    protected function getEntityIdName()
136
    {
137
        return Request::PINNER_ENTITY_ID;
138
    }
139
140
    /**
141
     * Follow resource
142
     *
143
     * @return string
144
     */
145
    protected function getFollowUrl()
146
    {
147
        return UrlHelper::RESOURCE_FOLLOW_USER;
148
    }
149
150
    /**
151
     * UnFollow resource
152
     * @return string
153
     */
154
    protected function getUnfFollowUrl()
155
    {
156
        return UrlHelper::RESOURCE_UNFOLLOW_USER;
157
    }
158
159
    /**
160
     * Wrapper over Pagination::getPaginatedData for 
161
     * high-level functions, such as 'following', 'pins' and others.
162
     *
163
     * @param string $username
164
     * @param string $url
165
     * @param string $sourceUrl
166
     * @param integer $batchesLimit
167
     * @return Iterator
168
     */
169
    protected function getPaginatedData($username, $url, $sourceUrl, $batchesLimit)
170
    {
171
        $data = [
172
            ['username' => $username],
173
            $url,
174
            $sourceUrl
175
        ];
176
177
        return Pagination::getPaginatedData([$this, 'getData'], $data, $batchesLimit);
178
    }
179
}
180