Passed
Branch master (d58387)
by Sergey
03:31
created

Pinners::getUserData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

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