Completed
Push — master ( dfbc21...f148e3 )
by John
64:33 queued 45:49
created

ConfigController::setConfig()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 2
dl 8
loc 24
rs 9.2248
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 $serverRoot;
41
42
	/** @var IConfig */
43
	private $config;
44
45
	/** @var IUserSession */
46
	private $userSession;
47
48
	/** @var AccessibilityProvider */
49
	private $accessibilityProvider;
50
51
	/**
52
	 * Config constructor.
53
	 *
54
	 * @param string $appName
55
	 * @param IRequest $request
56
	 * @param IConfig $config
57
	 * @param IUserSession $userSession
58
	 * @param AccessibilityProvider $accessibilityProvider
59
	 */
60 View Code Duplication
	public function __construct(string $appName,
61
								IRequest $request,
62
								IConfig $config,
63
								IUserSession $userSession,
64
								AccessibilityProvider $accessibilityProvider) {
65
		parent::__construct($appName, $request);
66
		$this->appName               = $appName;
67
		$this->config                = $config;
68
		$this->userSession           = $userSession;
69
		$this->accessibilityProvider = $accessibilityProvider;
70
	}
71
72
	/**
73
	 * @NoAdminRequired
74
	 *
75
	 * Get user accessibility config
76
	 *
77
	 * @param string $key theme or font
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
78
	 * @return DataResponse
79
	 */
80
	public function getConfig(): DataResponse {
81
		return new DataResponse([
82
			'theme' => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false),
83
			'font' => $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false)
84
		]);
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 *
90
	 * Set theme or font config
91
	 *
92
	 * @param string $key theme or font
93
	 * @return DataResponse
94
	 * @throws Exception
95
	 */
96
	public function setConfig(string $key, $value): DataResponse {
97
		if ($key === 'theme' || $key === 'font') {
98
			$themes = $this->accessibilityProvider->getThemes();
99
			$fonts  = $this->accessibilityProvider->getFonts();
100
101 View Code Duplication
			if ($value === false) {
102
				$this->config->deleteUserValue($this->userSession->getUser()->getUID(), $this->appName, $key);
103
				return new DataResponse();
104
			}
105
106
			$availableOptions = array_map(function($option) {
107
				return $option['id'];
108
			}, array_merge($themes, $fonts));
109
110 View Code Duplication
			if (in_array($value, $availableOptions)) {
111
				$this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, $key, $value);
112
				return new DataResponse();
113
			}
114
115
			throw new OCSBadRequestException('Invalid value: ' . $value);
116
		}
117
118
		throw new OCSBadRequestException('Invalid key: ' . $key);
119
	}
120
121
}