Pinners::block()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
use seregazhuk\PinterestBot\Helpers\Pagination;
7
use seregazhuk\PinterestBot\Api\Traits\Searchable;
8
use seregazhuk\PinterestBot\Exceptions\WrongFollowingType;
9
use seregazhuk\PinterestBot\Api\Traits\ResolvesCurrentUser;
10
use seregazhuk\PinterestBot\Api\Providers\Core\FollowableProvider;
11
12
class Pinners extends FollowableProvider
13
{
14
    use Searchable, ResolvesCurrentUser;
15
16
    /**
17
     * @var array
18
     */
19
    protected $loginRequiredFor = [
20
        'block',
21
        'search',
22
        'blockById',
23
        'followers',
24
        'following',
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(UrlBuilder::RESOURCE_USER_INFO, ['username' => $username]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get(serega...sername' => $username)) also could return the type boolean which is incompatible with the documented return type array.
Loading history...
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 ($followingUrl === null) {
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
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)) {
144
            return false;
145
        }
146
147
        return $this->blockById($profile['id']);
148
    }
149
150
    /**
151
     * @param int $userId
152
     * @return bool
153
     */
154
    public function blockById($userId)
155
    {
156
        return $this->post(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->post(sereg...d_user_id' => $userId)) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
157
            UrlBuilder::RESOURCE_BLOCK_USER, ['blocked_user_id' => $userId]
158
        );
159
    }
160
161
    /**
162
     * @param string $username
163
     * @param int $limit
164
     * @return Pagination
165
     */
166
    public function tried($username, $limit = Pagination::DEFAULT_LIMIT)
167
    {
168
        return $this->paginate(UrlBuilder::RESOURCE_USER_TRIED, ['username' => $username], $limit);
169
    }
170
171
    /**
172
     * @param string $username
173
     * @param string $url
174
     * @param int $limit
175
     *
176
     * @return Pagination
177
     */
178
    protected function paginateByUsername($username, $url, $limit = Pagination::DEFAULT_LIMIT)
179
    {
180
        return $this->paginate($url, ['username' => $username], $limit);
181
    }
182
183
    /**
184
     * @param mixed $entityId
185
     * @return int|null
186
     */
187
    protected function resolveEntityId($entityId)
188
    {
189
        // If user's id was passed we simply return it.
190
        if (is_numeric($entityId)) {
191
            return $entityId;
192
        }
193
194
        // Then we try to get user's info by username
195
        $userInfo = $this->info($entityId);
196
197
        // On success return users'id from his profile.
198
        return $userInfo['id'] ?? null;
199
    }
200
201
    /**
202
     * Returns current user's followers when used without arguments.
203
     * @param string $username
204
     * @param int $limit
205
     * @return array|Pagination
206
     */
207
    public function followers($username = '', $limit = Pagination::DEFAULT_LIMIT)
208
    {
209
        $username = empty($username) ?
210
            $this->resolveCurrentUsername() :
211
            $username;
212
213
        if (!$username) {
214
            return new Pagination();
215
        }
216
217
        return parent::followers($username, $limit);
218
    }
219
220
    /**
221
     * @param string $username
222
     * @return bool|null
223
     */
224
    public function isFollowedByMe($username)
225
    {
226
        return $this->info($username)['explicitly_followed_by_me'] ?? false;
227
    }
228
}
229