Passed
Push — master ( 2dc1bc...a15032 )
by Pauli
02:06
created

BaseCommand::getArgumentGroupUsers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 15
ccs 0
cts 9
cp 0
crap 20
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2018 - 2020
11
 */
12
13
namespace OCA\Music\Command;
14
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
abstract class BaseCommand extends Command {
22
	protected $userManager;
23
	protected $groupManager;
24
25
	public function __construct(\OCP\IUserManager $userManager, \OCP\IGroupManager $groupManager) {
26
		$this->userManager = $userManager;
27
		$this->groupManager = $groupManager;
28
		parent::__construct();
29
	}
30
31
	protected function configure() {
32
		$this
33
			->addArgument(
34
				'user_id',
35
				InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
36
				'specify one or more targeted users'
37
			)
38
			->addOption(
39
				'all',
40
				null,
41
				InputOption::VALUE_NONE,
42
				'target all known users'
43
			)
44
			->addOption(
45
				'group',
46
				null,
47
				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
48
				'specify a targeted group to include all users of that group'
49
			)
50
		;
51
		$this->doConfigure();
52
	}
53
54
	protected function execute(InputInterface $input, OutputInterface $output) {
55
		try {
56
			self::ensureUsersGiven($input);
57
			$argUsers = $this->getArgumentUsers($input);
58
			$groupUsers = $this->getArgumentGroupUsers($input);
59
			$users = \array_unique(\array_merge($argUsers, $groupUsers));
60
			if (!$input->getOption('all') && !\count($users)) {
61
				throw new \InvalidArgumentException("No users in selected groups!");
62
			}
63
			$this->doExecute($input, $output, $users);
64
		} catch (\InvalidArgumentException $e) {
65
			$output->writeln($e->getMessage());
66
		}
67
	}
68
69
	/**
70
	 * @return string[]
71
	 */
72
	private function getArgumentUsers(InputInterface $input) {
73
		/** @var string[] */
74
		$users = $input->getArgument('user_id');
75
		\assert(\is_array($users));
76
77
		foreach ($users as $user) {
78
			if (!$this->userManager->userExists($user)) {
79
				throw new \InvalidArgumentException("User <error>$user</error> does not exist!");
80
			}
81
		}
82
		return $users;
83
	}
84
85
	/**
86
	 * @return string[]
87
	 */
88
	private function getArgumentGroupUsers(InputInterface $input) {
89
		$users = [];
90
		$groups = $input->getOption('group');
91
		\assert(\is_array($groups));
92
93
		foreach ($groups as $group) {
94
			if (!$this->groupManager->groupExists($group)) {
95
				throw new \InvalidArgumentException("Group <error>$group</error> does not exist!");
96
			} else {
97
				foreach ($this->groupManager->get($group)->getUsers() as $user) {
98
					\array_push($users, $user->getUID());
99
				}
100
			}
101
		}
102
		return $users;
103
	}
104
105
	protected static function ensureUsersGiven(InputInterface $input) {
106
		if (!$input->getArgument('user_id')
107
			&& !$input->getOption('all')
108
			&& !$input->getOption('group')) {
109
			throw new \InvalidArgumentException("Specify either the target user(s), --group or --all");
110
		}
111
	}
112
113
	abstract protected function doConfigure();
114
	abstract protected function doExecute(InputInterface $input, OutputInterface $output, $users);
115
}
116