Passed
Push — cleanup-after-deleted-users ( 9407f3...2c4c8c )
by Branko
02:10
created

BackgroundCommand::execute()   B

Complexity

Conditions 8
Paths 19

Size

Total Lines 50
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 23
nc 19
nop 2
dl 0
loc 50
ccs 0
cts 18
cp 0
crap 72
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018, 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\Files\IRootFolder;
27
use OCP\App\IAppManager;
28
use OCP\IConfig;
29
use OCP\IUserManager;
30
31
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...
32
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument 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\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...
34
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...
35
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...
36
37
use OCA\FaceRecognition\BackgroundJob\BackgroundService;
38
39
class BackgroundCommand extends Command {
40
41
	/** @var BackgroundService */
42
	protected $backgroundService;
43
44
	/** @var IUserManager */
45
	protected $userManager;
46
47
	/**
48
	 * @param BackgroundService $backgroundService
49
	 * @param IUserManager $userManager
50
	 */
51
	public function __construct(BackgroundService $backgroundService,
52
	                            IUserManager      $userManager) {
53
		parent::__construct();
54
55
		$this->backgroundService = $backgroundService;
56
		$this->userManager = $userManager;
57
	}
58
59
	protected function configure() {
60
		$this
61
			->setName('face:background_job')
62
			->setDescription('Equivalent of cron job to analyze images, extract faces and create clusters from found faces')
63
			->addOption(
64
				'user_id',
65
				'u',
66
				InputOption::VALUE_REQUIRED,
67
				'Analyze faces for the given user only. If not given, analyzes images for all users.',
68
				null
69
			)
70
			->addOption(
71
				'max_image_area',
72
				null,
73
				InputOption::VALUE_REQUIRED,
74
				'Caps maximum area (in pixels^2) of the image to be fed to neural network, effectively lowering needed memory. ' .
75
				'Use this if face detection crashes randomly.'
76
			)
77
			->addOption(
78
				'timeout',
79
				't',
80
				InputOption::VALUE_REQUIRED,
81
				'Sets timeout in seconds for this command. Default is without timeout, e.g. command runs indefinitely.',
82
				0
83
			);
84
	}
85
86
	/**
87
	 * @param InputInterface $input
88
	 * @param OutputInterface $output
89
	 * @return int
90
	 */
91
	protected function execute(InputInterface $input, OutputInterface $output) {
92
		$this->backgroundService->setLogger($output);
93
94
		// Extract user, if any
95
		//
96
		$userId = $input->getOption('user_id');
97
		$user = null;
98
99
		if (!is_null($userId)) {
100
			$user = $this->userManager->get($userId);
101
			if ($user === null) {
102
				throw new \InvalidArgumentException("User with id <$userId> in unknown.");
103
			}
104
		}
105
106
		// Extract timeout
107
		//
108
		$timeout = $input->getOption('timeout');
109
		if (!is_null($timeout)) {
110
			if ($timeout < 0) {
111
				throw new \InvalidArgumentException("Timeout must be positive value in seconds.");
112
			}
113
		} else {
114
			$timeout = 0;
115
		}
116
117
		// Extract max image area
118
		//
119
		$maxImageArea = $input->getOption('max_image_area');
120
		if (!is_null($maxImageArea)) {
121
			$maxImageArea = intval($maxImageArea);
122
123
			if ($maxImageArea == 0) {
124
				throw new \InvalidArgumentException("Max image area must be positive number.");
125
			}
126
127
			if ($maxImageArea < 0) {
128
				throw new \InvalidArgumentException("Max image area must be positive value.");
129
			}
130
		}
131
132
		// Extract verbosity (for command, we don't need this, but execute asks for it, if running from cron job).
133
		//
134
		$verbose = $input->getOption('verbose');
135
136
		// Main thing
137
		//
138
		$this->backgroundService->execute($timeout, $verbose, $user, $maxImageArea);
139
140
		return 0;
141
	}
142
}
143