Passed
Pull Request — master (#232)
by Matias
05:40 queued 04:10
created

SetupCommand::downloadModel()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 36
nc 6
nop 1
dl 0
loc 51
ccs 0
cts 43
cp 0
crap 30
rs 9.0328
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2019-2020 Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\FaceRecognition\Command;
24
25
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...
26
use Symfony\Component\Console\Helper\Table;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Helper\Table 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...
27
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...
28
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...
29
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...
30
31
use OCA\FaceRecognition\Model\IModel;
32
33
use OCA\FaceRecognition\Model\ModelManager;
34
35
class SetupCommand extends Command {
36
37
	/** @var ModelManager */
38
	protected $modelManager;
39
40
	/** @var OutputInterface */
41
	protected $logger;
42
43
	/**
44
	 * @param ModelManager
45
	 */
46
	public function __construct(ModelManager $modelManager)
47
	{
48
		parent::__construct();
49
50
		$this->modelManager = $modelManager;
51
	}
52
53
	protected function configure() {
54
		$this
55
			->setName('face:setup')
56
			->setDescription('Download and Setup the model used for the analysis')
57
			->addOption(
58
				'model',
59
				'm',
60
				InputOption::VALUE_OPTIONAL,
61
				'The identifier number of the model to install',
62
				null
63
			);
64
	}
65
66
	/**
67
	 * @param InputInterface $input
68
	 * @param OutputInterface $output
69
	 * @return int
70
	 */
71
	protected function execute(InputInterface $input, OutputInterface $output) {
72
		$this->logger = $output;
73
74
		$modelId = $input->getOption('model');
75
		if (is_null($modelId)) {
76
			$this->logger->writeln('You must indicate the ID of the model to install');
77
			$this->dumpModels();
78
			return 0;
79
		}
80
81
		$model = $this->modelManager->getModel($modelId);
82
		if (is_null($model)) {
83
			$this->logger->writeln('Invalid model Id');
84
			return 1;
85
		}
86
87
		$modelDescription = $model->getId() . ' (' . $model->getName(). ')';
88
89
		if (!$model->meetDependencies()) {
90
			$this->logger->writeln('You do not meet the dependencies to install the model ' . $modelDescription);
91
			return 1;
92
		}
93
94
		if ($model->isInstalled()) {
95
			$this->logger->writeln('The files of model ' . $modelDescription . ' are already installed');
96
			$this->modelManager->setDefault($modelId);
97
			$this->logger->writeln('The model ' . $modelDescription . ' was configured as default');
98
99
			return 0;
100
		}
101
102
		$this->logger->writeln('The model ' . $modelDescription . ' will be installed');
103
		$model->install();
104
		$this->logger->writeln('Install model ' . $modelDescription . ' successfully done');
105
106
		$this->modelManager->setDefault($modelId);
107
		$this->logger->writeln('The model ' . $modelDescription . ' was configured as default');
108
109
		return 0;
110
	}
111
112
	/**
113
	 * Print list of models
114
	 */
115
	private function dumpModels() {
116
		$table = new Table($this->logger);
117
		$table->setHeaders(['Id', 'Name', 'Description']);
118
119
		$models = $this->modelManager->getAllModels();
120
		foreach ($models as $model) {
121
			$table->addRow([$model->getId(), $model->getName(), $model->getDescription()]);
122
		}
123
		$table->render();
124
	}
125
126
}
127