Passed
Pull Request — master (#293)
by Matias
06:20 queued 04:27
created

PersonController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 21
ccs 0
cts 20
cp 0
crap 2
rs 9.9666

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-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
24
namespace OCA\FaceRecognition\Controller;
25
26
use OCP\IRequest;
27
use OCP\Files\IRootFolder;
28
use OCP\Files\File;
29
use OCP\IUserSession;
30
use OCP\IURLGenerator;
31
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
use OCP\AppFramework\Http\JSONResponse;
35
use OCP\AppFramework\Http\DataDisplayResponse;
36
use OCP\AppFramework\Controller;
37
38
use OCA\FaceRecognition\Db\Face;
39
use OCA\FaceRecognition\Db\FaceMapper;
40
41
use OCA\FaceRecognition\Db\Image;
42
use OCA\FaceRecognition\Db\ImageMapper;
43
44
use OCA\FaceRecognition\Db\Person;
45
use OCA\FaceRecognition\Db\PersonMapper;
46
47
use OCA\FaceRecognition\Service\SettingsService;
48
49
50
class PersonController extends Controller {
51
52
	/** @var IRootFolder */
53
	private $rootFolder;
54
55
	/** @var IUserSession */
56
	private $userSession;
57
58
	/** @var IURLGenerator */
59
	private $urlGenerator;
60
61
	/** @var FaceMapper */
62
	private $faceMapper;
63
64
	/** @var ImageMapper */
65
	private $imageMapper;
66
67
	/** @var PersonMapper */
68
	private $personMapper;
69
70
	/** @var SettingsService */
71
	private $settingsService;
72
73
	/** @var string */
74
	private $userId;
75
76
	public function __construct($AppName,
77
	                            IRequest        $request,
78
	                            IRootFolder     $rootFolder,
79
	                            IUserSession    $userSession,
80
	                            IURLGenerator   $urlGenerator,
81
	                            FaceMapper      $faceMapper,
82
	                            ImageMapper     $imageMapper,
83
	                            PersonMapper    $personmapper,
84
	                            SettingsService $settingsService,
85
	                            $UserId)
86
	{
87
		parent::__construct($AppName, $request);
88
89
		$this->rootFolder      = $rootFolder;
90
		$this->userSession     = $userSession;
91
		$this->urlGenerator    = $urlGenerator;
92
		$this->faceMapper      = $faceMapper;
93
		$this->imageMapper     = $imageMapper;
94
		$this->personMapper    = $personmapper;
95
		$this->settingsService = $settingsService;
96
		$this->userId          = $UserId;
97
	}
98
99
	/**
100
	 * @NoAdminRequired
101
	 */
102
	public function index() {
103
		$notGrouped = $this->settingsService->getShowNotGrouped();
104
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
105
106
		$resp = array();
107
		$resp['enabled'] = $userEnabled;
108
		$resp['clusters'] = array();
109
110
		if (!$userEnabled)
111
			return new DataResponse($resp);
112
113
		$modelId = $this->settingsService->getCurrentFaceModel();
114
115
		$persons = $this->personMapper->findAll($this->userId, $modelId);
116
		foreach ($persons as $person) {
117
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId);
118
			if ($notGrouped === false && count($personFaces) <= 1)
119
				continue;
120
121
			$limit = 14;
122
			$faces = [];
123
			foreach ($personFaces as $personFace) {
124
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
125
				$fileId = $image->getFile();
126
				if ($fileId === null) continue;
127
128
				$fileUrl = $this->getRedirectToFileUrl($fileId);
129
				if ($fileUrl === null) continue;
130
131
				$face = [];
132
				$face['thumb-url'] = $this->getThumbUrl($personFace->getId());
133
				$face['file-url'] = $fileUrl;
134
				$faces[] = $face;
135
136
				if (--$limit === 0)
137
					break;
138
			}
139
140
			$cluster = [];
141
			$cluster['name'] = $person->getName();
142
			$cluster['count'] = count($personFaces);
143
			$cluster['id'] = $person->getId();
144
			$cluster['faces'] = $faces;
145
146
			$resp['clusters'][] = $cluster;
147
		}
148
		return new DataResponse($resp);
149
	}
150
151
	/**
152
	 * @NoAdminRequired
153
	 */
154
	public function find(int $id) {
155
		$person = $this->personMapper->find($this->userId, $id);
156
157
		$resp = [];
158
		$faces = [];
159
		$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $this->settingsService->getCurrentFaceModel());
160
		foreach ($personFaces as $personFace) {
161
			$image = $this->imageMapper->find($this->userId, $personFace->getImage());
162
			$fileId = $image->getFile();
163
			if ($fileId === null) continue;
164
165
			$fileUrl = $this->getRedirectToFileUrl($fileId);
166
			if ($fileUrl === null) continue;
167
168
			$face = [];
169
			$face['thumb-url'] = $this->getThumbUrl($personFace->getId());
170
			$face['file-url'] = $fileUrl;
171
			$faces[] = $face;
172
		}
173
		$resp['name'] = $person->getName();
174
		$resp['id'] = $person->getId();
175
		$resp['faces'] = $faces;
176
177
		return new DataResponse($resp);
178
	}
179
180
	/**
181
	 * @NoAdminRequired
182
	 */
183
	public function findByName(string $personName) {
184
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
185
186
		$resp = array();
187
		$resp['enabled'] = $userEnabled;
188
		$resp['clusters'] = array();
189
190
		if (!$userEnabled)
191
			return new DataResponse($resp);
192
193
		$modelId = $this->settingsService->getCurrentFaceModel();
194
195
		$persons = $this->personMapper->findByName($this->userId, $modelId, $personName);
196
		foreach ($persons as $person) {
197
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId);
198
199
			$faces = [];
200
			foreach ($personFaces as $personFace) {
201
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
202
				$fileId = $image->getFile();
203
				if ($fileId === null) continue;
204
205
				$fileUrl = $this->getRedirectToFileUrl($fileId);
206
				if ($fileUrl === null) continue;
207
208
				$face = [];
209
				$face['thumb-url'] = $this->getThumbUrl($personFace->getId());
210
				$face['file-url'] = $fileUrl;
211
				$faces[] = $face;
212
			}
213
214
			$cluster = [];
215
			$cluster['name'] = $person->getName();
216
			$cluster['count'] = count($personFaces);
217
			$cluster['id'] = $person->getId();
218
			$cluster['faces'] = $faces;
219
			$resp['clusters'][] = $cluster;
220
		}
221
222
		return new DataResponse($resp);
223
	}
224
225
	/**
226
	 * @NoAdminRequired
227
	 *
228
	 * @param int $id
229
	 * @param string $name
230
	 */
231
	public function updateName($id, $name) {
232
		$person = $this->personMapper->find ($this->userId, $id);
233
		$person->setName($name);
234
		$this->personMapper->update($person);
235
236
		$newPerson = $this->personMapper->find($this->userId, $id);
237
		return new DataResponse($newPerson);
238
	}
239
240
	/**
241
	 * Url to thumb face
242
	 *
243
	 * @param string $faceId face id to show
244
	 */
245
	private function getThumbUrl($faceId) {
246
		$params = [];
247
		$params['id'] = $faceId;
248
		$params['size'] = 50;
249
		return $this->urlGenerator->linkToRoute('facerecognition.face.getThumb', $params);
250
	}
251
252
	/**
253
	 * Redirects to the file list and highlight the given file id
254
	 *
255
	 * @param int $fileId file id to show
256
	 */
257
	private function getRedirectToFileUrl(int $fileId) {
258
		$uid        = $this->userSession->getUser()->getUID();
259
		$baseFolder = $this->rootFolder->getUserFolder($uid);
260
		$files      = $baseFolder->getById($fileId);
261
		$file       = current($files);
262
263
		if(!($file instanceof File)) {
264
			// If we cannot find a file probably it was deleted out of our control and we must clean our tables.
265
			$this->settingsService->setNeedRemoveStaleImages(true, $this->userId);
266
			return NULL;
267
		}
268
269
		$params = [];
270
		$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
271
		$params['scrollto'] = $file->getName();
272
273
		return $this->urlGenerator->linkToRoute('files.view.index', $params);
274
	}
275
276
}
277