Completed
Push — master ( 3a34ef...58fbb5 )
by Sergey
40:07 queued 32:54
created

Pinners::myAccountName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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