Passed
Push — visibilities ( ff77ec...bb2343 )
by Matias
05:34
created

ClusterController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 1
b 0
f 1
nc 1
nop 8
dl 0
loc 17
ccs 0
cts 8
cp 0
crap 2
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2018-2021 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
24
namespace OCA\FaceRecognition\Controller;
25
26
use OCP\IRequest;
27
use OCP\Files\File;
28
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\JSONResponse;
32
use OCP\AppFramework\Http\DataDisplayResponse;
33
use OCP\AppFramework\Controller;
34
35
use OCA\FaceRecognition\Db\Face;
36
use OCA\FaceRecognition\Db\FaceMapper;
37
38
use OCA\FaceRecognition\Db\Image;
39
use OCA\FaceRecognition\Db\ImageMapper;
40
41
use OCA\FaceRecognition\Db\Person;
42
use OCA\FaceRecognition\Db\PersonMapper;
43
44
use OCA\FaceRecognition\Service\SettingsService;
45
use OCA\FaceRecognition\Service\UrlService;
46
47
48
class ClusterController extends Controller {
49
50
	/** @var FaceMapper */
51
	private $faceMapper;
52
53
	/** @var ImageMapper */
54
	private $imageMapper;
55
56
	/** @var PersonMapper */
57
	private $personMapper;
58
59
	/** @var SettingsService */
60
	private $settingsService;
61
62
	/** @var UrlService */
63
	private $urlService;
64
65
	/** @var string */
66
	private $userId;
67
68
	public function __construct($AppName,
69
	                            IRequest        $request,
70
	                            FaceMapper      $faceMapper,
71
	                            ImageMapper     $imageMapper,
72
	                            PersonMapper    $personmapper,
73
	                            SettingsService $settingsService,
74
	                            UrlService      $urlService,
75
	                            $UserId)
76
	{
77
		parent::__construct($AppName, $request);
78
79
		$this->faceMapper      = $faceMapper;
80
		$this->imageMapper     = $imageMapper;
81
		$this->personMapper    = $personmapper;
82
		$this->settingsService = $settingsService;
83
		$this->urlService      = $urlService;
84
		$this->userId          = $UserId;
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 *
90
	 * @return DataResponse
91
	 */
92
	public function find(int $id): DataResponse {
93
		$person = $this->personMapper->find($this->userId, $id);
94
95
		$resp = [];
96
		$faces = [];
97
		$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $this->settingsService->getCurrentFaceModel());
98
		foreach ($personFaces as $personFace) {
99
			$image = $this->imageMapper->find($this->userId, $personFace->getImage());
100
			$fileId = $image->getFile();
101
			if ($fileId === null) continue;
102
103
			$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
104
			if ($fileUrl === null) continue;
105
106
			$face = [];
107
			$face['thumbUrl'] = $this->urlService->getThumbUrl($personFace->getId(), 50);
108
			$face['fileUrl'] = $fileUrl;
109
			$faces[] = $face;
110
		}
111
		$resp['name'] = $person->getName();
112
		$resp['id'] = $person->getId();
113
		$resp['faces'] = $faces;
114
115
		return new DataResponse($resp);
116
	}
117
118
	/**
119
	 * @NoAdminRequired
120
	 *
121
	 * @return DataResponse
122
	 */
123
	public function findByName(string $personName): DataResponse {
124
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
125
126
		$resp = array();
127
		$resp['enabled'] = $userEnabled;
128
		$resp['clusters'] = array();
129
130
		if (!$userEnabled)
131
			return new DataResponse($resp);
132
133
		$modelId = $this->settingsService->getCurrentFaceModel();
134
135
		$persons = $this->personMapper->findByName($this->userId, $modelId, $personName);
136
		foreach ($persons as $person) {
137
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId);
138
139
			$faces = [];
140
			foreach ($personFaces as $personFace) {
141
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
142
				$fileId = $image->getFile();
143
				if ($fileId === null) continue;
144
145
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
146
				if ($fileUrl === null) continue;
147
148
				$face = [];
149
				$face['thumbUrl'] = $this->urlService->getThumbUrl($personFace->getId(), 50);
150
				$face['fileUrl'] = $fileUrl;
151
				$faces[] = $face;
152
			}
153
154
			$cluster = [];
155
			$cluster['name'] = $person->getName();
156
			$cluster['count'] = count($personFaces);
157
			$cluster['id'] = $person->getId();
158
			$cluster['faces'] = $faces;
159
			$resp['clusters'][] = $cluster;
160
		}
161
162
		return new DataResponse($resp);
163
	}
164
165
	/**
166
	 * @NoAdminRequired
167
	 *
168
	 * @return DataResponse
169
	 */
170
	public function findUnassigned(): DataResponse {
171
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
172
173
		$resp = array();
174
		$resp['enabled'] = $userEnabled;
175
		$resp['clusters'] = array();
176
177
		if (!$userEnabled)
178
			return new DataResponse($resp);
179
180
		$modelId = $this->settingsService->getCurrentFaceModel();
181
182
		$persons = $this->personMapper->findUnassigned($this->userId, $modelId);
183
		foreach ($persons as $person) {
184
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId, 40);
185
			if (count($personFaces) === 1)
186
				continue;
187
188
			$faces = [];
189
			foreach ($personFaces as $personFace) {
190
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
191
192
				$fileId = $image->getFile();
193
				if ($fileId === null) continue;
194
195
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
196
				if ($fileUrl === null) continue;
197
198
				$thumbUrl = $this->urlService->getThumbUrl($personFace->getId(), 50);
199
200
				$face = [];
201
				$face['thumbUrl'] = $thumbUrl;
202
				$face['fileUrl'] = $fileUrl;
203
204
				$faces[] = $face;
205
			}
206
207
			$cluster = [];
208
			$cluster['count'] = count($personFaces);
209
			$cluster['id'] = $person->getId();
210
			$cluster['faces'] = $faces;
211
			$resp['clusters'][] = $cluster;
212
		}
213
214
		return new DataResponse($resp);
215
	}
216
217
	/**
218
	 * @NoAdminRequired
219
	 *
220
	 * @param int $id
221
	 * @param bool $visible
222
	 *
223
	 * @return DataResponse
224
	 */
225
	public function setVisibility (int $id, bool $visible): DataResponse {
226
		$resp = array();
227
		$this->personMapper->setVisibility($id, $visible);
228
		return new DataResponse($resp);
229
	}
230
231
	/**
232
	 * @NoAdminRequired
233
	 *
234
	 * @param int $id if of cluster
235
	 * @param int $face id of face.
236
	 * @param string|null $name optional name to rename it.
237
	 *
238
	 * @return DataResponse
239
	 */
240
	public function detachFace (int $id, int $face, $name = null): DataResponse {
241
		$resp = array();
242
		$this->personMapper->detachFace($id, $face, $name);
243
		return new DataResponse($resp);
244
	}
245
246
	/**
247
	 * @NoAdminRequired
248
	 *
249
	 * @param int $id
250
	 * @param string $name
251
	 *
252
	 * @return DataResponse
253
	 */
254
	public function updateName($id, $name): DataResponse {
255
		$person = $this->personMapper->find ($this->userId, $id);
256
		$person->setName($name);
257
		$this->personMapper->update($person);
258
259
		$newPerson = $this->personMapper->find($this->userId, $id);
260
		return new DataResponse($newPerson);
261
	}
262
263
}
264