Completed
Push — master ( 886762...abb0a0 )
by Joas
128:26 queued 113:24
created

Admin::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, ownCloud, Inc.
5
 *
6
 * @author Arthur Schiwon <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OCA\UpdateNotification\Settings;
28
29
use OCA\UpdateNotification\UpdateChecker;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\IConfig;
32
use OCP\IDateTimeFormatter;
33
use OCP\IGroupManager;
34
use OCP\Settings\ISettings;
35
use OCP\Util;
36
37
class Admin implements ISettings {
38
	/** @var IConfig */
39
	private $config;
40
	/** @var UpdateChecker */
41
	private $updateChecker;
42
	/** @var IGroupManager */
43
	private $groupManager;
44
	/** @var IDateTimeFormatter */
45
	private $dateTimeFormatter;
46
47
	/**
48
	 * @param IConfig $config
49
	 * @param UpdateChecker $updateChecker
50
	 * @param IGroupManager $groupManager
51
	 * @param IDateTimeFormatter $dateTimeFormatter
52
	 */
53
	public function __construct(IConfig $config,
54
								UpdateChecker $updateChecker,
55
								IGroupManager $groupManager,
56
								IDateTimeFormatter $dateTimeFormatter) {
57
		$this->config = $config;
58
		$this->updateChecker = $updateChecker;
59
		$this->groupManager = $groupManager;
60
		$this->dateTimeFormatter = $dateTimeFormatter;
61
	}
62
63
	/**
64
	 * @return TemplateResponse
65
	 */
66
	public function getForm(): TemplateResponse {
67
		$lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat');
68
		$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
69
70
		$channels = [
71
			'daily',
72
			'beta',
73
			'stable',
74
			'production',
75
		];
76
		$currentChannel = Util::getChannel();
77
		if ($currentChannel === 'git') {
78
			$channels[] = 'git';
79
		}
80
81
		$updateState = $this->updateChecker->getUpdateState();
82
83
		$notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
84
85
		$defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/';
86
		$updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL);
87
88
		$params = [
89
			'isNewVersionAvailable' => !empty($updateState['updateAvailable']),
90
			'isUpdateChecked' => $lastUpdateCheckTimestamp > 0,
91
			'lastChecked' => $lastUpdateCheck,
92
			'currentChannel' => $currentChannel,
93
			'channels' => $channels,
94
			'newVersionString' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'],
95
			'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'],
96
			'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'],
97
			'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL,
98
			'updateServerURL' => $updateServerURL,
99
			'notifyGroups' => $this->getSelectedGroups($notifyGroups),
100
		];
101
102
		$params = [
103
			'json' => json_encode($params),
104
		];
105
106
		return new TemplateResponse('updatenotification', 'admin', $params, '');
107
	}
108
109
	/**
110
	 * @param array $groupIds
111
	 * @return array
112
	 */
113
	protected function getSelectedGroups(array $groupIds): array {
114
		$result = [];
115
		foreach ($groupIds as $groupId) {
116
			$group = $this->groupManager->get($groupId);
117
118
			if ($group === null) {
119
				continue;
120
			}
121
122
			$result[] = ['value' => $group->getGID(), 'label' => $group->getDisplayName()];
123
		}
124
125
		return $result;
126
	}
127
128
	/**
129
	 * @return string the section ID, e.g. 'sharing'
130
	 */
131
	public function getSection(): string {
132
		return 'server';
133
	}
134
135
	/**
136
	 * @return int whether the form should be rather on the top or bottom of
137
	 * the admin section. The forms are arranged in ascending order of the
138
	 * priority values. It is required to return a value between 0 and 100.
139
	 *
140
	 * E.g.: 70
141
	 */
142
	public function getPriority(): int {
143
		return 1;
144
	}
145
}
146