Completed
Push — master ( 30ba5d...acee74 )
by Sergey
04:20 queued 01:26
created

Pinners   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 145
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A info() 0 4 1
A following() 0 10 2
A followingPeople() 0 4 1
A followingBoards() 0 4 1
A followingInterests() 0 4 1
A pins() 0 6 1
A likes() 0 6 1
A block() 0 6 1
A paginate() 0 9 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
     * Get following people for pinner.
65
     *
66
     * @param string $username
67
     * @param int $limit
68
     * @return Iterator
69
     */
70
    public function followingPeople($username, $limit = 0)
71
    {
72
        return $this->following($username, UrlBuilder::FOLLOWING_PEOPLE, $limit);
73
    }
74
75
    /**
76
     * Get following boards for pinner.
77
     *
78
     * @param string $username
79
     * @param int $limit
80
     * @return Iterator
81
     */
82
    public function followingBoards($username, $limit = 0)
83
    {
84
        return $this->following($username, UrlBuilder::FOLLOWING_BOARDS, $limit);
85
    }
86
87
    /**
88
     * Get following interests for pinner.
89
     *
90
     * @param string $username
91
     * @param int $limit
92
     * @return Iterator
93
     * @throws WrongFollowingType
94
     */
95
    public function followingInterests($username, $limit = 0)
96
    {
97
        return $this->following($username, UrlBuilder::FOLLOWING_INTERESTS, $limit);
98
    }
99
100
    /**
101
     * Get pinner pins.
102
     *
103
     * @param string $username
104
     * @param int $limit
105
     *
106
     * @return Iterator
107
     */
108
    public function pins($username, $limit = 0)
109
    {
110
        return $this->paginate(
111
            $username, UrlBuilder::RESOURCE_USER_PINS, $limit
112
        );
113
    }
114
115
    /**
116
     * Get pins that user likes.
117
     *
118
     * @param string $username
119
     * @param int $limit
120
     * @return Iterator
121
     */
122
    public function likes($username, $limit = 0)
123
    {
124
        return $this->paginate(
125
            $username, UrlBuilder::RESOURCE_USER_LIKES, $limit
126
        );
127
    }
128
129
    /**
130
     * @param int $userId
131
     * @return bool|Response
132
     */
133
    public function block($userId)
134
    {
135
        $data = ['blocked_user_id' => $userId];
136
137
        return $this->execPostRequest($data, UrlBuilder::RESOURCE_BLOCK_USER, true);
138
    }
139
140
    /**
141
     * @param string $username
142
     * @param string $url
143
     * @param int $limit
144
     *
145
     * @return Iterator
146
     */
147
    protected function paginate($username, $url, $limit)
148
    {
149
        $params = [
150
            'data' => ['username' => $username],
151
            'url'  => $url,
152
        ];
153
154
        return $this->getPaginatedResponse($params, $limit);
155
    }
156
}
157