Completed
Push — master ( 1f48f6...828106 )
by Lukas
10:12
created

Backends::formatConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * @author Robin Appelman <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
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 OCA\Files_External\Command;
23
24
use OC\Core\Command\Base;
25
use OCA\Files_External\Lib\Auth\AuthMechanism;
26
use OCA\Files_External\Lib\Backend\Backend;
27
use OCA\Files_External\Lib\DefinitionParameter;
28
use OCA\Files_External\Service\BackendService;
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\ArrayInput;
33
use Symfony\Component\Console\Input\InputArgument;
34
use Symfony\Component\Console\Input\InputInterface;
35
use Symfony\Component\Console\Input\InputOption;
36
use Symfony\Component\Console\Input\Input;
37
use Symfony\Component\Console\Output\OutputInterface;
38
39
class Backends extends Base {
40
	/** @var BackendService */
41
	private $backendService;
42
43
	function __construct(BackendService $backendService
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...
44
	) {
45
		parent::__construct();
46
47
		$this->backendService = $backendService;
48
	}
49
50
	protected function configure() {
51
		$this
52
			->setName('files_external:backends')
53
			->setDescription('Show available authentication and storage backends')
54
			->addArgument(
55
				'type',
56
				InputArgument::OPTIONAL,
57
				'only show backends of a certain type. Possible values are "authentication" or "storage"'
58
			)->addArgument(
59
				'backend',
60
				InputArgument::OPTIONAL,
61
				'only show information of a specific backend'
62
			);
63
		parent::configure();
64
	}
65
66
	protected function execute(InputInterface $input, OutputInterface $output) {
67
		$authBackends = $this->backendService->getAuthMechanisms();
68
		$storageBackends = $this->backendService->getBackends();
69
70
		$data = [
71
			'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends),
72
			'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends)
73
		];
74
75
		$type = $input->getArgument('type');
76
		$backend = $input->getArgument('backend');
77
		if ($type) {
78
			if (!isset($data[$type])) {
79
				$output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>');
80
				return 1;
81
			}
82
			$data = $data[$type];
83
84
			if ($backend) {
85
				if (!isset($data[$backend])) {
86
					$output->writeln('<error>Unknown backend "' . $backend . '" of type  "' . $type . '"</error>');
87
					return 1;
88
				}
89
				$data = $data[$backend];
90
			}
91
		}
92
93
		$this->writeArrayInOutputFormat($input, $output, $data);
94
	}
95
96
	private function serializeAuthBackend(\JsonSerializable $backend) {
97
		$data = $backend->jsonSerialize();
98
		$result = [
99
			'name' => $data['name'],
100
			'identifier' => $data['identifier'],
101
			'configuration' => $this->formatConfiguration($data['configuration'])
102
		];
103
		if ($backend instanceof Backend) {
104
			$result['storage_class'] = $backend->getStorageClass();
105
			$authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes()));
106
			$result['supported_authentication_backends'] = array_keys($authBackends);
107
			$authConfig = array_map(function (AuthMechanism $auth) {
108
				return $this->serializeAuthBackend($auth)['configuration'];
109
			}, $authBackends);
110
			$result['authentication_configuration'] = array_combine(array_keys($authBackends), $authConfig);
111
		}
112
		return $result;
113
	}
114
115
	/**
116
	 * @param DefinitionParameter[] $parameters
117
	 * @return string[]
118
	 */
119
	private function formatConfiguration(array $parameters) {
120
		$configuration = array_filter($parameters, function (DefinitionParameter $parameter) {
121
			return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN;
122
		});
123
		return array_map(function (DefinitionParameter $parameter) {
124
			return $parameter->getTypeName();
125
		}, $configuration);
126
	}
127
}
128