Issues (125)

lib/Command/ResetCommand.php (3 issues)

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;
29
use Symfony\Component\Console\Helper\QuestionHelper;
30
use Symfony\Component\Console\Input\InputOption;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
use Symfony\Component\Console\Question\ConfirmationQuestion;
34
35
use OCA\FaceRecognition\Helper\CommandLock;
36
37
use OCA\FaceRecognition\Service\FaceManagementService;
38
39
class ResetCommand extends Command {
40
41
	/** @var FaceManagementService */
42
	protected $faceManagementService;
43
44
	/** @var IUserManager */
45
	protected $userManager;
46
47
	/** @var InputInterface */
48
	protected $input;
49
50
	/** @var OutputInterface */
51
	protected $output;
52
53
	/** @var  QuestionHelper */
54
	protected $questionHelper;
55
56
	/**
57
	 * @param FaceManagementService $faceManagementService
58
	 * @param IUserManager $userManager
59
	 * @param QuestionHelper $questionHelper
60
	 */
61
	public function __construct(FaceManagementService $faceManagementService,
62
	                            IUserManager          $userManager,
63
	                            QuestionHelper        $questionHelper) {
64
		parent::__construct();
65
66
		$this->faceManagementService = $faceManagementService;
67
		$this->userManager           = $userManager;
68
		$this->questionHelper        = $questionHelper;
69
	}
70
71
	/**
72
	 * @return void
73
	 */
74
	protected function configure() {
75
		$this
76
			->setName('face:reset')
77
			->setDescription(
78
				'Resets and deletes everything. Good for starting over. ' .
79
				'BEWARE: Next runs of face:background_job will re-analyze all images.')
80
			->addOption(
81
				'user_id',
82
				'u',
83
				InputOption::VALUE_REQUIRED,
84
				'Resets data for a given user only. If not given, resets everything for all users.',
85
				null
86
			)
87
			->addOption(
88
				'all',
89
				null,
90
				InputOption::VALUE_NONE,
91
				'Reset everything.',
92
				null
93
			)
94
			->addOption(
95
				'model',
96
				null,
97
				InputOption::VALUE_NONE,
98
				'Reset current model.',
99
				null
100
			)
101
			->addOption(
102
				'image-errors',
103
				null,
104
				InputOption::VALUE_NONE,
105
				'Reset errors in images to re-analyze again',
106
				null
107
			)
108
			->addOption(
109
				'clustering',
110
				null,
111
				InputOption::VALUE_NONE,
112
				'Just reset the clustering.',
113
				null
114
			);
115
	}
116
117
	/**
118
	 * @param InputInterface $input
119
	 * @param OutputInterface $output
120
	 * @return int
121
	 */
122
	protected function execute(InputInterface $input, OutputInterface $output) {
123
		// Used to questions.
124
		//
125
		$this->input = $input;
126
		$this->output = $output;
127
128
		// Extract user, if any
129
		//
130
		$userId = $input->getOption('user_id');
131
		$user = null;
132
133
		if (!is_null($userId)) {
134
			$user = $this->userManager->get($userId);
0 ignored issues
show
It seems like $userId can also be of type string[]; however, parameter $uid of OCP\IUserManager::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
			$user = $this->userManager->get(/** @scrutinizer ignore-type */ $userId);
Loading history...
135
			if ($user === null) {
136
				$output->writeln("User with id <$userId> in unknown.");
137
				return 1;
138
			}
139
		}
140
141
		// Get lock to avoid potential errors.
142
		//
143
		$lock = CommandLock::Lock('face:reset');
0 ignored issues
show
Are you sure the assignment to $lock is correct as OCA\FaceRecognition\Help...ock::Lock('face:reset') targeting OCA\FaceRecognition\Helper\CommandLock::lock() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
144
		if (!$lock) {
0 ignored issues
show
$lock is of type null, thus it always evaluated to false.
Loading history...
145
			$output->writeln("Another command ('". CommandLock::IsLockedBy().  "') is already running that prevents it from continuing.");
146
			return 1;
147
		}
148
149
		// Main thing
150
		//
151
		$ret = 0;
152
		if ($input->getOption('all')) {
153
			if ($this->confirmate()) {
154
				$this->resetAll($user);
155
				$output->writeln('Reset successfully done');
156
			} else {
157
				$output->writeln('Aborted');
158
				$ret = 1;
159
			}
160
		}
161
		else if ($input->getOption('model')) {
162
			if ($this->confirmate()) {
163
				$this->resetModel($user);
164
				$output->writeln('Reset model successfully done');
165
			} else {
166
				$output->writeln('Aborted');
167
				$ret = 1;
168
			}
169
		}
170
		else if ($input->getOption('image-errors')) {
171
			if ($this->confirmate()) {
172
				$this->resetImageErrors($user);
173
				$output->writeln('Reset image errors done');
174
			} else {
175
				$output->writeln('Aborted');
176
				$ret = 1;
177
			}
178
		}
179
		else if ($input->getOption('clustering')) {
180
			if ($this->confirmate()) {
181
				$this->resetClusters($user);
182
				$output->writeln('Reset clustering done');
183
			} else {
184
				$output->writeln('Aborted');
185
				$ret = 1;
186
			}
187
		}
188
		else {
189
			$output->writeln('You must specify what you want to reset');
190
			$ret = 1;
191
		}
192
193
		// Release obtained lock
194
		//
195
		CommandLock::Unlock($lock);
196
197
		return $ret;
198
	}
199
200
	private function confirmate() {
201
		$question = new ConfirmationQuestion('Warning: This command is not reversible. Do you want to continue? [y/N]', false);
202
		return $this->questionHelper->ask($this->input, $this->output, $question);
203
	}
204
205
	/**
206
	 * @param \OCP\IUser|null $user
207
	 */
208
	private function resetClusters(?\OCP\IUser $user): void {
209
		$this->faceManagementService->resetClusters($user);
210
	}
211
212
	/**
213
	 * @param \OCP\IUser|null $user
214
	 */
215
	private function resetImageErrors(?\OCP\IUser $user): void {
216
		$this->faceManagementService->resetImageErrors($user);
217
	}
218
219
	/**
220
	 * @param \OCP\IUser|null $user
221
	 */
222
	private function resetAll(?\OCP\IUser $user): void {
223
		$this->faceManagementService->resetAll($user);
224
	}
225
226
	/**
227
	 * @param \OCP\IUser|null $user
228
	 */
229
	private function resetModel(?\OCP\IUser $user): void {
230
		$this->faceManagementService->resetModel($user);
231
	}
232
233
}
234