Passed
Push — php8 ( 3e2147...190cd5 )
by Matias
10:58 queued 08:41
created

PersonController::__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 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 17
ccs 0
cts 16
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-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\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 PersonController 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
	public function index() {
91
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
92
93
		$resp = array();
94
		$resp['enabled'] = $userEnabled;
95
		$resp['persons'] = array();
96
97
		if (!$userEnabled)
98
			return new DataResponse($resp);
99
100
		$modelId = $this->settingsService->getCurrentFaceModel();
101
102
		$personsNames = $this->personMapper->findDistinctNames($this->userId, $modelId);
103
		foreach ($personsNames as $personNamed) {
104
			$facesCount = 0;
105
			$faceUrl = null;
106
			$persons = $this->personMapper->findByName($this->userId, $modelId, $personNamed->getName());
107
			foreach ($persons as $person) {
108
				$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId);
109
				if (is_null($faceUrl)) {
110
					$faceUrl = $this->urlService->getThumbUrl($personFaces[0]->getId(), 128);
111
				}
112
				$facesCount += count($personFaces);
113
			}
114
115
			$person = [];
116
			$person['name'] = $personNamed->getName();
117
			$person['thumbUrl'] = $faceUrl;
118
			$person['count'] = $facesCount;
119
120
			$resp['persons'][] = $person;
121
		}
122
123
		return new DataResponse($resp);
124
	}
125
126
	/**
127
	 * @NoAdminRequired
128
	 */
129
	public function find(string $personName) {
130
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
131
132
		$resp = array();
133
		$resp['enabled'] = $userEnabled;
134
		$resp['name'] = $personName;
135
		$resp['thumbUrl'] = null;
136
		$resp['clusters'] = 0;
137
		$resp['images'] = array();
138
139
		if (!$userEnabled)
140
			return new DataResponse($resp);
141
142
		$faceUrl = null;
143
		$modelId = $this->settingsService->getCurrentFaceModel();
144
145
		$clusters = $this->personMapper->findByName($this->userId, $modelId, $personName);
146
		foreach ($clusters as $cluster) {
147
			$resp['clusters']++;
148
149
			$faces = $this->faceMapper->findFacesFromPerson($this->userId, $cluster->getId(), $modelId);
150
			if (is_null($faceUrl)) {
151
				$faceUrl = $this->urlService->getThumbUrl($faces[0]->getId(), 128);
152
				$resp['thumbUrl'] = $faceUrl;
153
			}
154
			foreach ($faces as $face) {
155
				$image = $this->imageMapper->find($this->userId, $face->getImage());
156
157
158
				$fileId = $image->getFile();
159
				if ($fileId === null) continue;
160
161
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
162
				if ($fileUrl === null) continue;
163
164
				$thumbUrl = $this->urlService->getPreviewUrl($fileId, 256);
165
				if ($thumbUrl === null) continue;
166
167
				$image = [];
168
				$image['thumbUrl'] = $thumbUrl;
169
				$image['fileUrl'] = $fileUrl;
170
171
				$resp['images'][] = $image;
172
			}
173
		}
174
175
		return new DataResponse($resp);
176
	}
177
178
	/**
179
	 * @NoAdminRequired
180
	 *
181
	 * @param string $personName
182
	 * @param string $name
183
	 */
184
	public function updateName($personName, $name) {
185
		$modelId = $this->settingsService->getCurrentFaceModel();
186
		$clusters = $this->personMapper->findByName($this->userId, $modelId, $personName);
187
		foreach ($clusters as $person) {
188
			$person->setName($name);
189
			$this->personMapper->update($person);
190
		}
191
		return $this->find($name);
192
	}
193
194
	/**
195
	 * @NoAdminRequired
196
	 */
197
	public function autocomplete(string $query) {
198
		$resp = array();
199
200
		if (!$this->settingsService->getUserEnabled($this->userId))
201
			return new DataResponse($resp);
202
203
		$modelId = $this->settingsService->getCurrentFaceModel();
204
205
		$persons = $this->personMapper->findPersonsLike($this->userId, $modelId, $query);
206
		foreach ($persons as $person) {
207
			$name = [];
208
			$name['name'] = $person->getName();
209
			$name['value'] = $person->getName();
210
			$resp[] = $name;
211
		}
212
		return new DataResponse($resp);
213
    }
214
215
}
216