Passed
Pull Request — master (#84)
by Georg
05:00
created

ViewController::index()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 59
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 42
nc 13
nop 0
dl 0
loc 59
ccs 45
cts 45
cp 1
crap 7
rs 7.5346
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 OC\AppFramework\Http\Request;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\ContentSecurityPolicy;
29
use OCP\AppFramework\Http\TemplateResponse;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\IUserSession;
33
use OCP\IURLGenerator;
34
35
class ViewController extends Controller {
36
37
	/**
38
	 * @var IConfig
39
	 */
40
	private $config;
41
42
	/**
43
	 * @var IURLGenerator
44
	 */
45
	private $urlGenerator;
46
47
	/**
48
	 * @var IUserSession
49
	 */
50
	private $userSession;
51
52
	/**
53
	 * @param string $appName
54
	 * @param IRequest $request an instance of the request
55
	 * @param IUserSession $userSession
56
	 * @param IConfig $config
57
	 * @param IURLGenerator $urlGenerator
58
	 */
59 15
	public function __construct($appName, IRequest $request, IUserSession $userSession,
60
								IConfig $config, IURLGenerator $urlGenerator) {
61 15
		parent::__construct($appName, $request);
62 15
		$this->config = $config;
63 15
		$this->userSession = $userSession;
64 15
		$this->urlGenerator = $urlGenerator;
65 15
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 * @NoCSRFRequired
70
	 *
71
	 * @return TemplateResponse
72
	 */
73 10
	public function index() {
74 10
		$runningOn = $this->config->getSystemValue('version');
75 10
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
76 10
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
77
78 10
		$supportsClass = $runningOnNextcloud10OrLater;
79 10
		$assetPipelineBroken = !$runningOnNextcloud10OrLater;
80 10
		$needsAutosize = !$runningOnNextcloud11OrLater;
81
82 10
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
83 10
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
84 2
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
85
		}
86
87 8
		$user = $this->userSession->getUser();
88 8
		$userId = $user->getUID();
89 8
		$emailAddress = $user->getEMailAddress();
90
91 8
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
92 8
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
93 8
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
94 8
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
95 8
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
96 8
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
97
98
		// the default view will be saved as soon as a user
99
		// opens the calendar app, therefore this is a good
100
		// indication if the calendar was used before
101 8
		if ($firstRun === null) {
102 2
			if ($initialView === null) {
103 1
				$firstRun = 'yes';
104 1
			} else {
105 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
106 1
				$firstRun = 'no';
107
			}
108 2
		}
109
110 8
		if ($initialView === null) {
111 2
			$initialView = 'month';
112 2
		}
113
		
114 8
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
115 8
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
116
117 8
		return new TemplateResponse('calendar', 'main', [
118 8
			'appVersion' => $appVersion,
119 8
			'initialView' => $initialView,
120 8
			'emailAddress' => $emailAddress,
121 8
			'skipPopover' => $skipPopover,
122 8
			'weekNumbers' => $weekNumbers,
123 8
			'firstRun' => $firstRun,
124 8
			'supportsClass' => $supportsClass,
125 8
			'defaultColor' => $defaultColor,
126 8
			'webCalWorkaround' => $webCalWorkaround,
127 8
			'isPublic' => false,
128 8
			'isIE' => $isIE,
129 8
			'needsAutosize' => $needsAutosize,
130 8
		]);
131
	}
132
133
	/**
134
	 * @PublicPage
135
	 * @NoCSRFRequired
136
	 *
137
	 * @return TemplateResponse
138
	 */
139 5
	public function publicIndex() {
140 5
		$runningOn = $this->config->getSystemValue('version');
141 5
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
142 5
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
143
144 5
		$supportsClass = $runningOnServer91OrLater;
145 5
		$assetPipelineBroken = !$runningOnServer91OrLater;
146 5
		$needsAutosize = !$runningOnNextcloud11OrLater;
147
148 5
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
149 5
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
150
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
151
		}
152
153 5
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
154 5
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
155
156 5
		$response = new TemplateResponse('calendar', 'main', [
157 5
			'appVersion' => $appVersion,
158 5
			'initialView' => 'month',
159 5
			'emailAddress' => '',
160 5
			'skipPopover' => 'no',
161 5
			'weekNumbers' => 'no',
162 5
			'supportsClass' => $supportsClass,
163 5
			'firstRun' => 'no',
164 5
			'isIE' => $isIE,
165 5
			'webCalWorkaround' => 'no',
166 5
			'isPublic' => true,
167 5
			'shareURL' => $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . $this->request->getRequestUri(),
168 5
			'previewImage' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png')),
169 5
			'needsAutosize' => $needsAutosize,
170 5
		], 'public');
171 5
		$response->addHeader('X-Frame-Options', 'ALLOW');
172 5
		$csp = new ContentSecurityPolicy();
173 5
		$csp->addAllowedScriptDomain('*');
174 5
		$response->setContentSecurityPolicy($csp);
175
176 5
		return $response;
177
	}
178
}
179