Completed
Push — master ( e4992c...6d0a35 )
by
unknown
10:42
created

DeleteConfig::removeSubValue()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 3
dl 0
loc 19
rs 9.0111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Core\Command\Config\System;
23
24
use OC\Core\Command\Base;
25
use OC\SystemConfig;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
class DeleteConfig extends Base {
32
	/** * @var SystemConfig */
33
	protected $systemConfig;
34
35
	/**
36
	 * @param SystemConfig $systemConfig
37
	 */
38
	public function __construct(SystemConfig $systemConfig) {
39
		parent::__construct();
40
		$this->systemConfig = $systemConfig;
41
	}
42
43 View Code Duplication
	protected function configure() {
44
		parent::configure();
45
46
		$this
47
			->setName('config:system:delete')
48
			->setDescription('Delete a system config value.')
49
			->addArgument(
50
				'name',
51
				InputArgument::REQUIRED | InputArgument::IS_ARRAY,
52
				'Name of the config to delete, specify multiple for array parameter.'
53
			)
54
			->addOption(
55
				'error-if-not-exists',
56
				null,
57
				InputOption::VALUE_NONE,
58
				'Checks whether the config exists before deleting it.'
59
			)
60
		;
61
	}
62
63
	protected function execute(InputInterface $input, OutputInterface $output) {
64
		$configNames = $input->getArgument('name');
65
		$configName = $configNames[0];
66
67
		if (\sizeof($configNames) > 1) {
68 View Code Duplication
			if ($input->hasParameterOption('--error-if-not-exists') && !\in_array($configName, $this->systemConfig->getKeys())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
				$output->writeln('<error>System config ' . \implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
70
				return 1;
71
			}
72
73
			$value = $this->systemConfig->getValue($configName);
74
75
			try {
76
				$value = $this->removeSubValue(\array_slice($configNames, 1), $value, $input->hasParameterOption('--error-if-not-exists'));
77
			} catch (\UnexpectedValueException $e) {
78
				$output->writeln('<error>System config ' . \implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
79
				return 1;
80
			}
81
82
			$this->systemConfig->setValue($configName, $value);
83
			$output->writeln('<info>System config value ' . \implode(' => ', $configNames) . ' deleted</info>');
84
			return 0;
85
		} else {
86 View Code Duplication
			if ($input->hasParameterOption('--error-if-not-exists') && !\in_array($configName, $this->systemConfig->getKeys())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
				$output->writeln('<error>System config ' . $configName . ' could not be deleted because it did not exist</error>');
88
				return 1;
89
			}
90
91
			$this->systemConfig->deleteValue($configName);
92
			$output->writeln('<info>System config value ' . $configName . ' deleted</info>');
93
			return 0;
94
		}
95
	}
96
97
	protected function removeSubValue($keys, $currentValue, $throwError) {
98
		$nextKey = \array_shift($keys);
99
100
		if (\is_array($currentValue)) {
101
			if (isset($currentValue[$nextKey])) {
102
				if (empty($keys)) {
103
					unset($currentValue[$nextKey]);
104
				} else {
105
					$currentValue[$nextKey] = $this->removeSubValue($keys, $currentValue[$nextKey], $throwError);
106
				}
107
			} elseif ($throwError) {
108
				throw new \UnexpectedValueException('Config parameter does not exist');
109
			}
110
		} elseif ($throwError) {
111
			throw new \UnexpectedValueException('Config parameter does not exist');
112
		}
113
114
		return $currentValue;
115
	}
116
}
117