Passed
Push — master ( b8cde2...3f0b7f )
by Joas
21:48 queued 07:07
created

SettingsController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeServer() 0 3 1
A addServer() 0 9 1
A __construct() 0 8 1
A checkServer() 0 14 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Björn Schießle <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Federation\Controller;
26
27
use OC\HintException;
28
use OCA\Federation\TrustedServers;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\IL10N;
32
use OCP\IRequest;
33
34
class SettingsController extends Controller {
35
36
	/** @var IL10N */
37
	private $l;
38
39
	/** @var  TrustedServers */
40
	private $trustedServers;
41
42
	/**
43
	 * @param string $AppName
44
	 * @param IRequest $request
45
	 * @param IL10N $l10n
46
	 * @param TrustedServers $trustedServers
47
	 */
48
	public function __construct($AppName,
49
								IRequest $request,
50
								IL10N $l10n,
51
								TrustedServers $trustedServers
52
	) {
53
		parent::__construct($AppName, $request);
54
		$this->l = $l10n;
55
		$this->trustedServers = $trustedServers;
56
	}
57
58
59
	/**
60
	 * add server to the list of trusted Nextclouds
61
	 *
62
	 * @param string $url
63
	 * @return DataResponse
64
	 * @throws HintException
65
	 */
66
	public function addServer($url) {
67
		$this->checkServer($url);
68
		$id = $this->trustedServers->addServer($url);
69
70
		return new DataResponse(
71
			[
72
				'url' => $url,
73
				'id' => $id,
74
				'message' => $this->l->t('Added to the list of trusted servers')
75
			]
76
		);
77
	}
78
79
	/**
80
	 * add server to the list of trusted Nextclouds
81
	 *
82
	 * @param int $id
83
	 * @return DataResponse
84
	 */
85
	public function removeServer($id) {
86
		$this->trustedServers->removeServer($id);
87
		return new DataResponse();
88
	}
89
90
	/**
91
	 * check if the server should be added to the list of trusted servers or not
92
	 *
93
	 * @param string $url
94
	 * @return bool
95
	 * @throws HintException
96
	 */
97
	protected function checkServer($url) {
98
		if ($this->trustedServers->isTrustedServer($url) === true) {
99
			$message = 'Server is already in the list of trusted servers.';
100
			$hint = $this->l->t('Server is already in the list of trusted servers.');
101
			throw new HintException($message, $hint);
102
		}
103
104
		if ($this->trustedServers->isOwnCloudServer($url) === false) {
105
			$message = 'No server to federate with found';
106
			$hint = $this->l->t('No server to federate with found');
107
			throw new HintException($message, $hint);
108
		}
109
110
		return true;
111
	}
112
}
113