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

Pinners::unFollow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

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