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