1
|
|
|
<?php |
2
|
|
|
namespace FlexyProject\GitHub\Receiver\Activity; |
3
|
|
|
|
4
|
|
|
use FlexyProject\GitHub\AbstractApi; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The Starring API class provide a feature that lets users bookmark repositories. |
9
|
|
|
* |
10
|
|
|
* @link https://developer.github.com/v3/activity/starring/ |
11
|
|
|
* @package GitHub\Receiver\Activity |
12
|
|
|
*/ |
13
|
|
|
class Starring extends AbstractActivity |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* List Stargazers |
18
|
|
|
* |
19
|
|
|
* @link https://developer.github.com/v3/activity/starring/#list-stargazers |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public function listStargazers(): array |
23
|
|
|
{ |
24
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/stargazers', |
25
|
|
|
$this->getActivity()->getOwner(), $this->getActivity()->getRepo())); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* List repositories being starred |
30
|
|
|
* |
31
|
|
|
* @link https://developer.github.com/v3/activity/starring/#list-repositories-being-starred |
32
|
|
|
* |
33
|
|
|
* @param string $sort |
34
|
|
|
* @param string $direction |
35
|
|
|
* @param string $username |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function listRepositories(string $sort = AbstractApi::SORT_CREATED, |
40
|
|
|
string $direction = AbstractApi::DIRECTION_DESC, string $username = null): array |
41
|
|
|
{ |
42
|
|
|
if (null !== $username) { |
43
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/users/:username/starred?:args', $username, |
44
|
|
|
http_build_query(['sort' => $sort, 'direction' => $direction]))); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/user/starred?:args', |
48
|
|
|
http_build_query(['sort' => $sort, 'direction' => $direction]))); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check if you are starring a repository |
53
|
|
|
* |
54
|
|
|
* @link https://developer.github.com/v3/activity/starring/#check-if-you-are-starring-a-repository |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function checkYouAreStarringRepository(): bool |
58
|
|
|
{ |
59
|
|
|
$this->getApi()->request($this->getApi() |
60
|
|
|
->sprintf('/user/starred/:owner/:repo', $this->getActivity()->getOwner(), |
61
|
|
|
$this->getActivity()->getRepo())); |
62
|
|
|
|
63
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Star a repository |
72
|
|
|
* |
73
|
|
|
* @link https://developer.github.com/v3/activity/starring/#star-a-repository |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function starRepository(): bool |
77
|
|
|
{ |
78
|
|
|
$this->getApi()->request($this->getApi() |
79
|
|
|
->sprintf('/user/starred/:owner/:repo', $this->getActivity()->getOwner(), |
80
|
|
|
$this->getActivity()->getRepo()), Request::METHOD_PUT); |
81
|
|
|
|
82
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Unstar a repository |
91
|
|
|
* |
92
|
|
|
* @link https://developer.github.com/v3/activity/starring/#unstar-a-repository |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function unStarRepository(): bool |
96
|
|
|
{ |
97
|
|
|
$this->getApi()->request($this->getApi() |
98
|
|
|
->sprintf('/user/starred/:owner/:repo', $this->getActivity()->getOwner(), |
99
|
|
|
$this->getActivity()->getRepo()), Request::METHOD_DELETE); |
100
|
|
|
|
101
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
} |