Passed
Push — self-contained-model ( d41f1e...0b87d3 )
by Matias
04:14
created

SetupCommand::dumpModels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
		$this->logger->writeln('The model ' . $modelDescription . ' will be installed');
95
		if ($model->isInstalled()) {
96
			$this->logger->writeln('The files of model ' . $modelDescription . ' are already installed');
97
98
			$this->modelManager->setDefault($modelId);
99
			$this->logger->writeln('This model was configured as default');
100
101
			return 0;
102
		}
103
104
		$model->install();
105
		$this->logger->writeln('Install model ' . $modelDescription . ' successfully done');
106
107
		$this->modelManager->setDefault($modelId);
108
		$this->logger->writeln('The new model was configured as default');
109
110
		return 0;
111
	}
112
113
	private function dumpModels () {
114
		$table = new Table($this->logger);
115
		$table->setHeaders(['Id', 'Name', 'Description']);
116
117
		$models = $this->modelManager->getAllModels();
118
		foreach ($models as $model) {
119
			$table->addRow([$model->getId(), $model->getName(), $model->getDescription()]);
120
		}
121
		$table->render();
122
	}
123
124
}
125