Passed
Push — wipe-clustering ( 19b724 )
by Matias
04:21
created

ResetCommand::resetClusters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2020, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2019, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\FaceRecognition\Command;
25
26
use OCP\IUserManager;
27
28
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
33
use OCA\FaceRecognition\Service\FaceManagementService;
34
35
class ResetCommand extends Command {
36
37
	/** @var FaceManagementService */
38
	protected $faceManagementService;
39
40
	/** @var IUserManager */
41
	protected $userManager;
42
43
	/**
44
	 * @param FaceManagementService $faceManagementService
45
	 * @param IUserManager $userManager
46
	 */
47
	public function __construct(FaceManagementService $faceManagementService,
48
	                            IUserManager          $userManager) {
49
		parent::__construct();
50
51
		$this->faceManagementService = $faceManagementService;
52
		$this->userManager = $userManager;
53
	}
54
55
	protected function configure() {
56
		$this
57
			->setName('face:reset')
58
			->setDescription(
59
				'Resets and deletes everything. Good for starting over. ' .
60
				'BEWARE: Next runs of face:background_job will re-analyze all images.')
61
			->addOption(
62
				'user_id',
63
				'u',
64
				InputOption::VALUE_REQUIRED,
65
				'Resets data for a given user only. If not given, resets everything for all users.',
66
				null
67
			)
68
			->addOption(
69
				'all',
70
				null,
71
				InputOption::VALUE_NONE,
72
				'Reset everything.',
73
				null
74
			)
75
			->addOption(
76
				'clustering',
77
				null,
78
				InputOption::VALUE_NONE,
79
				'Just reset the clustering.',
80
				null
81
			);
82
	}
83
84
	/**
85
	 * @param InputInterface $input
86
	 * @param OutputInterface $output
87
	 * @return int
88
	 */
89
	protected function execute(InputInterface $input, OutputInterface $output) {
90
		// Extract user, if any
91
		//
92
		$userId = $input->getOption('user_id');
93
		$user = null;
94
95
		if (!is_null($userId)) {
96
			$user = $this->userManager->get($userId);
97
			if ($user === null) {
98
				throw new \InvalidArgumentException("User with id <$userId> in unknown.");
99
			}
100
		}
101
102
		// Main thing
103
		//
104
		if ($input->getOption('all')) {
105
			$this->resetAll($user);
106
			$output->writeln('Reset successfully done');
107
			return 0;
108
		}
109
		else if ($input->getOption('clustering')) {
110
			$this->resetClusters($user);
111
			$output->writeln('Reset clustering done');
112
			return 0;
113
		}
114
		else {
115
			$output->writeln('You must specify that you want to reset...');
116
		}
117
	}
118
119
	private function resetClusters($user) {
120
		$this->faceManagementService->resetClusters($user);
121
	}
122
123
	private function resetAll($user) {
124
		$this->faceManagementService->resetAll($user);
125
	}
126
127
}
128