Completed
Push — master ( b6eb50...929611 )
by Morris
40:39 queued 20:56
created

Admin::getForm()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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