Reset   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 14 1
A execute() 0 25 6
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
use OCA\audioplayer\Controller\DbController;
19
20
class Reset extends Command {
21
	private $userManager;
22
    private $DBController;
23
24
    public function __construct(\OCP\IUserManager $userManager, DbController $DBController)
25
    {
26
		$this->userManager = $userManager;
27
        $this->DBController = $DBController;
28
		parent::__construct();
29
	}
30
	
31
	protected function configure() {
32
		$this
33
			->setName('audioplayer:reset')
34
			->setDescription('reset audio player library')
35
			->addArgument(
36
					'user_id',
37
					InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
38
					'reset the whole library of the given user(s)'
39
			)
40
			->addOption(
41
					'all',
42
					null,
43
					InputOption::VALUE_NONE,
44
					'reset the whole library of all known users'
45
			)
46
		;
47
	}
48
	
49
	protected function execute(InputInterface $input, OutputInterface $output) {
50
		if ($input->getOption('all')) {
51
			$users = $this->userManager->search('');
52
		} else {
53
			$users = $input->getArgument('user_id');
54
		}
55
56
		if (count($users) === 0) {
57
			$output->writeln("<error>Please specify a valid user id to reset, \"--all\" to scan for all users<error>");
58
            return 1;
59
		}
60
		
61
		foreach ($users as $userId) {
62
			if (is_object($userId)) $user = $userId;
63
			else $user = $this->userManager->get($userId);
64
65
			if ($user === null) {
66
				$output->writeln("<error>User $userId does not exist</error>");
67
			} else {
68
				$userId = $user->getUID();
69
				$output->writeln("<info>Reset library for $userId</info>");
70
                $this->DBController->resetMediaLibrary($userId, $output);
71
			}
72
		}
73
        return 0;
74
	}
75
}
76