Completed
Branch master (23259a)
by Sergey
04:17 queued 48s
created

Pinners::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
    ];
24
25
    protected $searchScope  = 'people';
26
    protected $entityIdName = 'user_id';
27
    protected $followersFor = 'username';
28
29
    protected $followUrl    = UrlBuilder::RESOURCE_FOLLOW_USER;
30
    protected $unFollowUrl  = UrlBuilder::RESOURCE_UNFOLLOW_USER;
31
    protected $followersUrl = UrlBuilder::RESOURCE_USER_FOLLOWERS;
32
33
    /**
34
     * Get user info.
35
     * If username param is not specified, will
36
     * return info for logged user.
37
     *
38
     * @param string $username
39
     * @return array
40
     */
41
    public function info($username)
42
    {
43
        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...
44
    }
45
46
    /**
47
     * Get following info for pinner.
48
     *
49
     * @param string $username
50
     * @param string $type
51
     * @param int $limit
52
     * @return Pagination
53
     * @throws WrongFollowingType
54
     */
55
    public function following($username, $type = UrlBuilder::FOLLOWING_PEOPLE, $limit = Pagination::DEFAULT_LIMIT)
56
    {
57
        $followingUrl = UrlBuilder::getFollowingUrlByType($type);
58
59
        if ($followingUrl === null) {
60
            throw new WrongFollowingType("No following results for $type");
61
        }
62
63
        return $this->paginateByUsername($username, $followingUrl, $limit);
64
    }
65
66
    /**
67
     * Get following people for pinner.
68
     *
69
     * @param string $username
70
     * @param int $limit
71
     * @return Pagination
72
     */
73
    public function followingPeople($username, $limit = Pagination::DEFAULT_LIMIT)
74
    {
75
        return $this->following($username, UrlBuilder::FOLLOWING_PEOPLE, $limit);
76
    }
77
78
    /**
79
     * Get following boards for pinner.
80
     *
81
     * @param string $username
82
     * @param int $limit
83
     * @return Pagination
84
     */
85
    public function followingBoards($username, $limit = Pagination::DEFAULT_LIMIT)
86
    {
87
        return $this->following($username, UrlBuilder::FOLLOWING_BOARDS, $limit);
88
    }
89
90
    /**
91
     * Get following interests for pinner.
92
     *
93
     * @param string $username
94
     * @param int $limit
95
     * @return Pagination
96
     * @throws WrongFollowingType
97
     */
98
    public function followingInterests($username, $limit = Pagination::DEFAULT_LIMIT)
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 Pagination
110
     */
111
    public function pins($username, $limit = Pagination::DEFAULT_LIMIT)
112
    {
113
        return $this->paginateByUsername(
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 Pagination
124
     */
125
    public function likes($username, $limit = Pagination::DEFAULT_LIMIT)
126
    {
127
        return $this->paginateByUsername(
128
            $username, UrlBuilder::RESOURCE_USER_LIKES, $limit
129
        );
130
    }
131
132
    /**
133
     * @param string $username
134
     * @return bool
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)) {
142
            return false;
143
        }
144
145
        return $this->blockById($profile['id']);
146
    }
147
148
    /**
149
     * @param int $userId
150
     * @return bool
151
     */
152
    public function blockById($userId)
153
    {
154
        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...
155
            UrlBuilder::RESOURCE_BLOCK_USER, ['blocked_user_id' => $userId]
156
        );
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(UrlBuilder::RESOURCE_USER_TRIED, ['username' => $username], $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($url, ['username' => $username], $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)) {
189
            return $entityId;
190
        }
191
192
        // Then we try to get user's info by username
193
        $userInfo = $this->info($entityId);
194
195
        // On success return users'id from his profile.
196
        return isset($userInfo['id']) ?
197
            $userInfo['id'] :
198
            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