Passed
Push — enconde-from-orig ( 13bb2b...c256b7 )
by Matias
05:03
created

MigrateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 18
ccs 0
cts 18
cp 0
crap 2
rs 9.7333
c 1
b 0
f 0
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\Db\FaceMapper;
34
35
use OCA\FaceRecognition\Model\SettingsManager;
0 ignored issues
show
Bug introduced by
The type OCA\FaceRecognition\Model\SettingsManager 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...
36
37
use OCA\FaceRecognition\Service\FaceManagementService;
38
39
class MigrateCommand extends Command {
40
41
	/** @var FaceManagementService */
42
	protected $faceManagementService;
43
44
	/** @var IUserManager */
45
	protected $userManager;
46
47
	/** @var ModelManager */
0 ignored issues
show
Bug introduced by
The type OCA\FaceRecognition\Command\ModelManager 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...
48
	protected $modelManager;
49
50
	/** @var FaceMapper */
51
	protected $faceMapper;
52
53
	/**
54
	 * @param FaceManagementService $faceManagementService
55
	 * @param IUserManager $userManager
56
	 */
57
	public function __construct(FaceManagementService $faceManagementService,
58
	                            IUserManager          $userManager,
59
	                            ModelManager          $modelManager,
60
	                            FaceMapper            $faceMapper)
61
	{
62
		parent::__construct();
63
64
		$this->faceManagementService = $faceManagementService;
65
		$this->userManager           = $userManager;
66
		$this->modelManager          = $modelManager;
67
		$this->faceMapper            = $faceMapper;
68
	}
69
70
	protected function configure() {
71
		$this
72
			->setName('face:migrate')
73
			->setDescription(
74
				'Migrate the faces found in a model and analyze with the current model.')
75
			->addOption(
76
				'user_id',
77
				'u',
78
				InputOption::VALUE_REQUIRED,
79
				'Migrate data for a given user only. If not given, migrate everything for all users.',
80
				null
81
			)
82
			->addOption(
83
				'model',
84
				'm',
85
				InputOption::VALUE_OPTIONAL,
86
				'The identifier number of the model to migrate',
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
		if (!is_null($userId)) {
101
			$user = $this->userManager->get($userId);
102
			if ($user === null) {
103
				$output->writeln("User with id <$userId> is unknown.");
104
				return 1;
105
			}
106
		}
107
108
		$modelId = $input->getOption('model');
109
		if (is_null($modelId)) {
110
			$this->logger->writeln("You must indicate the ID of the model to migrate");
111
			return 1;
112
		}
113
114
		$model = $this->modelManager->getModel($modelId);
115
		if (is_null($model)) {
116
			$this->logger->writeln("Invalid model Id");
117
			return 1;
118
		}
119
120
		if (!$model->isIstalled()) {
121
			$this->logger->writeln("The model <$modelId> is not installed");
122
			return 1;
123
		}
124
125
		$currentModel = $this->modelManager->getCurrentModel();
126
		$currentModelId = (!is_null($currentModel)) ? $currentModel->getId() : -1;
127
128
		if ($currentModelId === $modelId) {
129
			$this->logger->writeln("The proposed model <$modelId> to migrate must be other than the current one <$currentModelId>");
130
			return 1;
131
		}
132
133
		if (!$this->$faceManagementService->hasDataForUser($userId, $modelId)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $faceManagementService seems to be never defined.
Loading history...
134
			$this->logger->writeln("The proposed model <$modelId> to migrate is empty");
135
			return 1;
136
		}
137
138
		if ($this->$faceManagementService->hasDataForUser($userId, $modelId)) {
139
			$this->logger->writeln("The current model <$currentModelId> already has data. You cannot migrate to a used model.");
140
			return 1;
141
		}
142
143
144
	}
145
146
}
147