1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - Calendar App |
4
|
|
|
* |
5
|
|
|
* @author Georg Ehrke |
6
|
|
|
* @copyright 2014 Georg Ehrke <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 3 of the License, or any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public |
19
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
namespace OCA\Calendar\Controller; |
23
|
|
|
|
24
|
|
|
use OCA\Calendar\Backend\Exception as BackendException; |
25
|
|
|
use OCA\Calendar\BusinessLayer\Subscription; |
26
|
|
|
use OCA\Calendar\Db\SubscriptionFactory; |
27
|
|
|
use OCA\Calendar\Http\JSON; |
28
|
|
|
use OCA\Calendar\Http\ReaderException; |
29
|
|
|
use OCA\Calendar\IBackend; |
30
|
|
|
use OCA\Calendar\IBackendCollection; |
31
|
|
|
use OCA\Calendar\ISubscription; |
32
|
|
|
|
33
|
|
|
use OCP\AppFramework\Http; |
34
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
35
|
|
|
use OCP\IRequest; |
36
|
|
|
use OCP\IUserSession; |
37
|
|
|
|
38
|
|
|
class SubscriptionController extends Controller { |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* BusinessLayer for managing backends |
42
|
|
|
* @var Subscription |
43
|
|
|
*/ |
44
|
|
|
protected $subscriptions; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var IBackendCollection |
49
|
|
|
*/ |
50
|
|
|
protected $backends; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $appName |
55
|
|
|
* @param IRequest $request an instance of the request |
56
|
|
|
* @param IUserSession $userSession |
57
|
|
|
* @param Subscription $subscriptions |
58
|
|
|
* @param SubscriptionFactory $subscriptionFactory |
59
|
|
|
* @param IBackendCollection $backends |
60
|
|
|
*/ |
61
|
|
|
public function __construct($appName, IRequest $request, IUserSession $userSession, |
62
|
|
|
Subscription $subscriptions, SubscriptionFactory $subscriptionFactory, |
63
|
|
|
IBackendCollection $backends) { |
64
|
|
|
parent::__construct($appName, $request, $userSession); |
65
|
|
|
$this->subscriptions = $subscriptions; |
66
|
|
|
$this->backends = $backends; |
67
|
|
|
|
68
|
|
|
$this->registerReader('json', function(IRequest $request) use ($subscriptionFactory) { |
69
|
|
|
$reader = new JSON\SubscriptionReader($request, $subscriptionFactory); |
70
|
|
|
return $reader; |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$this->registerResponder('json', function($value) { |
74
|
|
|
return new JSON\SubscriptionResponse($value, $this->getSuccessfulStatusCode()); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $limit |
81
|
|
|
* @param int $offset |
82
|
|
|
* @return \OCP\AppFramework\Http\Response |
83
|
|
|
* |
84
|
|
|
* @NoAdminRequired |
85
|
|
|
* @NoCSRFRequired |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function index($limit=null, $offset=null) { |
|
|
|
|
88
|
|
|
try { |
89
|
|
|
return $this->subscriptions->findAll( |
90
|
|
|
$this->user->getUID(), |
91
|
|
|
$limit, |
92
|
|
|
$offset |
93
|
|
|
); |
94
|
|
|
} catch (\Exception $ex) { |
95
|
|
|
return $this->handleException($ex); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param integer $id |
102
|
|
|
* @return \OCP\AppFramework\Http\Response |
103
|
|
|
* |
104
|
|
|
* @NoAdminRequired |
105
|
|
|
* @NoCSRFRequired |
106
|
|
|
*/ |
107
|
|
|
public function show($id) { |
108
|
|
|
try { |
109
|
|
|
return $this->subscriptions->find( |
110
|
|
|
$id, |
111
|
|
|
$this->user->getUID() |
112
|
|
|
); |
113
|
|
|
} catch (\Exception $ex) { |
114
|
|
|
return $this->handleException($ex); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @NoAdminRequired |
121
|
|
|
* @NoCSRFRequired |
122
|
|
|
*/ |
123
|
|
|
public function create() { |
124
|
|
|
try { |
125
|
|
|
$subscription = $this->readInput(); |
126
|
|
|
|
127
|
|
|
if (!($subscription instanceof ISubscription)) { |
128
|
|
|
throw new ReaderException( |
129
|
|
|
'Reader returned unrecognised format' |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$subscription->setUserId($this->user->getUID()); |
134
|
|
|
|
135
|
|
|
// todo - move scanner call to businesslayer |
136
|
|
|
$subscription = $this->subscriptions->create( |
137
|
|
|
$subscription |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$backend = $this->backends->bySubscriptionType( |
141
|
|
|
$subscription->getType() |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
if ($backend instanceof IBackend) { |
145
|
|
|
$backend->getBackendCollection()->getScanner()->scan($this->user->getUID()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $subscription; |
149
|
|
|
} catch (\Exception $ex) { |
150
|
|
|
return $this->handleException($ex); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param int $id |
157
|
|
|
* @return \OCP\AppFramework\Http\Response |
158
|
|
|
* |
159
|
|
|
* @NoAdminRequired |
160
|
|
|
* @NoCSRFRequired |
161
|
|
|
*/ |
162
|
|
|
public function update($id) { |
163
|
|
|
try { |
164
|
|
|
$subscription = $this->readInput(); |
165
|
|
|
|
166
|
|
|
if (!($subscription instanceof ISubscription)) { |
167
|
|
|
throw new ReaderException( |
168
|
|
|
'Reader returned unrecognised format' |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$subscription->setUserId($this->user->getUID()); |
173
|
|
|
$subscription->setId($id); |
174
|
|
|
|
175
|
|
|
return $this->subscriptions->update( |
176
|
|
|
$subscription |
177
|
|
|
); |
178
|
|
|
} catch (\Exception $ex) { |
179
|
|
|
return $this->handleException($ex); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param integer $id |
186
|
|
|
* @return \OCP\AppFramework\Http\Response |
187
|
|
|
* |
188
|
|
|
* @NoAdminRequired |
189
|
|
|
* @NoCSRFRequired |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
public function destroy($id) { |
|
|
|
|
192
|
|
|
try { |
193
|
|
|
$subscription = $this->subscriptions->find( |
194
|
|
|
$id, |
195
|
|
|
$this->user->getUID() |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$this->subscriptions->delete($subscription); |
199
|
|
|
|
200
|
|
|
return new JSONResponse([ |
201
|
|
|
'message' => 'Subscription was deleted successfully', |
202
|
|
|
]); |
203
|
|
|
} catch (\Exception $ex) { |
204
|
|
|
return $this->handleException($ex); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
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.