Passed
Pull Request — master (#751)
by Matias
11:11 queued 08:36
created

SyncAlbumsCommand::execute()   C

Complexity

Conditions 15
Paths 26

Size

Total Lines 81
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 15
eloc 57
c 3
b 1
f 1
nc 26
nop 2
dl 0
loc 81
ccs 0
cts 54
cp 0
crap 240
rs 5.9166

How to fix   Long Method    Complexity   

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) 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;
26
use Symfony\Component\Console\Helper\Table;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
use OCP\App\IAppManager;
32
33
use OCP\IUser;
34
use OCP\IUserManager;
35
36
use OCA\FaceRecognition\Helper\PhotoAlbums;
37
use OCA\FaceRecognition\Db\PersonMapper;
38
39
use OCA\FaceRecognition\Service\SettingsService;
40
41
class SyncAlbumsCommand extends Command {
42
43
	/** @var IUserManager */
44
	protected $userManager;
45
46
        /** @var PersonMapper Person mapper*/
47
	private $personMapper;
48
49
	/** @var IAppManager */
50
	private $appManager;
51
52
	/** @var PhotoAlbums */
53
	protected $photoAlbums;
54
55
	/** @var SettingsService */
56
	private $settingsService;
57
58
	/**
59
	 * @param IUserManager $userManager
60
	 * @param PersonMapper $personMapper
61
	 * @param PhotoAlbums $photoAlbums
62
	 * @param SettingsService $settingsService
63
	 * @param IAppManager $appManager
64
	 */
65
	public function __construct(IUserManager    $userManager,
66
	                            PersonMapper    $personMapper,
67
	                            IAppManager     $appManager,
68
	                            PhotoAlbums     $photoAlbums,
69
	                            SettingsService $settingsService)
70
	{
71
		parent::__construct();
72
73
		$this->appManager      = $appManager;
74
		$this->personMapper    = $personMapper;
75
		$this->userManager     = $userManager;
76
		$this->photoAlbums     = $photoAlbums;
77
		$this->settingsService = $settingsService;
78
	}
79
80
	/**
81
	 * @return void
82
	 */
83
	protected function configure() {
84
		$this
85
			->setName('face:sync-albums')
86
			->setDescription('Synchronize the people found with the photo albums')
87
			->addOption(
88
				'user_id',
89
				'u',
90
				InputOption::VALUE_REQUIRED,
91
				'Sync albums for a given user only. If not given, sync albums for all users.',
92
				null
93
			)->addOption(
94
				'list_person',
95
				'l',
96
				InputOption::VALUE_NONE,
97
				'List all persons defined for the given user_id.',
98
				null
99
			)->addOption(
100
				'person_name',
101
				'p',
102
				InputOption::VALUE_REQUIRED,
103
				'Sync albums for a given user and person name(s) (separate using comma). If not used, sync albums for all persons defined by the user.',
104
				null
105
			)->addOption(
106
				'mode',
107
				'm',
108
				InputOption::VALUE_REQUIRED,
109
				'Album creation mode. Use "album-per-person" to create one album for each given person via person_name parameter. Use "album-combined" to create one album for all person names given via person_name parameter.',
110
				'album-per-person'
111
			);
112
	}
113
114
	/**
115
	 * @param InputInterface $input
116
	 * @param OutputInterface $output
117
	 * @return int
118
	 */
119
	protected function execute(InputInterface $input, OutputInterface $output) {
120
		if (!$this->appManager->isEnabledForUser('photos')) {
121
			$output->writeln('The photos app is disabled.');
122
			return 1;
123
		}
124
125
		$users = array();
126
		$userId = $input->getOption('user_id');
127
		$personNames = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $personNames is dead and can be removed.
Loading history...
128
		$person_name = $input->getOption('person_name');
129
		$mode = $input->getOption('mode');
130
131
		if (!is_null($userId)) {
132
			if ($this->userManager->get($userId) === null) {
0 ignored issues
show
Bug introduced by
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

132
			if ($this->userManager->get(/** @scrutinizer ignore-type */ $userId) === null) {
Loading history...
133
				$output->writeln("User with id <$userId> in unknown.");
134
				return 1;
135
			}
136
			else {
137
				$users[] = $userId;
138
			}
139
		}
140
		else {
141
			$this->userManager->callForAllUsers(function (IUser $iUser) use (&$users) {
142
				$users[] = $iUser->getUID();
143
			});
144
		}
145
146
		if ($input->getOption('list_person')) {
147
			if (is_null($userId)) {
148
				$output->writeln("List option requires option user_id!");
149
				return 1;
150
			} else{
151
				$output->writeln("List of defined persons for the user <$userId> :");
152
				$modelId = $this->settingsService->getCurrentFaceModel();
153
				$distintNames = $this->personMapper->findDistinctNames($userId, $modelId);
154
				foreach ($distintNames as $key=>$distintName) {
155
					if ($key > 0 ){
156
						$output->write(", ");
157
					}
158
					$output->write($distintName->getName());
159
				}
160
				$output->writeln("");
161
				$output->writeln("Done.");
162
			}
163
			return 0;
164
		}
165
166
		foreach ($users as $userId) {
167
			if (!is_null($person_name)) {
168
				if (is_null($userId)) {
169
					$output->writeln("Person_name option requires option user_id!");
170
					return 1;
171
				}
172
				$output->writeln("Synchronizing albums for the user <$userId> and person_name <$person_name> using mode <$mode>... ");
173
				if ($mode === "album-per-person") {
174
					$personList = explode(",", $person_name);
175
					foreach ($personList as $person) {
176
						$this->photoAlbums->syncUserPersonNamesSelected($userId, $person, $output);
177
					}
178
				}
179
				else if ($mode === "album-combined") {
180
					$personList = explode(",", $person_name); 
181
					if (count($personList) < 2) {
182
						$output->writeln("Note parameter mode <$mode> requires at least two persons separated using coma.");
183
						return 1;
184
					}
185
					$this->photoAlbums->syncUserPersonNamesCombinedAlbum($userId, $personList, $output);
186
				}
187
				else {
188
					$output->writeln("Error: invalid value for parameter mode <$mode>. ");
189
					return 1;
190
				}
191
				$output->writeln("Done.");
192
			} else {
193
				$output->write("Synchronizing albums for the user <$userId>... ");
194
				$this->photoAlbums->syncUser($userId);
195
				$output->writeln("Done.");
196
			}
197
		}
198
199
		return 0;
200
	}
201
202
}
203