Completed
Push — master ( bd4cd2...6d9054 )
by Andras
02:51
created

SettingsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 5
crap 2
1
<?php
2
/**
3
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2014 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments\Controller;
13
14
use OCA\Richdocuments\WOPI\DiscoveryManager;
15
use \OCP\AppFramework\Controller;
16
use OCP\AppFramework\Http\JSONResponse;
17
use OCP\Files\IAppData;
18
use \OCP\IRequest;
19
use \OCP\IL10N;
20
use OCA\Richdocuments\AppConfig;
21
22
class SettingsController extends Controller{
23
	/** @var IL10N */
24
	private $l10n;
25
	/** @var AppConfig */
26
	private $appConfig;
27
	/** @var DiscoveryManager  */
28
	private $discoveryManager;
29
30
	/**
31
	 * @param string $appName
32
	 * @param IRequest $request
33
	 * @param IL10N $l10n
34
	 * @param AppConfig $appConfig
35
	 * @param DiscoveryManager $discoveryManager
36
	 */
37
	public function __construct($appName,
38
								IRequest $request,
39
								IL10N $l10n,
40
								AppConfig $appConfig,
41
								DiscoveryManager $discoveryManager) {
42
		parent::__construct($appName, $request);
43
		$this->l10n = $l10n;
44
		$this->appConfig = $appConfig;
45
		$this->discoveryManager = $discoveryManager;
46
	}
47
48
	/**
49
	 * @NoAdminRequired
50
	 *
51
	 * @return JSONResponse
52
	 */
53
	public function getSettings() {
54
		return new JSONResponse([
55
			'doc_format' => $this->appConfig->getAppValue('doc_format'),
56
			'wopi_url' => $this->appConfig->getAppValue('wopi_url'),
57
		]);
58
	}
59
60
	/**
61
	 * @param string $wopi_url
62
	 * @param string $doc_format
63
	 * @return JSONResponse
64
	 */
65
	public function setSettings($wopi_url,
66
								$doc_format){
67
		$message = $this->l10n->t('Saved');
68
69
		if ($wopi_url !== null){
70
			$this->appConfig->setAppValue('wopi_url', $wopi_url);
71
72
			$colon = strpos($wopi_url, ':', 0);
73
			if ($this->request->getServerProtocol() !== substr($wopi_url, 0, $colon)){
74
				$message = $this->l10n->t('Saved with error: Collabora Online should use the same protocol as the server installation.');
75
			}
76
		}
77
78
		if ($doc_format !== null) {
79
			$this->appConfig->setAppValue('doc_format', $doc_format);
80
		}
81
82
		$this->discoveryManager->refretch();
83
84
		$response = [
85
			'status' => 'success',
86
			'data' => array('message' => (string) $message)
87
		];
88
89
		return new JSONResponse($response);
90
	}
91
}
92