Scan::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 14
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Audio Player
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2016-2021 Marcel Scherello
10
 */
11
 
12
namespace OCA\audioplayer\Command;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Scan extends Command {
20
	private $userManager;
21
	private $scanner;
22
	public function __construct(
23
			\OCP\IUserManager $userManager, 
24
			$scanner
25
		) {
26
		$this->userManager = $userManager;
27
		$this->scanner = $scanner;
28
		parent::__construct();
29
	}
30
31
	protected function configure() {
32
		$this
33
			->setName('audioplayer:scan')
34
            ->setDescription('scan for new audio files; use -v for debugging')
35
			->addArgument(
36
					'user_id',
37
					InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
38
					'scan all audio files of the given user(s)'
39
			)
40
			->addOption(
41
					'all',
42
					null,
43
					InputOption::VALUE_NONE,
44
					'scan all audio files of all known users'
45
			)
46
		;
47
	}
48
	
49
	protected function execute(InputInterface $input, OutputInterface $output) {
50
        # restrict the verbosity level to VERBOSITY_VERY_VERBOSE
51
        if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERY_VERBOSE) {
52
            $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
53
        }
54
55
        if ($input->getOption('all')) {
56
			$users = $this->userManager->search('');
57
		} else {
58
			$users = $input->getArgument('user_id');
59
		}
60
61
		if (count($users) === 0) {
62
			$output->writeln("<error>Please specify a valid user id to scan, \"--all\" to scan for all users<error>");
63
            return 1;
64
		}
65
		
66
		foreach ($users as $userId) {
67
			if (is_object($userId)) $user = $userId;
68
			else $user = $this->userManager->get($userId);
69
70
			if ($user === null) {
71
				$output->writeln("<error>User $userId does not exist</error>");
72
			} else {
73
				$userId = $user->getUID();
74
				$output->writeln("<info>Start scan for $userId</info>");
75
                $this->scanner->scanForAudios($userId, $output);
76
			}
77
		}
78
        return 0;
79
    }
80
}
81