Passed
Push — master ( 1b613c...568762 )
by Morris
17:15 queued 04:41
created

UpdateConfig::execute()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 37
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 55
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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) && !in_array($key, self::SUPPORTED_IMAGE_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 (strpos($value, '/') !== 0) {
120
				$output->writeln('<error>The image file needs to be provided as an absolute path: ' . $value . '.</error>');
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

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