Admin   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 38.39 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 43
loc 112
ccs 0
cts 63
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
C getForm() 37 62 10
A getSection() 0 3 1
A getPriority() 0 3 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
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Activity\Settings;
23
24
use OCA\Activity\UserSettings;
25
use OCP\Activity\IExtension;
26
use OCP\Activity\IManager;
27
use OCP\Activity\ISetting;
28
use OCP\AppFramework\Http\TemplateResponse;
29
use OCP\IConfig;
30
use OCP\IL10N;
31
use OCP\Settings\ISettings;
32
33
class Admin implements ISettings {
34
35
	/** @var IConfig */
36
	protected $config;
37
38
	/** @var IL10N */
39
	protected $l10n;
40
41
	/** @var IManager */
42
	protected $manager;
43
44
	/** @var UserSettings */
45
	protected $userSettings;
46
47
	/**
48
	 * @param IConfig $config
49
	 * @param IL10N $l10n
50
	 * @param IManager $manager
51
	 * @param UserSettings $userSettings
52
	 */
53 View Code Duplication
	public function __construct(IConfig $config, IL10N $l10n, UserSettings $userSettings, IManager $manager) {
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...
54
		$this->config = $config;
55
		$this->l10n = $l10n;
56
		$this->manager = $manager;
57
		$this->userSettings = $userSettings;
58
	}
59
60
	/**
61
	 * @return TemplateResponse
62
	 */
63
	public function getForm() {
64
		$settings = $this->manager->getSettings();
65 View Code Duplication
		usort($settings, function(ISetting $a, ISetting $b) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
			if ($a->getPriority() === $b->getPriority()) {
67
				return $a->getIdentifier() > $b->getIdentifier();
68
			}
69
70
			return $a->getPriority() > $b->getPriority();
71
		});
72
73
		$activities = [];
74 View Code Duplication
		foreach ($settings as $setting) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
			if (!$setting->canChangeStream() && !$setting->canChangeMail()) {
76
				// No setting can be changed => don't display
77
				continue;
78
			}
79
80
			$methods = [];
81
			if ($setting->canChangeStream()) {
82
				$methods[] = IExtension::METHOD_STREAM;
83
			}
84
			if ($setting->canChangeMail()) {
85
				$methods[] = IExtension::METHOD_MAIL;
86
			}
87
88
			$identifier = $setting->getIdentifier();
89
90
			$activities[$identifier] = array(
91
				'desc'		=> $setting->getName(),
92
				IExtension::METHOD_MAIL		=> $this->userSettings->getConfigSetting('email', $identifier),
93
				IExtension::METHOD_STREAM	=> $this->userSettings->getConfigSetting('stream', $identifier),
94
				'methods'	=> $methods,
95
			);
96
		}
97
98
		$settingBatchTime = UserSettings::EMAIL_SEND_HOURLY;
99
		$currentSetting = (int) $this->userSettings->getConfigSetting('setting', 'batchtime');
100 View Code Duplication
		if ($currentSetting === 3600 * 24 * 7) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
101
			$settingBatchTime = UserSettings::EMAIL_SEND_WEEKLY;
102
		} else if ($currentSetting === 3600 * 24) {
103
			$settingBatchTime = UserSettings::EMAIL_SEND_DAILY;
104
		} else if ($currentSetting === 0) {
105
			$settingBatchTime = UserSettings::EMAIL_SEND_ASAP;
106
		}
107
108
		return new TemplateResponse('activity', 'settings/admin', [
109
			'setting'			=> 'admin',
110
			'activities'		=> $activities,
111
			'is_email_set'		=> true,
112
			'email_enabled'		=> $this->config->getAppValue('activity', 'enable_email', 'yes') === 'yes',
113
114
			'setting_batchtime'	=> $settingBatchTime,
115
116
			'notify_self'		=> $this->userSettings->getConfigSetting('setting', 'self'),
117
			'notify_selfemail'	=> $this->userSettings->getConfigSetting('setting', 'selfemail'),
118
119
			'methods'			=> [
120
				IExtension::METHOD_MAIL => $this->l10n->t('Mail'),
121
				IExtension::METHOD_STREAM => $this->l10n->t('Stream'),
122
			],
123
		], 'blank');
124
	}
125
126
	/**
127
	 * @return string the section ID, e.g. 'sharing'
128
	 */
129
	public function getSection() {
130
		return 'activity';
131
	}
132
133
	/**
134
	 * @return int whether the form should be rather on the top or bottom of
135
	 * the admin section. The forms are arranged in ascending order of the
136
	 * priority values. It is required to return a value between 0 and 100.
137
	 *
138
	 * E.g.: 70
139
	 */
140
	public function getPriority() {
141
		return 55;
142
	}
143
144
}
145