Passed
Push — master ( 132edd...186cf7 )
by Matias
21:30 queued 04:34
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
				'image-errors',
77
				null,
78
				InputOption::VALUE_NONE,
79
				'Reset errors in images to re-analyze again',
80
				null
81
			)
82
			->addOption(
83
				'clustering',
84
				null,
85
				InputOption::VALUE_NONE,
86
				'Just reset the clustering.',
87
				null
88
			);
89
	}
90
91
	/**
92
	 * @param InputInterface $input
93
	 * @param OutputInterface $output
94
	 * @return int
95
	 */
96
	protected function execute(InputInterface $input, OutputInterface $output) {
97
		// Extract user, if any
98
		//
99
		$userId = $input->getOption('user_id');
100
		$user = null;
101
102
		if (!is_null($userId)) {
103
			$user = $this->userManager->get($userId);
104
			if ($user === null) {
105
				$output->writeln("User with id <$userId> in unknown.");
106
				return 1;
107
			}
108
		}
109
110
		// Main thing
111
		//
112
		if ($input->getOption('all')) {
113
			$this->resetAll($user);
114
			$output->writeln('Reset successfully done');
115
			return 0;
116
		}
117
		else if ($input->getOption('image-errors')) {
118
			$this->resetImageErrors($user);
119
			$output->writeln('Reset image errors done');
120
			return 0;
121
		}
122
		else if ($input->getOption('clustering')) {
123
			$this->resetClusters($user);
124
			$output->writeln('Reset clustering done');
125
			return 0;
126
		}
127
		else {
128
			$output->writeln('You must specify that you want to reset');
129
			return 1;
130
		}
131
	}
132
133
	private function resetClusters($user) {
134
		$this->faceManagementService->resetClusters($user);
135
	}
136
137
	private function resetImageErrors($user) {
138
		$this->faceManagementService->resetImageErrors($user);
139
	}
140
141
	private function resetAll($user) {
142
		$this->faceManagementService->resetAll($user);
143
	}
144
145
}
146