1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage GitHub
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * GitHub API Activity Watching Events class for the Joomla Platform.
14 *
15 * @documentation https://developer.github.com/v3/activity/watching/
16 *
17 * @since 3.3 (CMS)
18 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
19 */
20 class JGithubPackageActivityWatching extends JGithubPackage
21 {
22 /**
23 * List watchers
24 *
25 * @param string $owner Repository owner.
26 * @param string $repo Repository name.
27 *
28 * @since 3.3 (CMS)
29 *
30 * @return mixed
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 * @since 3.3 (CMS)
50 *
51 * @return mixed
52 */
53 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 * @since 3.3 (CMS)
72 *
73 * @return mixed
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 * @since 3.3 (CMS)
94 *
95 * @return object
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 * @since 3.3 (CMS)
119 *
120 * @return object
121 */
122 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 * @throws UnexpectedValueException
142 * @since 3.3 (CMS)
143 *
144 * @return object
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 * @since 3.3 (CMS)
178 *
179 * @return object
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 * @since 3.3 (CMS)
201 *
202 * @return object
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