Completed
Push — master ( b99d2b...8a743e )
by Morris
22:42 queued 11s
created

Config::getOption()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 2
b 0
f 0
nc 4
nop 3
dl 11
loc 11
rs 8.8571
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Files_External\Command;
25
26
use OC\Core\Command\Base;
27
use OCA\Files_External\Lib\StorageConfig;
28
use OCA\Files_External\NotFoundException;
29
use OCA\Files_External\Service\GlobalStoragesService;
30
use Symfony\Component\Console\Input\InputArgument;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
34
class Config extends Base {
35
	/**
36
	 * @var GlobalStoragesService
37
	 */
38
	protected $globalService;
39
40
	function __construct(GlobalStoragesService $globalService) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
		parent::__construct();
42
		$this->globalService = $globalService;
43
	}
44
45
	protected function configure() {
46
		$this
47
			->setName('files_external:config')
48
			->setDescription('Manage backend configuration for a mount')
49
			->addArgument(
50
				'mount_id',
51
				InputArgument::REQUIRED,
52
				'The id of the mount to edit'
53
			)->addArgument(
54
				'key',
55
				InputArgument::REQUIRED,
56
				'key of the config option to set/get'
57
			)->addArgument(
58
				'value',
59
				InputArgument::OPTIONAL,
60
				'value to set the config option to, when no value is provided the existing value will be printed'
61
			);
62
		parent::configure();
63
	}
64
65
	protected function execute(InputInterface $input, OutputInterface $output) {
66
		$mountId = $input->getArgument('mount_id');
67
		$key = $input->getArgument('key');
68
		try {
69
			$mount = $this->globalService->getStorage($mountId);
70
		} catch (NotFoundException $e) {
71
			$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
72
			return 404;
73
		}
74
75
		$value = $input->getArgument('value');
76
		if ($value) {
77
			$this->setOption($mount, $key, $value, $output);
78
		} else {
79
			$this->getOption($mount, $key, $output);
80
		}
81
	}
82
83
	/**
84
	 * @param StorageConfig $mount
85
	 * @param string $key
86
	 * @param OutputInterface $output
87
	 */
88 View Code Duplication
	protected function getOption(StorageConfig $mount, $key, OutputInterface $output) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
89
		if ($key === 'mountpoint' || $key === 'mount_point') {
90
			$value = $mount->getMountPoint();
91
		} else {
92
			$value = $mount->getBackendOption($key);
93
		}
94
		if (!is_string($value) && json_decode(json_encode($value)) === $value) { // show bools and objects correctly
95
 			$value = json_encode($value);
96
 		}
97
		$output->writeln($value);
98
	}
99
100
	/**
101
	 * @param StorageConfig $mount
102
	 * @param string $key
103
	 * @param string $value
104
	 * @param OutputInterface $output
105
	 */
106 View Code Duplication
	protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
107
		$decoded = json_decode($value, true);
108
 		if (!is_null($decoded) && json_encode($decoded) === $value) {
109
 			$value = $decoded;
110
 		}
111
		if ($key === 'mountpoint' || $key === 'mount_point') {
112
			$mount->setMountPoint($value);
113
		} else {
114
			$mount->setBackendOption($key, $value);
115
		}
116
		$this->globalService->updateStorage($mount);
117
	}
118
}
119