Completed
Push — master ( 24938e...71a4d8 )
by Matias
23s queued 12s
created

ResetCommand::confirmate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
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\Helper\QuestionHelper;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Helper\QuestionHelper 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\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...
31
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...
32
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...
33
use Symfony\Component\Console\Question\ConfirmationQuestion;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Consol...on\ConfirmationQuestion 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...
34
35
use OCA\FaceRecognition\Service\FaceManagementService;
36
37
class ResetCommand extends Command {
38
39
	/** @var FaceManagementService */
40
	protected $faceManagementService;
41
42
	/** @var IUserManager */
43
	protected $userManager;
44
45
	/** @var InputInterface */
46
	protected $input;
47
48
	/** @var OutputInterface */
49
	protected $output;
50
51
	/** @var  QuestionHelper */
52
	protected $questionHelper;
53
54
	/**
55
	 * @param FaceManagementService $faceManagementService
56
	 * @param IUserManager $userManager
57
	 * @param QuestionHelper $questionHelper
58
	 */
59
	public function __construct(FaceManagementService $faceManagementService,
60
	                            IUserManager          $userManager,
61
	                            QuestionHelper        $questionHelper) {
62
		parent::__construct();
63
64
		$this->faceManagementService = $faceManagementService;
65
		$this->userManager           = $userManager;
66
		$this->questionHelper        = $questionHelper;
67
	}
68
69
	protected function configure() {
70
		$this
71
			->setName('face:reset')
72
			->setDescription(
73
				'Resets and deletes everything. Good for starting over. ' .
74
				'BEWARE: Next runs of face:background_job will re-analyze all images.')
75
			->addOption(
76
				'user_id',
77
				'u',
78
				InputOption::VALUE_REQUIRED,
79
				'Resets data for a given user only. If not given, resets everything for all users.',
80
				null
81
			)
82
			->addOption(
83
				'all',
84
				null,
85
				InputOption::VALUE_NONE,
86
				'Reset everything.',
87
				null
88
			)
89
			->addOption(
90
				'model',
91
				null,
92
				InputOption::VALUE_NONE,
93
				'Reset current model.',
94
				null
95
			)
96
			->addOption(
97
				'image-errors',
98
				null,
99
				InputOption::VALUE_NONE,
100
				'Reset errors in images to re-analyze again',
101
				null
102
			)
103
			->addOption(
104
				'clustering',
105
				null,
106
				InputOption::VALUE_NONE,
107
				'Just reset the clustering.',
108
				null
109
			);
110
	}
111
112
	/**
113
	 * @param InputInterface $input
114
	 * @param OutputInterface $output
115
	 * @return int
116
	 */
117
	protected function execute(InputInterface $input, OutputInterface $output) {
118
		// Used to questions.
119
		//
120
		$this->input = $input;
121
		$this->output = $output;
122
123
		// Extract user, if any
124
		//
125
		$userId = $input->getOption('user_id');
126
		$user = null;
127
128
		if (!is_null($userId)) {
129
			$user = $this->userManager->get($userId);
130
			if ($user === null) {
131
				$output->writeln("User with id <$userId> in unknown.");
132
				return 1;
133
			}
134
		}
135
136
		// Main thing
137
		//
138
		if ($input->getOption('all')) {
139
			if ($this->confirmate()) {
140
				$this->resetAll($user);
141
				$output->writeln('Reset successfully done');
142
			} else {
143
				$output->writeln('Aborted');
144
				return 1;
145
			}
146
		}
147
		else if ($input->getOption('model')) {
148
			if ($this->confirmate()) {
149
				$this->resetModel($user);
150
				$output->writeln('Reset model successfully done');
151
			} else {
152
				$output->writeln('Aborted');
153
				return 1;
154
			}
155
		}
156
		else if ($input->getOption('image-errors')) {
157
			if ($this->confirmate()) {
158
				$this->resetImageErrors($user);
159
				$output->writeln('Reset image errors done');
160
			} else {
161
				$output->writeln('Aborted');
162
				return 1;
163
			}
164
		}
165
		else if ($input->getOption('clustering')) {
166
			if ($this->confirmate()) {
167
				$this->resetClusters($user);
168
				$output->writeln('Reset clustering done');
169
			} else {
170
				$output->writeln('Aborted');
171
				return 1;
172
			}
173
		}
174
		else {
175
			$output->writeln('You must specify what you want to reset');
176
			return 1;
177
		}
178
179
		return 0;
180
	}
181
182
	private function confirmate() {
183
		$question = new ConfirmationQuestion('Warning: This command is not reversible. Do you want to continue? [y/N]', false);
184
		return $this->questionHelper->ask($this->input, $this->output, $question);
185
	}
186
187
	private function resetClusters($user) {
188
		$this->faceManagementService->resetClusters($user);
189
	}
190
191
	private function resetImageErrors($user) {
192
		$this->faceManagementService->resetImageErrors($user);
193
	}
194
195
	private function resetAll($user) {
196
		$this->faceManagementService->resetAll($user);
197
	}
198
199
	private function resetModel($user) {
200
		$this->faceManagementService->resetModel($user);
201
	}
202
203
}
204