Completed
Pull Request — master (#26700)
by Philipp
08:19
created

apps/files_external/lib/Command/Config.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Robin Appelman <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2016, ownCloud GmbH.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Files_External\Command;
24
25
use OC\Core\Command\Base;
26
use OCA\Files_External\Lib\StorageConfig;
27
use OCA\Files_External\NotFoundException;
28
use OCA\Files_External\Service\GlobalStoragesService;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Helper\Table;
31
use Symfony\Component\Console\Helper\TableHelper;
32
use Symfony\Component\Console\Input\InputArgument;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Input\InputOption;
35
use Symfony\Component\Console\Output\OutputInterface;
36
37
class Config extends Base {
38
	/**
39
	 * @var GlobalStoragesService
40
	 */
41
	protected $globalService;
42
43
	function __construct(GlobalStoragesService $globalService) {
44
		parent::__construct();
45
		$this->globalService = $globalService;
46
	}
47
48 View Code Duplication
	protected function configure() {
49
		$this
50
			->setName('files_external:config')
51
			->setDescription('Manage backend configuration for a mount')
52
			->addArgument(
53
				'mount_id',
54
				InputArgument::REQUIRED,
55
				'The id of the mount to edit'
56
			)->addArgument(
57
				'key',
58
				InputArgument::REQUIRED,
59
				'key of the config option to set/get'
60
			)->addArgument(
61
				'value',
62
				InputArgument::OPTIONAL,
63
				'value to set the config option to, when no value is provided the existing value will be printed'
64
			);
65
		parent::configure();
66
	}
67
68
	protected function execute(InputInterface $input, OutputInterface $output) {
69
		$mountId = $input->getArgument('mount_id');
70
		$key = $input->getArgument('key');
71
		try {
72
			$mount = $this->globalService->getStorage($mountId);
73
		} catch (NotFoundException $e) {
74
			$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
75
			return 404;
76
		}
77
78
		$value = $input->getArgument('value');
79
		if ($value) {
80
			$this->setOption($mount, $key, $value, $output);
0 ignored issues
show
It seems like $mount defined by $this->globalService->getStorage($mountId) on line 72 can be null; however, OCA\Files_External\Command\Config::setOption() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
81
		} else {
82
			$this->getOption($mount, $key, $output);
0 ignored issues
show
It seems like $mount defined by $this->globalService->getStorage($mountId) on line 72 can be null; however, OCA\Files_External\Command\Config::getOption() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
83
		}
84
	}
85
86
	/**
87
	 * @param StorageConfig $mount
88
	 * @param string $key
89
	 * @param OutputInterface $output
90
	 */
91
	protected function getOption(StorageConfig $mount, $key, OutputInterface $output) {
92
		if ($key === 'mountpoint' || $key === 'mount_point') {
93
			$value = $mount->getMountPoint();
94
		} else {
95
			$value = $mount->getBackendOption($key);
96
		}
97
		if (!is_string($value)) { // show bools and objects correctly
98
			$value = json_encode($value);
99
		}
100
		$output->writeln($value);
101
	}
102
103
	/**
104
	 * @param StorageConfig $mount
105
	 * @param string $key
106
	 * @param string $value
107
	 * @param OutputInterface $output
108
	 */
109
	protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) {
110
		$decoded = json_decode($value, true);
111
		if (!is_null($decoded)) {
112
			$value = $decoded;
113
		}
114
		if ($key === 'mountpoint' || $key === 'mount_point') {
115
			$mount->setMountPoint($value);
116
		} else {
117
			$mount->setBackendOption($key, $value);
118
		}
119
		$this->globalService->updateStorage($mount);
120
	}
121
}
122