Completed
Pull Request — master (#245)
by Sergey
04:32
created

Pinners::resolveEntityId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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