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

Pinners::getScope()   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\SearchHelper;
8
use seregazhuk\PinterestBot\Helpers\Requests\PinnerHelper;
9
10
class Pinners extends Provider
11
{
12
    use SearchHelper;
13
14
    /**
15
     * Follow user by user_id
16
     *
17
     * @param integer $userId
18
     * @return bool
19
     */
20 View Code Duplication
    public function follow($userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $this->request->checkLoggedIn();
23
24
        $response = $this->request->followMethodCall($userId, Request::PINNER_ENTITY_ID, UrlHelper::RESOURCE_FOLLOW_USER);
25
        return $this->response->checkErrorInResponse($response);
26
    }
27
28
    /**
29
     * Unfollow user by user_id
30
     *
31
     * @param integer $userId
32
     * @return bool
33
     */
34 View Code Duplication
    public function unFollow($userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $this->request->checkLoggedIn();
37
38
        $response = $this->request->followMethodCall($userId, Request::PINNER_ENTITY_ID, UrlHelper::RESOURCE_UNFOLLOW_USER);
39
        return $this->response->checkErrorInResponse($response);
40
    }
41
42
    /**
43
     * Get different user data, for example, followers, following, pins.
44
     * Collects data while paginating with bookmarks through pinterest results.
45
     * Return array. Key data - for results and key bookmarks - for pagination.
46
     *
47
     * @param string $username
48
     * @param string $url
49
     * @param string $sourceUrl
50
     * @param array  $bookmarks
51
     * @return array
52
     */
53
    public function getUserData($username, $url, $sourceUrl, $bookmarks = [])
54
    {
55
        $get = PinnerHelper::createUserDataRequest($username, $sourceUrl, $bookmarks);
56
        $getString = UrlHelper::buildRequestString($get);
57
        $response = $this->request->exec($url . '?' . $getString, $username);
58
        return $this->response->getPaginationData($response);
59
    }
60
61
    /**
62
     * @param string $username
63
     * @param string $resourceUrl
64
     * @param string $sourceUrl
65
     * @param int     $batchesLimit
66
     * @return \Iterator
67
     */
68
    public function getPaginatedUserData($username, $resourceUrl, $sourceUrl, $batchesLimit = 0)
69
    {
70
        return $this->getPaginatedData(
71
            [$this, 'getUserData'],
72
            [
73
                'username'  => $username,
74
                'url'       => $resourceUrl,
75
                'sourceUrl' => $sourceUrl,
76
            ],
77
            $batchesLimit
78
        );
79
    }
80
81
    /**
82
     * Get the logged-in account username
83
     *
84
     * @return array|null
85
     */
86
    public function myAccountName()
87
    {
88
        $this->request->checkLoggedIn();
89
        $res = $this->request->exec(UrlHelper::RESOURCE_GET_ACCOUNT_NAME);
90
91
        return PinnerHelper::parseAccountNameResponse($res);
92
    }
93
94
    /**
95
     * Get user info
96
     * If username param is not specified, will
97
     * return info for logged user
98
     *
99
     * @param string $username
100
     * @return null|array
101
     */
102
    public function info($username)
103
    {
104
        $res = $this->getUserData($username, UrlHelper::RESOURCE_USER_INFO, "/$username/");
105
        return isset($res['data']) ? $res['data'] : null;
106
    }
107
108
    /**
109
     * Get pinner followers
110
     *
111
     * @param string $username
112
     * @param int    $batchesLimit
113
     * @return \Iterator
114
     */
115
    public function followers($username, $batchesLimit = 0)
116
    {
117
        return $this->getPaginatedUserData(
118
            $username, UrlHelper::RESOURCE_USER_FOLLOWERS, "/$username/followers/", $batchesLimit
119
        );
120
    }
121
122
    /**
123
     * Get pinner following other pinners
124
     *
125
     * @param string $username
126
     * @param int    $batchesLimit
127
     * @return \Iterator
128
     */
129
    public function following($username, $batchesLimit = 0)
130
    {
131
        return $this->getPaginatedUserData(
132
            $username, UrlHelper::RESOURCE_USER_FOLLOWING, "/$username/following/", $batchesLimit
133
        );
134
    }
135
136
    /**
137
     * Get pinner pins
138
     *
139
     * @param string $username
140
     * @param int    $batchesLimit
141
     * @return \Iterator
142
     */
143
    public function pins($username, $batchesLimit = 0)
144
    {
145
        return $this->getPaginatedUserData(
146
            $username, UrlHelper::RESOURCE_USER_PINS, "/$username/pins/", $batchesLimit
147
        );
148
    }
149
150
    /**
151
     * Login as pinner
152
     *
153
     * @param $username
154
     * @param $password
155
     * @return bool
156
     */
157
    public function login($username, $password)
158
    {
159
        if ($this->request->isLoggedIn()) {
160
            return true;
161
        }
162
163
        $post = PinnerHelper::createLoginRequest($username, $password);
164
        $postString = UrlHelper::buildRequestString($post);
165
        $this->request->clearToken();
166
        $result = $this->response->checkErrorInResponse($this->request->exec(UrlHelper::RESOURCE_LOGIN, $postString));
167
        if ($result) {
168
            $this->request->setLoggedIn();
169
        }
170
171
        return $result;
172
    }
173
174
    protected function getScope()
175
    {
176
        return 'people';
177
    }
178
}
179