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

Pinners::blockById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use Iterator;
6
use seregazhuk\PinterestBot\Api\Response;
7
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
8
use seregazhuk\PinterestBot\Api\Traits\Followable;
9
use seregazhuk\PinterestBot\Api\Traits\Searchable;
10
use seregazhuk\PinterestBot\Exceptions\WrongFollowingType;
11
12
class Pinners extends Provider
13
{
14
    use Searchable, Followable;
15
16
    protected $loginRequiredFor = [
17
        'follow',
18
        'unFollow',
19
    ];
20
21
    protected $searchScope  = 'people';
22
    protected $entityIdName = 'user_id';
23
    protected $followersFor = 'username';
24
25
    protected $followUrl    = UrlBuilder::RESOURCE_FOLLOW_USER;
26
    protected $unFollowUrl  = UrlBuilder::RESOURCE_UNFOLLOW_USER;
27
    protected $followersUrl = UrlBuilder::RESOURCE_USER_FOLLOWERS;
28
    
29
    /**
30
     * Get user info.
31
     * If username param is not specified, will
32
     * return info for logged user.
33
     *
34
     * @param string $username
35
     *
36
     * @return array
37
     */
38
    public function info($username)
39
    {
40
        return $this->execGetRequest(['username' => $username], UrlBuilder::RESOURCE_USER_INFO);
41
    }
42
43
    /**
44
     * Get following info for pinner.
45
     *
46
     * @param string $username
47
     * @param string $type
48
     * @param int $limit
49
     * @return Iterator
50
     * @throws WrongFollowingType
51
     */
52
    public function following($username, $type = UrlBuilder::FOLLOWING_PEOPLE, $limit = 0)
53
    {
54
        $followingUrl = UrlBuilder::getFollowingUrlByType($type);
55
56
        if(empty($followingUrl)) {
57
            throw new WrongFollowingType("No following results for $type");
58
        }
59
60
        return $this->paginate($username, $followingUrl, $limit);
61
    }
62
63
    /**
64
     * @codeCoverageIgnore
65
     * Get following people for pinner.
66
     *
67
     * @param string $username
68
     * @param int $limit
69
     * @return Iterator
70
     */
71
    public function followingPeople($username, $limit = 0)
72
    {
73
        return $this->following($username, UrlBuilder::FOLLOWING_PEOPLE, $limit);
74
    }
75
76
    /**
77
     * @codeCoverageIgnore
78
     * Get following boards for pinner.
79
     *
80
     * @param string $username
81
     * @param int $limit
82
     * @return Iterator
83
     */
84
    public function followingBoards($username, $limit = 0)
85
    {
86
        return $this->following($username, UrlBuilder::FOLLOWING_BOARDS, $limit);
87
    }
88
89
    /**
90
     * @codeCoverageIgnore
91
     * Get following interests for pinner.
92
     *
93
     * @param string $username
94
     * @param int $limit
95
     * @return Iterator
96
     * @throws WrongFollowingType
97
     */
98
    public function followingInterests($username, $limit = 0)
99
    {
100
        return $this->following($username, UrlBuilder::FOLLOWING_INTERESTS, $limit);
101
    }
102
103
    /**
104
     * Get pinner pins.
105
     *
106
     * @param string $username
107
     * @param int $limit
108
     *
109
     * @return Iterator
110
     */
111
    public function pins($username, $limit = 0)
112
    {
113
        return $this->paginate(
114
            $username, UrlBuilder::RESOURCE_USER_PINS, $limit
115
        );
116
    }
117
118
    /**
119
     * Get pins that user likes.
120
     *
121
     * @param string $username
122
     * @param int $limit
123
     * @return Iterator
124
     */
125
    public function likes($username, $limit = 0)
126
    {
127
        return $this->paginate(
128
            $username, UrlBuilder::RESOURCE_USER_LIKES, $limit
129
        );
130
    }
131
132
    /**
133
     * @param string $username
134
     * @return bool|Response
135
     */
136
    public function block($username)
137
    {
138
        // Retrieve profile data to get user id
139
        $profile = $this->info($username);
140
141
        if(empty($profile)) return false;
142
143
        return $this->blockById($profile['id']);
144
    }
145
146
    /**
147
     * @param int $userId
148
     * @return bool|Response
149
     */
150
    public function blockById($userId)
151
    {
152
        $data = ['blocked_user_id' => $userId];
153
154
        return $this->execPostRequest($data, UrlBuilder::RESOURCE_BLOCK_USER);
155
    }
156
157
    /**
158
     * @param string $username
159
     * @param string $url
160
     * @param int $limit
161
     *
162
     * @return Iterator
163
     */
164
    protected function paginate($username, $url, $limit)
165
    {
166
        $params = [
167
            'data' => ['username' => $username],
168
            'url'  => $url,
169
        ];
170
171
        return $this->getPaginatedResponse($params, $limit);
172
    }
173
}
174