Test Failed
Push — master ( 6e750b...ddc13c )
by Daniel
19:00
created

ConfigService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 16
ccs 4
cts 4
cp 1
rs 9.8333
cc 1
nc 1
nop 2
crap 1
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 IUserSession */
68
	protected $userSession;
69
70
	/** @var array<string,string> */
71
	private $defaults;
72
73
	/**
74
	 * ConfigService constructor.
75
	 *
76
	 * @param IConfig      $config
77
	 * @param IUserSession $userSession
78
	 */
79
	public function __construct(IConfig $config, IUserSession $userSession)
80
	{
81
		$this->config = $config;
82
		$this->userSession = $userSession;
83
84
		$this->defaults = [
85
			self::SYSTEM_TEMPLATES => '',
86
			self::CUSTOM_TEMPLATES => '',
87
			self::SYSTEM_THEMES => '',
88
			self::CUSTOM_THEMES => '',
89 1
			self::THEMES_ETAG => '',
90
			self::SYSTEM_PLUGINS => '',
91 1
			self::CUSTOM_PLUGINS => '',
92 1
			self::PLUGINS_ETAG => '',
93 1
			self::LIMIT_GROUPS => '',
94
			self::LINK_MODE => (string) WebsitesService::LINK_MODE_LONG,
95
		];
96
	}
97
98
	/**
99
	 * @param string $key
100 5
	 *
101
	 * @return string
102 5
	 */
103 5
	public function getAppValue(string $key): string
104
	{
105
		$defaultValue = $this->getDefaultValue($key);
106
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
107
	}
108
109
	/**
110 2
	 * @param string $key
111
	 * @param string $value
112 2
	 */
113 2
	public function setAppValue(string $key, string $value): void
114
	{
115
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
116
	}
117
118
	/**
119
	 * @param string $key
120
	 */
121
	public function deleteAppValue(string $key): void
122
	{
123
		$this->config->deleteAppValue(Application::APP_NAME, $key);
124
	}
125
126
	/**
127
	 * @param string $key
128
	 * @param mixed  $defaultValue
129
	 *
130
	 * @return mixed
131
	 */
132
	public function getSystemValue(string $key, $defaultValue = '')
133
	{
134
		return $this->config->getSystemValue($key, $defaultValue);
135
	}
136
137
	/**
138
	 * @param string $key
139
	 *
140
	 * @return string
141
	 */
142
	protected function getDefaultValue(string $key): string
143
	{
144
		return $this->defaults[$key] ?? '';
145
	}
146
}
147