Passed
Pull Request — master (#232)
by Matias
05:40 queued 04:10
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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 2
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
				$fileUrl = $this->getRedirectToFileUrl($image->getFile());
0 ignored issues
show
Bug introduced by
It seems like $image->getFile() can also be of type null; however, parameter $fileId of OCA\FaceRecognition\Cont...:getRedirectToFileUrl() does only seem to accept integer, 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

125
				$fileUrl = $this->getRedirectToFileUrl(/** @scrutinizer ignore-type */ $image->getFile());
Loading history...
126
				if (NULL === $fileUrl)
127
					continue;
128
129
				$face = [];
130
				$face['thumb-url'] = $this->getThumbUrl($personFace->getId());
131
				$face['file-url'] = $fileUrl;
132
				$faces[] = $face;
133
134
				if (--$limit === 0)
135
					break;
136
			}
137
138
			$cluster = [];
139
			$cluster['name'] = $person->getName();
140
			$cluster['count'] = count($personFaces);
141
			$cluster['id'] = $person->getId();
142
			$cluster['faces'] = $faces;
143
144
			$resp['clusters'][] = $cluster;
145
		}
146
		return new DataResponse($resp);
147
	}
148
149
	/**
150
	 * @NoAdminRequired
151
	 */
152
	public function find(int $id) {
153
		$person = $this->personMapper->find($this->userId, $id);
154
155
		$resp = [];
156
		$faces = [];
157
		$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $this->settingsService->getCurrentFaceModel());
158
		foreach ($personFaces as $personFace) {
159
			$image = $this->imageMapper->find($this->userId, $personFace->getImage());
160
			$fileUrl = $this->getRedirectToFileUrl($image->getFile());
0 ignored issues
show
Bug introduced by
It seems like $image->getFile() can also be of type null; however, parameter $fileId of OCA\FaceRecognition\Cont...:getRedirectToFileUrl() does only seem to accept integer, 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

160
			$fileUrl = $this->getRedirectToFileUrl(/** @scrutinizer ignore-type */ $image->getFile());
Loading history...
161
			if (NULL === $fileUrl)
162
				continue;
163
			$face = [];
164
			$face['thumb-url'] = $this->getThumbUrl($personFace->getId());
165
			$face['file-url'] = $fileUrl;
166
			$faces[] = $face;
167
		}
168
		$resp['name'] = $person->getName();
169
		$resp['id'] = $person->getId();
170
		$resp['faces'] = $faces;
171
172
		return new DataResponse($resp);
173
	}
174
175
	/**
176
	 * @NoAdminRequired
177
	 */
178
	public function findByName(string $personName) {
179
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
180
181
		$resp = array();
182
		$resp['enabled'] = $userEnabled;
183
		$resp['clusters'] = array();
184
185
		if (!$userEnabled)
186
			return new DataResponse($resp);
187
188
		$modelId = $this->settingsService->getCurrentFaceModel();
189
190
		$persons = $this->personMapper->findByName($this->userId, $modelId, $personName);
191
		foreach ($persons as $person) {
192
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId);
193
194
			$faces = [];
195
			foreach ($personFaces as $personFace) {
196
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
197
				$fileUrl = $this->getRedirectToFileUrl($image->getFile());
0 ignored issues
show
Bug introduced by
It seems like $image->getFile() can also be of type null; however, parameter $fileId of OCA\FaceRecognition\Cont...:getRedirectToFileUrl() does only seem to accept integer, 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

197
				$fileUrl = $this->getRedirectToFileUrl(/** @scrutinizer ignore-type */ $image->getFile());
Loading history...
198
				if (NULL === $fileUrl)
199
					continue;
200
201
				$face = [];
202
				$face['thumb-url'] = $this->getThumbUrl($personFace->getId());
203
				$face['file-url'] = $fileUrl;
204
				$faces[] = $face;
205
			}
206
207
			$cluster = [];
208
			$cluster['name'] = $person->getName();
209
			$cluster['count'] = count($personFaces);
210
			$cluster['id'] = $person->getId();
211
			$cluster['faces'] = $faces;
212
			$resp['clusters'][] = $cluster;
213
		}
214
215
		return new DataResponse($resp);
216
	}
217
218
	/**
219
	 * @NoAdminRequired
220
	 *
221
	 * @param int $id
222
	 * @param string $name
223
	 */
224
	public function updateName($id, $name) {
225
		$person = $this->personMapper->find ($this->userId, $id);
226
		$person->setName($name);
227
		$this->personMapper->update($person);
228
229
		$newPerson = $this->personMapper->find($this->userId, $id);
230
		return new DataResponse($newPerson);
231
	}
232
233
	/**
234
	 * Url to thumb face
235
	 *
236
	 * @param string $faceId face id to show
237
	 */
238
	private function getThumbUrl($faceId) {
239
		$params = [];
240
		$params['id'] = $faceId;
241
		$params['size'] = 50;
242
		return $this->urlGenerator->linkToRoute('facerecognition.face.getThumb', $params);
243
	}
244
245
	/**
246
	 * Redirects to the file list and highlight the given file id
247
	 *
248
	 * @param int $fileId file id to show
249
	 */
250
	private function getRedirectToFileUrl(int $fileId) {
251
		$uid        = $this->userSession->getUser()->getUID();
252
		$baseFolder = $this->rootFolder->getUserFolder($uid);
253
		$files      = $baseFolder->getById($fileId);
254
		$file       = current($files);
255
256
		if(!($file instanceof File)) {
257
			// If we cannot find a file probably it was deleted out of our control and we must clean our tables.
258
			$this->settingsService->setNeedRemoveStaleImages(true, $this->userId);
259
			return NULL;
260
		}
261
262
		$params = [];
263
		$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
264
		$params['scrollto'] = $file->getName();
265
266
		return $this->urlGenerator->linkToRoute('files.view.index', $params);
267
	}
268
269
}
270