Passed
Push — master ( df6e91...2ec5d2 )
by Morris
78:37 queued 64:33
created

ConfigController::setConfig()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 5
nop 2
dl 0
loc 31
rs 8.8333
c 0
b 0
f 0
1
<?php
2
declare (strict_types = 1);
3
/**
4
 * @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\Accessibility\Controller;
24
25
use OCA\Accessibility\AccessibilityProvider;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\AppFramework\OCSController;
29
use OCP\AppFramework\OCS\OCSBadRequestException;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\IUserSession;
33
34
class ConfigController extends OCSController {
35
36
	/** @var string */
37
	protected $appName;
38
39
	/** @var string */
40
	protected $userId;
41
42
	/** @var string */
43
	protected $serverRoot;
44
45
	/** @var IConfig */
46
	private $config;
47
48
	/** @var IUserSession */
49
	private $userSession;
50
51
	/** @var AccessibilityProvider */
52
	private $accessibilityProvider;
53
54
	/**
55
	 * Config constructor.
56
	 *
57
	 * @param string $appName
58
	 * @param IRequest $request
59
	 * @param IConfig $config
60
	 * @param IUserSession $userSession
61
	 * @param AccessibilityProvider $accessibilityProvider
62
	 */
63
	public function __construct(string $appName,
64
								IRequest $request,
65
								IConfig $config,
66
								IUserSession $userSession,
67
								AccessibilityProvider $accessibilityProvider) {
68
		parent::__construct($appName, $request);
69
		$this->appName               = $appName;
70
		$this->config                = $config;
71
		$this->userSession           = $userSession;
72
		$this->accessibilityProvider = $accessibilityProvider;
73
		$this->userId				 = $userSession->getUser()->getUID();
74
	}
75
76
	/**
77
	 * @NoAdminRequired
78
	 *
79
	 * Get user accessibility config
80
	 *
81
	 * @param string $key theme or font
82
	 * @return DataResponse
83
	 */
84
	public function getConfig(): DataResponse {
85
		return new DataResponse([
86
			'theme' => $this->config->getUserValue($this->userId, $this->appName, 'theme', false),
87
			'font' => $this->config->getUserValue($this->userId, $this->appName, 'font', false)
88
		]);
89
	}
90
91
	/**
92
	 * @NoAdminRequired
93
	 *
94
	 * Set theme or font config
95
	 *
96
	 * @param string $key theme or font
97
	 * @return DataResponse
98
	 * @throws Exception
99
	 */
100
	public function setConfig(string $key, $value): DataResponse {
101
		if ($key === 'theme' || $key === 'font') {
102
103
			if ($value === false) {
104
				$this->config->deleteUserValue($this->userId, $this->appName, $key);
105
				$userValues = $this->config->getUserKeys($this->userId, $this->appName);
106
107
				// remove hash if no settings selected
108
				if (count($userValues) === 1 && $userValues[0] === 'icons-css') {
109
					$this->config->deleteUserValue($this->userId, $this->appName, 'icons-css');
110
				}
111
112
				return new DataResponse();
113
			}
114
115
			$themes = $this->accessibilityProvider->getThemes();
116
			$fonts  = $this->accessibilityProvider->getFonts();
117
118
			$availableOptions = array_map(function($option) {
119
				return $option['id'];
120
			}, array_merge($themes, $fonts));
121
122
			if (in_array($value, $availableOptions)) {
123
				$this->config->setUserValue($this->userId, $this->appName, $key, $value);
124
				return new DataResponse();
125
			}
126
127
			throw new OCSBadRequestException('Invalid value: ' . $value);
128
		}
129
130
		throw new OCSBadRequestException('Invalid key: ' . $key);
131
	}
132
133
}