Completed
Pull Request — master (#317)
by Jan-Christoph
09:12
created

ViewController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 9.4285

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 * @author Raghu Nayyar
8
 * @copyright 2016 Raghu Nayyar <[email protected]>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public
21
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\Calendar\Controller;
25
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http;
28
use OCP\AppFramework\Http\ContentSecurityPolicy;
29
use OCP\AppFramework\Http\DataDisplayResponse;
30
use OCP\AppFramework\Http\JSONResponse;
31
use OCP\AppFramework\Http\NotFoundResponse;
32
use OCP\AppFramework\Http\TemplateResponse;
33
use OCP\Defaults;
34
use OCP\IConfig;
35
use OCP\IL10N;
36
use OCP\IRequest;
37
use OCP\IUserSession;
38
use OCP\Mail\IMailer;
39
use OCP\IURLGenerator;
40
41
class ViewController extends Controller {
42
43
	/**
44
	 * @var IURLGenerator
45
	 */
46
	private $urlGenerator;
47
48
	/**
49
	 * @var IConfig
50
	 */
51
	private $config;
52
53
	/**
54
	 * @var IUserSession
55
	 */
56
	private $userSession;
57
58
	/**
59
	 * @var IMailer
60
	 */
61
	private $mailer;
62
63
	/**
64
	 * @var IL10N
65
	 */
66
	private $l10n;
67
68
	/**
69
	 * @var Defaults
70
	 */
71
	private $defaults;
72
73
	/**
74
	 * @param string $appName
75
	 * @param IRequest $request an instance of the request
76
	 * @param IUserSession $userSession
77
	 * @param IConfig $config
78
	 * @param IMailer $mailer
79
	 * @param IL10N $l10N
80
	 * @param Defaults $defaults
81
	 * @param IURLGenerator $urlGenerator
82
	 */
83 19
	public function __construct($appName, IRequest $request, IUserSession $userSession,
84
								IConfig $config, IMailer $mailer, IL10N $l10N,
85
								Defaults $defaults, IURLGenerator $urlGenerator) {
86 19
		parent::__construct($appName, $request);
87 19
		$this->config = $config;
88 19
		$this->userSession = $userSession;
89 19
		$this->mailer = $mailer;
90 19
		$this->l10n = $l10N;
91 19
		$this->defaults = $defaults;
92 19
		$this->urlGenerator = $urlGenerator;
93 19
	}
94
95
	/**
96
	 * @NoAdminRequired
97
	 * @NoCSRFRequired
98
	 *
99
	 * @return TemplateResponse
100
	 */
101 9
	public function index() {
102 9
		$runningOn = $this->config->getSystemValue('version');
103 9
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
104 9
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
105
106 9
		$supportsClass = $runningOnNextcloud10OrLater;
107 9
		$assetPipelineBroken = !$runningOnNextcloud10OrLater;
108 9
		$needsAutosize = !$runningOnNextcloud11OrLater;
109
110 9
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
111 9
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
112 2
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
113
		}
114
115 7
		$user = $this->userSession->getUser();
116 7
		$userId = $user->getUID();
117 7
		$emailAddress = $user->getEMailAddress();
118
119 7
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
120 7
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
121 7
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
122 7
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
123 7
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
124 7
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
125
126
		// the default view will be saved as soon as a user
127
		// opens the calendar app, therefore this is a good
128
		// indication if the calendar was used before
129 7
		if ($firstRun === null) {
130 2
			if ($initialView === null) {
131 1
				$firstRun = 'yes';
132
			} else {
133 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
134 1
				$firstRun = 'no';
135
			}
136
		}
137
138 7
		if ($initialView === null) {
139 2
			$initialView = 'month';
140
		}
141
		
142 7
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
143
144 7
		return new TemplateResponse('calendar', 'main', [
145 7
			'appVersion' => $appVersion,
146 7
			'initialView' => $initialView,
147 7
			'emailAddress' => $emailAddress,
148 7
			'skipPopover' => $skipPopover,
149 7
			'weekNumbers' => $weekNumbers,
150 7
			'firstRun' => $firstRun,
151 7
			'supportsClass' => $supportsClass,
152 7
			'defaultColor' => $defaultColor,
153 7
			'webCalWorkaround' => $webCalWorkaround,
154
			'isPublic' => false,
155 7
			'needsAutosize' => $needsAutosize,
156
		]);
157
	}
158
159
	/**
160
	 * @PublicPage
161
	 * @NoCSRFRequired
162
	 *
163
	 * @return TemplateResponse
164
	 */
165 4
	public function publicIndex() {
166 4
		$runningOn = $this->config->getSystemValue('version');
167 4
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
168 4
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
169
170 4
		$supportsClass = $runningOnServer91OrLater;
171 4
		$assetPipelineBroken = !$runningOnServer91OrLater;
172 4
		$needsAutosize = !$runningOnNextcloud11OrLater;
173
174 4
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
175 4
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
176
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
177
		}
178
179 4
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
180
181 4
		$response = new TemplateResponse('calendar', 'main', [
182 4
			'appVersion' => $appVersion,
183 4
			'initialView' => 'month',
184 4
			'emailAddress' => '',
185 4
			'supportsClass' => $supportsClass,
186 4
			'firstRun' => 'no',
187
			'isPublic' => true,
188 4
			'shareURL' => $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . $this->request->getRequestUri(),
189 4
			'previewImage' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png')),
190 4
			'needsAutosize' => $needsAutosize,
191 4
		], 'public');
192 4
		$response->addHeader('X-Frame-Options', 'ALLOW');
193 4
		$csp = new ContentSecurityPolicy();
194 4
		$csp->addAllowedScriptDomain('*');
195 4
		$response->setContentSecurityPolicy($csp);
196
197 4
		return $response;
198
	}
199
200
	/**
201
	 * @NoAdminRequired
202
	 *
203
	 * @param string $id
204
	 * @return NotFoundResponse|DataDisplayResponse
205
	 */
206 4
	public function getTimezone($id) {
207 4
		if (!in_array($id, $this->getTimezoneList())) {
208 2
			return new NotFoundResponse();
209
		}
210
211 2
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
212
213 2
		return new DataDisplayResponse($tzData, Http::STATUS_OK, [
214 2
			'content-type' => 'text/calendar',
215
		]);
216
	}
217
218
219
	/**
220
	 * @NoAdminRequired
221
	 * @NoCSRFRequired
222
	 * @PublicPage
223
	 *
224
	 * @param $region
225
	 * @param $city
226
	 * @return DataDisplayResponse
227
	 */
228 2
	public function getTimezoneWithRegion($region, $city) {
229 2
		return $this->getTimezone($region . '-' . $city);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTimezone($region . '-' . $city); of type OCP\AppFramework\Http\No...ttp\DataDisplayResponse adds the type OCP\AppFramework\Http\NotFoundResponse to the return on line 229 which is incompatible with the return type documented by OCA\Calendar\Controller\...::getTimezoneWithRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
230
	}
231
232
233
	/**
234
	 * @NoAdminRequired
235
	 * @PublicPage
236
	 * @NoCSRFRequired
237
	 *
238
	 * @param $region
239
	 * @param $subregion
240
	 * @param $city
241
	 * @return DataDisplayResponse
242
	 */
243
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
244
		return $this->getTimezone($region . '-' . $subregion . '-' . $city);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTimezone($regi...bregion . '-' . $city); of type OCP\AppFramework\Http\No...ttp\DataDisplayResponse adds the type OCP\AppFramework\Http\NotFoundResponse to the return on line 244 which is incompatible with the return type documented by OCA\Calendar\Controller\...etTimezoneWithSubRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
245
	}
246
247
248
	/**
249
	 * get a list of default timezones
250
	 *
251
	 * @return array
252
	 */
253 4
	private function getTimezoneList() {
254 4
		$allFiles = scandir(__DIR__ . '/../timezones/');
255
256 4
		return array_values(array_filter($allFiles, function($file) {
257 4
			return (substr($file, -4) === '.ics');
258 4
		}));
259
	}
260
261
	/**
262
	 * @param string $to
263
	 * @param string $url
264
	 * @param string $name
265
	 * @return JSONResponse
266
	 * @NoAdminRequired
267
	 */
268 2
	public function sendEmailPublicLink($to, $url, $name) {
269
270 2
		$user = $this->userSession->getUser();
271 2
		$username = $user->getDisplayName();
272
273 2
		$subject = $this->l10n->t('%s has published the calendar "%s"', [$username, $name]);
274
275 2
		$emailTemplateHTML = new TemplateResponse('calendar', 'mail.publication.html', ['subject' => $subject, 'username' => $username, 'calendarname' => $name, 'calendarurl' => $url, 'defaults' => $this->defaults], 'public');
276 2
		$bodyHTML = $emailTemplateHTML->render();
277 2
		$emailTemplateText = new TemplateResponse('calendar', 'mail.publication.text', ['subject' => $subject, 'username' => $username, 'calendarname' => $name, 'calendarurl' => $url], 'blank');
278 2
		$textBody = $emailTemplateText->render();
279
280 2
		$status = $this->sendEmail($to, $subject, $bodyHTML, $textBody);
281
282 2
		return new JSONResponse([], $status);
283
	}
284
285
	/**
286
	 * @param string $target
287
	 * @param string $subject
288
	 * @param string $body
289
	 * @param string $textBody
290
	 * @return int
291
	 */
292 2
	private function sendEmail($target, $subject, $body, $textBody) {
293 2
		if (!$this->mailer->validateMailAddress($target)) {
294 2
			return Http::STATUS_BAD_REQUEST;
295
		}
296
297
		$sendFromDomain = $this->config->getSystemValue('mail_domain', 'domain.org');
298
		$sendFromAddress = $this->config->getSystemValue('mail_from_address', 'nextcloud');
299
		$sendFrom = $sendFromAddress . '@' . $sendFromDomain;
300
301
		$message = $this->mailer->createMessage();
302
		$message->setSubject($subject);
303
		$message->setFrom([$sendFrom => $this->defaults->getName()]);
304
		$message->setTo([$target => 'Recipient']);
305
		$message->setPlainBody($textBody);
306
		$message->setHtmlBody($body);
307
		$this->mailer->send($message);
308
309
		return Http::STATUS_OK;
310
	}
311
}
312