|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of the Joomla Framework Github Package |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
|
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Joomla\Github\Package\Activity; |
|
10
|
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* GitHub API Activity Watching Events class for the Joomla Framework. |
|
15
|
|
|
* |
|
16
|
|
|
* @documentation http://developer.github.com/v3/activity/watching/ |
|
17
|
|
|
* |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class Watching extends AbstractPackage |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* List watchers |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $owner Repository owner. |
|
26
|
|
|
* @param string $repo Repository name. |
|
27
|
|
|
* |
|
28
|
|
|
* @return mixed |
|
29
|
|
|
* |
|
30
|
|
|
* @since 1.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getList($owner, $repo) |
|
33
|
|
|
{ |
|
34
|
|
|
// Build the request path. |
|
35
|
|
|
$path = '/repos/' . $owner . '/' . $repo . '/subscribers'; |
|
36
|
|
|
|
|
37
|
|
|
return $this->processResponse( |
|
38
|
|
|
$this->client->get($this->fetchUrl($path)) |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* List repositories being watched. |
|
44
|
|
|
* |
|
45
|
|
|
* List repositories being watched by a user. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $user User name. |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
* |
|
51
|
|
|
* @since 1.0 |
|
52
|
|
|
*/ |
|
53
|
|
View Code Duplication |
public function getRepositories($user = '') |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
// Build the request path. |
|
56
|
|
|
$path = ($user) |
|
57
|
|
|
? '/users/' . $user . '/subscriptions' |
|
58
|
|
|
: '/user/subscriptions'; |
|
59
|
|
|
|
|
60
|
|
|
return $this->processResponse( |
|
61
|
|
|
$this->client->get($this->fetchUrl($path)) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get a Repository Subscription. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $owner Repository owner. |
|
69
|
|
|
* @param string $repo Repository name. |
|
70
|
|
|
* |
|
71
|
|
|
* @return object |
|
72
|
|
|
* |
|
73
|
|
|
* @since 1.0 |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getSubscription($owner, $repo) |
|
76
|
|
|
{ |
|
77
|
|
|
// Build the request path. |
|
78
|
|
|
$path = '/repos/' . $owner . '/' . $repo . '/subscription'; |
|
79
|
|
|
|
|
80
|
|
|
return $this->processResponse( |
|
81
|
|
|
$this->client->get($this->fetchUrl($path)) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set a Repository Subscription. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $owner Repository owner. |
|
89
|
|
|
* @param string $repo Repository name. |
|
90
|
|
|
* @param boolean $subscribed Determines if notifications should be received from this thread. |
|
91
|
|
|
* @param boolean $ignored Determines if all notifications should be blocked from this thread. |
|
92
|
|
|
* |
|
93
|
|
|
* @return object |
|
94
|
|
|
* |
|
95
|
|
|
* @since 1.0 |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setSubscription($owner, $repo, $subscribed, $ignored) |
|
98
|
|
|
{ |
|
99
|
|
|
// Build the request path. |
|
100
|
|
|
$path = '/repos/' . $owner . '/' . $repo . '/subscription'; |
|
101
|
|
|
|
|
102
|
|
|
$data = array( |
|
103
|
|
|
'subscribed' => $subscribed, |
|
104
|
|
|
'ignored' => $ignored |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
return $this->processResponse( |
|
108
|
|
|
$this->client->put($this->fetchUrl($path), json_encode($data)) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Delete a Repository Subscription. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $owner Repository owner. |
|
116
|
|
|
* @param string $repo Repository name. |
|
117
|
|
|
* |
|
118
|
|
|
* @return object |
|
119
|
|
|
* |
|
120
|
|
|
* @since 1.0 |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public function deleteSubscription($owner, $repo) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
// Build the request path. |
|
125
|
|
|
$path = '/repos/' . $owner . '/' . $repo . '/subscription'; |
|
126
|
|
|
|
|
127
|
|
|
return $this->processResponse( |
|
128
|
|
|
$this->client->delete($this->fetchUrl($path)), |
|
129
|
|
|
204 |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Check if you are watching a repository (LEGACY). |
|
135
|
|
|
* |
|
136
|
|
|
* Requires for the user to be authenticated. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $owner Repository owner. |
|
139
|
|
|
* @param string $repo Repository name. |
|
140
|
|
|
* |
|
141
|
|
|
* @return boolean |
|
142
|
|
|
* |
|
143
|
|
|
* @since 1.0 |
|
144
|
|
|
* @throws \UnexpectedValueException |
|
145
|
|
|
*/ |
|
146
|
|
|
public function check($owner, $repo) |
|
147
|
|
|
{ |
|
148
|
|
|
// Build the request path. |
|
149
|
|
|
$path = '/user/subscriptions/' . $owner . '/' . $repo; |
|
150
|
|
|
|
|
151
|
|
|
$response = $this->client->get($this->fetchUrl($path)); |
|
152
|
|
|
|
|
153
|
|
|
switch ($response->code) |
|
154
|
|
|
{ |
|
155
|
|
|
case '204' : |
|
156
|
|
|
// This repository is watched by you. |
|
157
|
|
|
return true; |
|
158
|
|
|
break; |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
case '404' : |
|
161
|
|
|
// This repository is not watched by you. |
|
162
|
|
|
return false; |
|
163
|
|
|
break; |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Watch a repository (LEGACY). |
|
171
|
|
|
* |
|
172
|
|
|
* Requires for the user to be authenticated. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $owner Repository owner. |
|
175
|
|
|
* @param string $repo Repository name. |
|
176
|
|
|
* |
|
177
|
|
|
* @return object |
|
178
|
|
|
* |
|
179
|
|
|
* @since 1.0 |
|
180
|
|
|
*/ |
|
181
|
|
|
public function watch($owner, $repo) |
|
182
|
|
|
{ |
|
183
|
|
|
// Build the request path. |
|
184
|
|
|
$path = '/user/subscriptions/' . $owner . '/' . $repo; |
|
185
|
|
|
|
|
186
|
|
|
return $this->processResponse( |
|
187
|
|
|
$this->client->put($this->fetchUrl($path), ''), |
|
188
|
|
|
204 |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Stop watching a repository (LEGACY). |
|
194
|
|
|
* |
|
195
|
|
|
* Requires for the user to be authenticated. |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $owner Repository owner. |
|
198
|
|
|
* @param string $repo Repository name. |
|
199
|
|
|
* |
|
200
|
|
|
* @return object |
|
201
|
|
|
* |
|
202
|
|
|
* @since 1.0 |
|
203
|
|
|
*/ |
|
204
|
|
|
public function unwatch($owner, $repo) |
|
205
|
|
|
{ |
|
206
|
|
|
// Build the request path. |
|
207
|
|
|
$path = '/user/subscriptions/' . $owner . '/' . $repo; |
|
208
|
|
|
|
|
209
|
|
|
return $this->processResponse( |
|
210
|
|
|
$this->client->delete($this->fetchUrl($path)), |
|
211
|
|
|
204 |
|
212
|
|
|
); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
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.