Failed Conditions
Push — master ( 2a7938...ff349a )
by Sander
27:39 queued 19:45
created

SettingsController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 16
loc 80
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 1
A getForm() 0 3 1
A getSection() 0 3 1
A getPriority() 0 3 1
A getSettings() 0 4 1
A saveUserSetting() 0 4 1
A saveAdminSetting() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Sander Brand <[email protected]>
9
 * @copyright Sander Brand 2016
10
 */
11
12
namespace OCA\Passman\Controller;
13
14
use OCP\IL10N;
15
use OCP\Settings\ISettings;
16
use OCP\AppFramework\Http\TemplateResponse;
17
use OCP\AppFramework\Http\JSONResponse;
18
use OCP\AppFramework\ApiController;
19
use OCP\IRequest;
20
use OCA\Passman\Service\SettingsService;
21
22
class SettingsController extends ApiController {
23
	private $userId;
24
	private $settings;
25
26 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
27
		$AppName,
28
		IRequest $request,
29
		$userId,
30
		SettingsService $settings,
31
		IL10N $l) {
32
		parent::__construct(
33
			$AppName,
34
			$request,
35
			'GET, POST, DELETE, PUT, PATCH, OPTIONS',
36
			'Authorization, Content-Type, Accept',
37
			86400);
38
		$this->settings = $settings;
39
		$this->l = $l;
40
		$this->userId = $userId;
41
	}
42
43
	/**
44
	 * @return TemplateResponse
45
	 */
46 1
	public function getForm() {
47 1
		return new TemplateResponse('passman', 'part.admin');
48
	}
49
50
	/**
51
	 * @return string the section ID, e.g. 'sharing'
52
	 */
53 1
	public function getSection() {
54 1
		return 'additional';
55
	}
56
57
	/**
58
	 * @return int whether the form should be rather on the top or bottom of
59
	 * the admin section. The forms are arranged in ascending order of the
60
	 * priority values. It is required to return a value between 0 and 100.
61
	 *
62
	 * E.g.: 70
63
	 */
64 1
	public function getPriority() {
65 1
		return 0;
66
	}
67
68
	/**
69
	 * Get all settings
70
	 *
71
	 * @NoAdminRequired
72
	 * @NoCSRFRequired
73
	 */
74 1
	public function getSettings() {
75 1
		$settings = $this->settings->getAppSettings();
76 1
		return new JSONResponse($settings);
77
	}
78
79
	/**
80
	 * Save a user setting
81
	 *
82
	 * @NoAdminRequired
83
	 * @NoCSRFRequired
84
	 */
85 1
	public function saveUserSetting($key, $value) {
86 1
		$this->settings->setUserSetting($key, $value);
87 1
		return new JSONResponse('OK');
88
	}
89
90
91
	/**
92
	 * Save a app setting
93
	 *
94
	 * @NoCSRFRequired
95
	 */
96 1
	public function saveAdminSetting($key, $value) {
97 1
		$this->settings->setAppSetting($key, $value);
98 1
		return new JSONResponse('OK');
99
	}
100
101
}