Passed
Push — master ( c233ac...ceefe0 )
by Joas
14:23 queued 13s
created

Delete::completeArgumentValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Jens-Christian Fischer <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
namespace OC\Core\Command\User;
26
27
use OC\Core\Command\Base;
28
use OCP\IUser;
29
use OCP\IUserManager;
30
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
31
use Symfony\Component\Console\Input\InputArgument;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class Delete extends Base {
36
	/** @var IUserManager */
37
	protected $userManager;
38
39
	/**
40
	 * @param IUserManager $userManager
41
	 */
42
	public function __construct(IUserManager $userManager) {
43
		$this->userManager = $userManager;
44
		parent::__construct();
45
	}
46
47
	protected function configure() {
48
		$this
49
			->setName('user:delete')
50
			->setDescription('deletes the specified user')
51
			->addArgument(
52
				'uid',
53
				InputArgument::REQUIRED,
54
				'the username'
55
			);
56
	}
57
58
	protected function execute(InputInterface $input, OutputInterface $output): int {
59
		$user = $this->userManager->get($input->getArgument('uid'));
60
		if (is_null($user)) {
61
			$output->writeln('<error>User does not exist</error>');
62
			return 0;
63
		}
64
65
		if ($user->delete()) {
66
			$output->writeln('<info>The specified user was deleted</info>');
67
			return 0;
68
		}
69
70
		$output->writeln('<error>The specified user could not be deleted. Please check the logs.</error>');
71
		return 1;
72
	}
73
74
	/**
75
	 * @param string $argumentName
76
	 * @param CompletionContext $context
77
	 * @return string[]
78
	 */
79
	public function completeArgumentValues($argumentName, CompletionContext $context) {
80
		if ($argumentName === 'uid') {
81
			return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
82
		}
83
		return [];
84
	}
85
}
86