SettingsController::setSettings()   F
last analyzed

Complexity

Conditions 16
Paths 3712

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 0
cts 65
cp 0
rs 1.5781
c 0
b 0
f 0
cc 16
nc 3712
nop 7
crap 272

How to fix   Long Method    Complexity   

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
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2014 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments\Controller;
13
14
use OCA\Richdocuments\Service\CapabilitiesService;
15
use OCA\Richdocuments\Service\DemoService;
16
use OCA\Richdocuments\WOPI\DiscoveryManager;
17
use OCA\Richdocuments\WOPI\Parser;
18
use \OCP\AppFramework\Controller;
19
use OCP\AppFramework\Http;
20
use OCP\AppFramework\Http\DataResponse;
21
use OCP\AppFramework\Http\JSONResponse;
22
use OCP\AppFramework\Http\NotFoundResponse;
23
use \OCP\IRequest;
24
use \OCP\IL10N;
25
use OCA\Richdocuments\AppConfig;
26
use OCP\IConfig;
27
use OCP\PreConditionNotMetException;
28
29
class SettingsController extends Controller{
30
	/** @var IL10N */
31
	private $l10n;
32
	/** @var AppConfig */
33
	private $appConfig;
34
	/** @var IConfig */
35
	private $config;
36
	/** @var DiscoveryManager  */
37
	private $discoveryManager;
38
	/** @var Parser */
39
	private $wopiParser;
40
	/** @var string */
41
	private $userId;
42
	/** @var CapabilitiesService */
43
	private $capabilitiesService;
44
	/** @var DemoService */
45
	private $demoService;
46
47
	/**
48
	 * @param string $appName
49
	 * @param IRequest $request
50
	 * @param IL10N $l10n
51
	 * @param AppConfig $appConfig
52
	 * @param IConfig $config
53
	 * @param DiscoveryManager $discoveryManager
54
	 * @param Parser $wopiParser
55
	 * @param string $userId
56
	 * @param CapabilitiesService $capabilitiesService
57
	 */
58
	public function __construct($appName,
59
		IRequest $request,
60
		IL10N $l10n,
61
		AppConfig $appConfig,
62
		IConfig $config,
63
		DiscoveryManager $discoveryManager,
64
		Parser $wopiParser,
65
		$userId,
66
		CapabilitiesService $capabilitiesService,
67
		DemoService $demoService
68
	) {
69
		parent::__construct($appName, $request);
70
		$this->l10n = $l10n;
71
		$this->appConfig = $appConfig;
72
		$this->config = $config;
73
		$this->discoveryManager = $discoveryManager;
74
		$this->wopiParser = $wopiParser;
75
		$this->userId = $userId;
76
		$this->capabilitiesService = $capabilitiesService;
77
		$this->demoService = $demoService;
78
	}
79
80
	/**
81
	 * @PublicPage
82
	 * @NoCSRFRequired
83
	 * @throws \Exception
84
	 */
85
	public function checkSettings() {
86
		try {
87
			$response = $this->discoveryManager->fetchFromRemote();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
		} catch (\Exception $e) {
89
			return new DataResponse([
90
				'status' => $e->getCode(),
91
				'message' => $e->getMessage()
92
			], Http::STATUS_INTERNAL_SERVER_ERROR);
93
		}
94
95
		return new DataResponse();
96
	}
97
98
	public function demoServers() {
99
		$demoServers = $this->demoService->fetchDemoServers(true);
100
		if (count($demoServers) > 0) {
101
			return new DataResponse($demoServers);
102
		}
103
		return new NotFoundResponse([]);
0 ignored issues
show
Unused Code introduced by
The call to NotFoundResponse::__construct() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
104
	}
105
106
	/**
107
	 * @NoAdminRequired
108
	 *
109
	 * @return JSONResponse
110
	 */
111
	public function getSettings() {
112
		return new JSONResponse([
113
			'wopi_url' => $this->appConfig->getAppValue('wopi_url'),
114
			'public_wopi_url' => $this->appConfig->getAppValue('public_wopi_url'),
115
			'disable_certificate_verification' => $this->appConfig->getAppValue('disable_certificate_verification') === 'yes',
116
			'edit_groups' => $this->appConfig->getAppValue('edit_groups'),
117
			'use_groups' => $this->appConfig->getAppValue('use_groups'),
118
			'doc_format' => $this->appConfig->getAppValue('doc_format'),
119
		]);
120
	}
121
122
	/**
123
	 * @param string $wopi_url
124
	 * @param string $disable_certificate_verification
125
	 * @param string $edit_groups
126
	 * @param string $use_groups
127
	 * @param string $doc_format
128
	 * @param string $external_apps
129
	 * @param string $canonical_webroot
130
	 * @return JSONResponse
131
	 */
132
	public function setSettings($wopi_url,
133
	                            $disable_certificate_verification,
134
	                            $edit_groups,
135
	                            $use_groups,
136
	                            $doc_format,
137
	                            $external_apps,
138
	                            $canonical_webroot) {
139
		$message = $this->l10n->t('Saved');
140
141
		if ($wopi_url !== null){
142
			$this->appConfig->setAppValue('wopi_url', $wopi_url);
143
		}
144
145
		if ($disable_certificate_verification !== null) {
146
			$this->appConfig->setAppValue(
147
				'disable_certificate_verification',
148
				$disable_certificate_verification === true ? 'yes' : ''
149
			);
150
		}
151
152
		if ($edit_groups !== null){
153
			$this->appConfig->setAppValue('edit_groups', $edit_groups);
154
		}
155
156
		if ($use_groups !== null){
157
			$this->appConfig->setAppValue('use_groups', $use_groups);
158
		}
159
160
		if ($doc_format !== null) {
161
			$this->appConfig->setAppValue('doc_format', $doc_format);
162
		}
163
164
		if ($external_apps !== null) {
165
			$this->appConfig->setAppValue('external_apps', $external_apps);
166
		}
167
168
		if ($canonical_webroot !== null) {
169
			$this->appConfig->setAppValue('canonical_webroot', $canonical_webroot);
170
		}
171
172
		$this->discoveryManager->refetch();
173
		$this->capabilitiesService->clear();
174
		try {
175
			$capaUrlSrc = $this->wopiParser->getUrlSrc('Capabilities');
176
			if (is_array($capaUrlSrc) && $capaUrlSrc['action'] === 'getinfo') {
177
				$public_wopi_url = str_replace('/hosting/capabilities', '', $capaUrlSrc['urlsrc']);
178
				if ($public_wopi_url !== null) {
179
					$this->appConfig->setAppValue('public_wopi_url', $public_wopi_url);
180
					$colon = strpos($public_wopi_url, ':', 0);
181
					if ($this->request->getServerProtocol() !== substr($public_wopi_url, 0, $colon)){
182
						$message = $this->l10n->t('Saved with error: Collabora Online should use the same protocol as the server installation.');
183
					}
184
				}
185
			}
186
		} catch (\Exception $e){
187
			if ($wopi_url !== null) {
188
				return new JSONResponse([
189
					'status' => 'error',
190
					'data' => ['message' => 'Failed to connect to the remote server']
191
				], 500);
192
			}
193
		}
194
195
		$this->capabilitiesService->clear();
196
		$this->capabilitiesService->refetch();
197
		if ($this->capabilitiesService->getCapabilities() === []) {
198
			return new JSONResponse([
199
				'status' => 'error',
200
				'data' => ['message' => 'Failed to connect to the remote server', 'hint' => 'missing_capabilities']
201
			], 500);
202
		}
203
204
		$response = [
205
			'status' => 'success',
206
			'data' => ['message' => $message]
207
		];
208
209
		return new JSONResponse($response);
210
	}
211
212
	public function updateWatermarkSettings($settings = []) {
213
		$supportedOptions = [
214
			'watermark_text',
215
			'watermark_enabled',
216
			'watermark_shareAll',
217
			'watermark_shareRead',
218
			'watermark_linkSecure',
219
			'watermark_linkRead',
220
			'watermark_linkAll',
221
			'watermark_linkTags',
222
			'watermark_linkTagsList',
223
			'watermark_allGroups',
224
			'watermark_allGroupsList',
225
			'watermark_allTags',
226
			'watermark_allTagsList',
227
		];
228
		$message = $this->l10n->t('Saved');
229
230
		$watermarkSettings = $settings['watermark'];
231
		foreach ($watermarkSettings as $key => $value) {
232
			$fullKey = 'watermark_' . $key;
233
			if (in_array($fullKey, $supportedOptions) !== true) {
234
				return new JSONResponse([
235
					'status' => 'error',
236
					'data' => ['message' => $this->l10n->t('Invalid config key') . ' ' . $fullKey]
237
				], Http::STATUS_BAD_REQUEST);
238
			}
239
			$parsedValue = $value;
240
			if (is_bool($value)) {
241
				$parsedValue = $value ? 'yes' : 'no';
242
			}
243
			$appSettingsType = array_key_exists($fullKey, AppConfig::APP_SETTING_TYPES) ? AppConfig::APP_SETTING_TYPES[$fullKey] : 'string';
244
			if ($appSettingsType === 'array') {
245
				$parsedValue = implode(',', $value);
246
			}
247
			$this->appConfig->setAppValue($fullKey, $parsedValue);
248
		}
249
250
		$response = [
251
			'status' => 'success',
252
			'data' => ['message' => $message]
253
		];
254
255
		return new JSONResponse($response);
256
	}
257
258
	/**
259
	 * @NoAdminRequired
260
	 *
261
	 * @param $key
262
	 * @param $value
263
	 * @return JSONResponse
264
	 */
265
	public function setPersonalSettings($templateFolder) {
266
		$message = $this->l10n->t('Saved');
267
		$status = 'success';
268
269
		if ($templateFolder !== null){
270
			try {
271
				$this->config->setUserValue($this->userId, 'richdocuments', 'templateFolder', $templateFolder);
272
			} catch (PreConditionNotMetException $e) {
273
				$message = $this->l10n->t('Error when saving');
274
				$status = 'error';
275
			}
276
		}
277
278
		$response = [
279
			'status' => $status,
280
			'data' => ['message' => $message]
281
		];
282
283
		return new JSONResponse($response);
284
285
	}
286
}
287