Completed
Push — master ( 79706e...890f75 )
by Joas
12:25 queued 01:12
created

AppConfigController::setValue()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 3
nop 3
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Provisioning_API\Controller;
23
24
25
use OCP\AppFramework\Http;
26
use OCP\AppFramework\Http\DataResponse;
27
use OCP\AppFramework\OCSController;
28
use OCP\IAppConfig;
29
use OCP\IConfig;
30
use OCP\IRequest;
31
32
class AppConfigController extends OCSController {
33
34
	/** @var IConfig */
35
	protected $config;
36
37
	/** @var IAppConfig */
38
	protected $appConfig;
39
40
	/**
41
	 * @param string $appName
42
	 * @param IRequest $request
43
	 * @param IConfig $config
44
	 * @param IAppConfig $appConfig
45
	 */
46
	public function __construct($appName,
47
								IRequest $request,
48
								IConfig $config,
49
								IAppConfig $appConfig) {
50
		parent::__construct($appName, $request);
51
		$this->config = $config;
52
		$this->appConfig = $appConfig;
53
	}
54
55
	/**
56
	 * @return DataResponse
57
	 */
58
	public function getApps() {
59
		return new DataResponse([
60
			'data' => $this->appConfig->getApps(),
61
		]);
62
	}
63
64
	/**
65
	 * @param string $app
66
	 * @return DataResponse
67
	 */
68
	public function getKeys($app) {
69
		try {
70
			$this->verifyAppId($app);
71
		} catch (\InvalidArgumentException $e) {
72
			return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
73
		}
74
		return new DataResponse([
75
			'data' => $this->config->getAppKeys($app),
76
		]);
77
	}
78
79
	/**
80
	 * @param string $app
81
	 * @param string $key
82
	 * @param string $defaultValue
83
	 * @return DataResponse
84
	 */
85
	public function getValue($app, $key, $defaultValue = '') {
86
		try {
87
			$this->verifyAppId($app);
88
		} catch (\InvalidArgumentException $e) {
89
			return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
90
		}
91
		return new DataResponse([
92
			'data' => $this->config->getAppValue($app, $key, $defaultValue),
93
		]);
94
	}
95
96
	/**
97
	 * @PasswordConfirmationRequired
98
	 * @param string $app
99
	 * @param string $key
100
	 * @param string $value
101
	 * @return DataResponse
102
	 */
103 View Code Duplication
	public function setValue($app, $key, $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
		try {
105
			$this->verifyAppId($app);
106
			$this->verifyConfigKey($app, $key);
107
		} catch (\InvalidArgumentException $e) {
108
			return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
109
		}
110
111
		$this->config->setAppValue($app, $key, $value);
112
		return new DataResponse();
113
	}
114
115
	/**
116
	 * @PasswordConfirmationRequired
117
	 * @param string $app
118
	 * @param string $key
119
	 * @return DataResponse
120
	 */
121 View Code Duplication
	public function deleteKey($app, $key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
		try {
123
			$this->verifyAppId($app);
124
			$this->verifyConfigKey($app, $key);
125
		} catch (\InvalidArgumentException $e) {
126
			return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
127
		}
128
129
		$this->config->deleteAppValue($app, $key);
130
		return new DataResponse();
131
	}
132
133
	/**
134
	 * @param string $app
135
	 * @throws \InvalidArgumentException
136
	 */
137
	protected function verifyAppId($app) {
138
		if (\OC_App::cleanAppId($app) !== $app) {
139
			throw new \InvalidArgumentException('Invalid app id given');
140
		}
141
	}
142
143
	/**
144
	 * @param string $app
145
	 * @param string $key
146
	 * @throws \InvalidArgumentException
147
	 */
148
	protected function verifyConfigKey($app, $key) {
149
		if (in_array($key, ['installed_version', 'enabled', 'types'])) {
150
			throw new \InvalidArgumentException('The given key can not be set');
151
		}
152
153
		if ($app === 'core' && (strpos($key, 'public_') === 0 || strpos($key, 'remote_') === 0)) {
154
			throw new \InvalidArgumentException('The given key can not be set');
155
		}
156
	}
157
}
158