Completed
Push — master ( 489c78...f25022 )
by Joas
06:06
created

Admin::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
	public function __construct(IConfig $config, IL10N $l10n, UserSettings $userSettings, IManager $manager) {
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
		foreach ($settings as $setting) {
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
		}
105
106
		return new TemplateResponse('activity', 'settings/admin', [
107
			'activities'		=> $activities,
108
			'activity_email'	=> '!empty',
109
110
			'setting_batchtime'	=> $settingBatchTime,
111
112
			'notify_self'		=> $this->userSettings->getConfigSetting('setting', 'self'),
113
			'notify_selfemail'	=> $this->userSettings->getConfigSetting('setting', 'selfemail'),
114
115
			'methods'			=> [
116
				IExtension::METHOD_MAIL => $this->l10n->t('Mail'),
117
				IExtension::METHOD_STREAM => $this->l10n->t('Stream'),
118
			],
119
		], 'blank');
120
	}
121
122
	/**
123
	 * @return string the section ID, e.g. 'sharing'
124
	 */
125
	public function getSection() {
126
		return 'activity';
127
	}
128
129
	/**
130
	 * @return int whether the form should be rather on the top or bottom of
131
	 * the admin section. The forms are arranged in ascending order of the
132
	 * priority values. It is required to return a value between 0 and 100.
133
	 *
134
	 * E.g.: 70
135
	 */
136
	public function getPriority() {
137
		return 55;
138
	}
139
140
}
141