Passed
Push — master ( 0c05c1...774e03 )
by Daniel
27:58
created

ConfigService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.8666
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>)
6
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
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
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
declare(strict_types=1);
25
26
namespace OCA\CMSPico\Service;
27
28
use OCA\CMSPico\AppInfo\Application;
29
use OCP\IConfig;
30
use OCP\IUserSession;
31
32
class ConfigService
33
{
34
	/** @var string */
35
	public const SYSTEM_TEMPLATES = 'system_templates';
36
37
	/** @var string */
38
	public const CUSTOM_TEMPLATES = 'custom_templates';
39
40
	/** @var string */
41
	public const SYSTEM_THEMES = 'system_themes';
42
43
	/** @var string */
44
	public const CUSTOM_THEMES = 'custom_themes';
45
46
	/** @var string */
47
	public const THEMES_ETAG = 'themes_etag';
48
49
	/** @var string */
50
	public const SYSTEM_PLUGINS = 'system_plugins';
51
52
	/** @var string */
53
	public const CUSTOM_PLUGINS = 'custom_plugins';
54
55
	/** @var string */
56
	public const PLUGINS_ETAG = 'plugins_etag';
57
58
	/** @var string */
59
	public const LIMIT_GROUPS = 'limit_groups';
60
61
	/** @var string */
62
	public const LINK_MODE = 'link_mode';
63
64
	/** @var IConfig */
65
	protected $config;
66
67
	/** @var array<string,string> */
68
	private $defaults;
69
70
	/**
71
	 * ConfigService constructor.
72
	 *
73
	 * @param IConfig      $config
74
	 */
75
	public function __construct(IConfig $config)
76
	{
77
		$this->config = $config;
78
79
		$this->defaults = [
80
			self::SYSTEM_TEMPLATES => '',
81
			self::CUSTOM_TEMPLATES => '',
82
			self::SYSTEM_THEMES => '',
83
			self::CUSTOM_THEMES => '',
84
			self::THEMES_ETAG => '',
85
			self::SYSTEM_PLUGINS => '',
86
			self::CUSTOM_PLUGINS => '',
87
			self::PLUGINS_ETAG => '',
88
			self::LIMIT_GROUPS => '',
89
			self::LINK_MODE => (string) WebsitesService::LINK_MODE_LONG,
90
		];
91
	}
92
93
	/**
94
	 * @param string $key
95
	 *
96
	 * @return string
97
	 */
98 25
	public function getAppValue(string $key): string
99
	{
100 25
		$defaultValue = $this->getDefaultValue($key);
101 25
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
102
	}
103
104
	/**
105
	 * @param string $key
106
	 * @param string $value
107
	 */
108
	public function setAppValue(string $key, string $value): void
109
	{
110
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
111
	}
112
113
	/**
114
	 * @param string $key
115
	 */
116
	public function deleteAppValue(string $key): void
117
	{
118
		$this->config->deleteAppValue(Application::APP_NAME, $key);
119
	}
120
121
	/**
122
	 * @param string $key
123
	 * @param mixed  $defaultValue
124
	 *
125
	 * @return mixed
126
	 */
127 6
	public function getSystemValue(string $key, $defaultValue = '')
128
	{
129 6
		return $this->config->getSystemValue($key, $defaultValue);
130
	}
131
132
	/**
133
	 * @param string $key
134
	 *
135
	 * @return string
136
	 */
137 25
	protected function getDefaultValue(string $key): string
138
	{
139 25
		return $this->defaults[$key] ?? '';
140
	}
141
}
142