Completed
Pull Request — master (#19)
by Sergey
03:32
created

Pinners::getFollowUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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 seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Helpers\UrlHelper;
7
use seregazhuk\PinterestBot\Helpers\Requests\PinnerHelper;
8
use seregazhuk\PinterestBot\Helpers\Providers\FollowHelper;
9
use seregazhuk\PinterestBot\Helpers\Providers\SearchHelper;
10
11
class Pinners extends Provider
12
{
13
    use SearchHelper, FollowHelper;
14
15
    /**
16
     * Get different user data, for example, followers, following, pins.
17
     * Collects data while paginating with bookmarks through pinterest results.
18
     * Return array. Key data - for results and key bookmarks - for pagination.
19
     *
20
     * @param string $username
21
     * @param string $url
22
     * @param string $sourceUrl
23
     * @param array  $bookmarks
24
     * @return array
25
     */
26
    public function getUserData($username, $url, $sourceUrl, $bookmarks = [])
27
    {
28
        $get = PinnerHelper::createUserDataRequest($username, $sourceUrl, $bookmarks);
29
        $getString = UrlHelper::buildRequestString($get);
30
        $response = $this->request->exec($url.'?'.$getString, $username);
31
32
        return $this->response->getPaginationData($response);
33
    }
34
35
    /**
36
     * @param string $username
37
     * @param string $resourceUrl
38
     * @param string $sourceUrl
39
     * @param int    $batchesLimit
40
     * @return \Iterator
41
     */
42
    public function getPaginatedUserData($username, $resourceUrl, $sourceUrl, $batchesLimit = 0)
43
    {
44
        return $this->getPaginatedData(
45
            [$this, 'getUserData'], [
46
            'username'  => $username,
47
            'url'       => $resourceUrl,
48
            'sourceUrl' => $sourceUrl,
49
        ], $batchesLimit
50
        );
51
    }
52
53
    /**
54
     * Get the logged-in account username
55
     *
56
     * @return array|null
57
     */
58
    public function myAccountName()
59
    {
60
        $this->request->checkLoggedIn();
61
        $res = $this->request->exec(UrlHelper::RESOURCE_GET_ACCOUNT_NAME);
62
63
        return PinnerHelper::parseAccountNameResponse($res);
64
    }
65
66
    /**
67
     * Get user info
68
     * If username param is not specified, will
69
     * return info for logged user
70
     *
71
     * @param string $username
72
     * @return null|array
73
     */
74
    public function info($username)
75
    {
76
        $res = $this->getUserData($username, UrlHelper::RESOURCE_USER_INFO, "/$username/");
77
78
        return isset($res['data']) ? $res['data'] : null;
79
    }
80
81
    /**
82
     * Get pinner followers
83
     *
84
     * @param string $username
85
     * @param int    $batchesLimit
86
     * @return \Iterator
87
     */
88
    public function followers($username, $batchesLimit = 0)
89
    {
90
        return $this->getPaginatedUserData(
91
            $username, UrlHelper::RESOURCE_USER_FOLLOWERS, "/$username/followers/", $batchesLimit
92
        );
93
    }
94
95
    /**
96
     * Get pinner following other pinners
97
     *
98
     * @param string $username
99
     * @param int    $batchesLimit
100
     * @return \Iterator
101
     */
102
    public function following($username, $batchesLimit = 0)
103
    {
104
        return $this->getPaginatedUserData(
105
            $username, UrlHelper::RESOURCE_USER_FOLLOWING, "/$username/following/", $batchesLimit
106
        );
107
    }
108
109
    /**
110
     * Get pinner pins
111
     *
112
     * @param string $username
113
     * @param int    $batchesLimit
114
     * @return \Iterator
115
     */
116
    public function pins($username, $batchesLimit = 0)
117
    {
118
        return $this->getPaginatedUserData(
119
            $username, UrlHelper::RESOURCE_USER_PINS, "/$username/pins/", $batchesLimit
120
        );
121
    }
122
123
    /**
124
     * Login as pinner
125
     *
126
     * @param $username
127
     * @param $password
128
     * @return bool
129
     */
130
    public function login($username, $password)
131
    {
132
        if ($this->request->isLoggedIn()) {
133
            return true;
134
        }
135
136
        $post = PinnerHelper::createLoginRequest($username, $password);
137
        $postString = UrlHelper::buildRequestString($post);
138
        $this->request->clearToken();
139
        $result = $this->response->checkErrorInResponse($this->request->exec(UrlHelper::RESOURCE_LOGIN, $postString));
140
        if ($result) {
141
            $this->request->setLoggedIn();
142
        }
143
144
        return $result;
145
    }
146
147
    protected function getScope()
148
    {
149
        return 'people';
150
    }
151
152
    protected function getEntityIdName()
153
    {
154
        return Request::PINNER_ENTITY_ID;
155
    }
156
157
    protected function getFollowUrl()
158
    {
159
        return UrlHelper::RESOURCE_FOLLOW_USER;
160
    }
161
162
    protected function getUnfFollowUrl()
163
    {
164
        return UrlHelper::RESOURCE_UNFOLLOW_USER;
165
    }
166
}
167