|
1
|
|
|
<?php |
|
2
|
|
|
namespace FlexyProject\GitHub\Receiver\Users; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* The Followers API class provide access to manage followers. |
|
8
|
|
|
* |
|
9
|
|
|
* @link https://developer.github.com/v3/users/followers/ |
|
10
|
|
|
* @package FlexyProject\GitHub\Receiver\Users |
|
11
|
|
|
*/ |
|
12
|
|
|
class Followers extends AbstractUsers |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* List followers of a user |
|
17
|
|
|
* |
|
18
|
|
|
* @link https://developer.github.com/v3/users/followers/#list-followers-of-a-user |
|
19
|
|
|
* |
|
20
|
|
|
* @param null|string $username |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
* @throws \Exception |
|
24
|
|
|
*/ |
|
25
|
|
View Code Duplication |
public function listFollowers(string $username = null): array |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$url = '/user/followers'; |
|
28
|
|
|
if (null !== $username) { |
|
29
|
|
|
$url = $this->getApi()->sprintf('/users/:username/followers', $username); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $this->getApi()->request($url); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* List users followed by another user |
|
37
|
|
|
* |
|
38
|
|
|
* @link https://developer.github.com/v3/users/followers/#list-users-followed-by-another-user |
|
39
|
|
|
* |
|
40
|
|
|
* @param null|string $username |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @throws \Exception |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
public function listUsersFollowedBy(string $username = null): array |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$url = '/user/following'; |
|
48
|
|
|
if (null !== $username) { |
|
49
|
|
|
$url = $this->getApi()->sprintf('/users/:username/following', $username); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->getApi()->request($url); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Check if you are following a user |
|
57
|
|
|
* |
|
58
|
|
|
* @link https://developer.github.com/v3/users/followers/#check-if-you-are-following-a-user |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $username |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
* @throws \Exception |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function checkFollowingUser(string $username): bool |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/user/following/:username', $username)); |
|
68
|
|
|
|
|
69
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Check if one user follows another |
|
78
|
|
|
* |
|
79
|
|
|
* @link https://developer.github.com/v3/users/followers/#check-if-one-user-follows-another |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $username |
|
82
|
|
|
* @param string $targetUser |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
* @throws \Exception |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
public function checkUserFollowsAnother(string $username, string $targetUser): bool |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$this->getApi()->request($this->getApi() |
|
90
|
|
|
->sprintf('/users/:username/following/:target_user', $username, $targetUser)); |
|
91
|
|
|
|
|
92
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Follow a user |
|
101
|
|
|
* |
|
102
|
|
|
* @link https://developer.github.com/v3/users/followers/#follow-a-user |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $username |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
* @throws \Exception |
|
108
|
|
|
*/ |
|
109
|
|
View Code Duplication |
public function followUser(string $username): bool |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/user/following/:username', $username), Request::METHOD_PUT); |
|
112
|
|
|
|
|
113
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Unfollow a user |
|
122
|
|
|
* |
|
123
|
|
|
* @link https://developer.github.com/v3/users/followers/#unfollow-a-user |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $username |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
* @throws \Exception |
|
129
|
|
|
*/ |
|
130
|
|
View Code Duplication |
public function unfollowUser(string $username): bool |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/user/following/:username', $username), |
|
133
|
|
|
Request::METHOD_DELETE); |
|
134
|
|
|
|
|
135
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
136
|
|
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.