Passed
Push — master ( 4d1d4d...9d67c2 )
by Roeland
12:27 queued 13s
created

UpdateConfig::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 20
rs 9.7
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Theming\Command;
25
26
use OCA\Theming\ImageManager;
27
use OCA\Theming\ThemingDefaults;
28
use OCP\IConfig;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputArgument;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class UpdateConfig extends Command {
36
	public const SUPPORTED_KEYS = [
37
		'name', 'url', 'imprintUrl', 'privacyUrl', 'slogan', 'color'
38
	];
39
40
	public const SUPPORTED_IMAGE_KEYS = [
41
		'background', 'logo', 'favicon', 'logoheader'
42
	];
43
44
	private $themingDefaults;
45
	private $imageManager;
46
	private $config;
47
48
	public function __construct(ThemingDefaults $themingDefaults, ImageManager $imageManager, IConfig $config) {
49
		parent::__construct();
50
51
		$this->themingDefaults = $themingDefaults;
52
		$this->imageManager = $imageManager;
53
		$this->config = $config;
54
	}
55
56
	protected function configure() {
57
		$this
58
			->setName('theming:config')
59
			->setDescription('Set theming app config values')
60
			->addArgument(
61
				'key',
62
				InputArgument::OPTIONAL,
63
				'Key to update the theming app configuration (leave empty to get a list of all configured values)' . PHP_EOL .
64
				'One of: ' . implode(', ', self::SUPPORTED_KEYS)
65
			)
66
			->addArgument(
67
				'value',
68
				InputArgument::OPTIONAL,
69
				'Value to set (leave empty to obtain the current value)'
70
			)
71
			->addOption(
72
				'reset',
73
				'r',
74
				InputOption::VALUE_NONE,
75
				'Reset the given config key to default'
76
			);
77
	}
78
79
80
	protected function execute(InputInterface $input, OutputInterface $output): int {
81
		$key = $input->getArgument('key');
82
		$value = $input->getArgument('value');
83
84
		if ($key === null) {
85
			$output->writeln('Current theming config:');
86
			foreach (self::SUPPORTED_KEYS as $key) {
87
				$value = $this->config->getAppValue('theming', $key, '');
88
				$output->writeln('- ' . $key . ': ' . $value . '');
89
			}
90
			foreach (self::SUPPORTED_IMAGE_KEYS as $key) {
91
				$value = $this->config->getAppValue('theming', $key . 'Mime', '');
92
				$output->writeln('- ' . $key . ': ' . $value . '');
93
			}
94
			return 0;
95
		}
96
97
		if (!in_array($key, self::SUPPORTED_KEYS, true)) {
98
			$output->writeln('<error>Invalid config key provided</error>');
99
			return 1;
100
		}
101
102
		if ($input->getOption('reset')) {
103
			$defaultValue = $this->themingDefaults->undo($key);
104
			$output->writeln('<info>Reset ' . $key . ' to ' . $defaultValue . '</info>');
0 ignored issues
show
Bug introduced by
Are you sure $key of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
			$output->writeln('<info>Reset ' . /** @scrutinizer ignore-type */ $key . ' to ' . $defaultValue . '</info>');
Loading history...
105
			return 0;
106
		}
107
108
		if ($value === null) {
109
			$value = $this->config->getAppValue('theming', $key, '');
110
			if ($value !== '') {
111
				$output->writeln('<info>' . $key . ' is currently set to ' . $value . '</info>');
112
			} else {
113
				$output->writeln('<info>' . $key . ' is currently not set</info>');
114
			}
115
			return 0;
116
		}
117
118
		if (in_array($key, self::SUPPORTED_IMAGE_KEYS, true)) {
119
			if (file_exists(__DIR__ . $value)) {
0 ignored issues
show
Bug introduced by
Are you sure $value of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
			if (file_exists(__DIR__ . /** @scrutinizer ignore-type */ $value)) {
Loading history...
120
				$value = __DIR__ . $value;
121
			}
122
			if (!file_exists($value)) {
123
				$output->writeln('<error>File could not be found: ' . $value . '</error>');
124
				return 1;
125
			}
126
			$value = $this->imageManager->updateImage($key, $value);
127
			$key = $key . 'Mime';
128
		}
129
130
		$this->themingDefaults->set($key, $value);
131
		$output->writeln('<info>Updated ' . $key . ' to ' . $value . '</info>');
132
133
		return 0;
134
	}
135
}
136