Passed
Push — split-cluster-person ( 45a2b1...5162aa )
by Matias
05:48
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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
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['clusters'] = 0;
136
		$resp['images'] = array();
137
138
		if (!$userEnabled)
139
			return new DataResponse($resp);
140
141
		$modelId = $this->settingsService->getCurrentFaceModel();
142
143
		$clusters = $this->personMapper->findByName($this->userId, $modelId, $personName);
144
		foreach ($clusters as $cluster) {
145
			$resp['clusters']++;
146
147
			$faces = $this->faceMapper->findFacesFromPerson($this->userId, $cluster->getId(), $modelId);
148
			foreach ($faces as $face) {
149
				$image = $this->imageMapper->find($this->userId, $face->getImage());
150
151
				$fileId = $image->getFile();
152
				if ($fileId === null) continue;
153
154
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
155
				if ($fileUrl === null) continue;
156
157
				$thumbUrl = $this->urlService->getPreviewUrl($fileId, 256);
158
				if ($thumbUrl === null) continue;
159
160
				$image = [];
161
				$image['thumbUrl'] = $thumbUrl;
162
				$image['fileUrl'] = $fileUrl;
163
164
				$resp['images'][] = $image;
165
			}
166
		}
167
168
		return new DataResponse($resp);
169
	}
170
171
	/**
172
	 * @NoAdminRequired
173
	 *
174
	 * @param string $personName
175
	 * @param string $name
176
	 */
177
	public function updateName($personName, $name) {
178
		$modelId = $this->settingsService->getCurrentFaceModel();
179
		$clusters = $this->personMapper->findByName($this->userId, $modelId, $personName);
180
		foreach ($clusters as $person) {
181
			$person->setName($name);
182
			$this->personMapper->update($person);
183
		}
184
		return $this->find($name);
185
	}
186
187
}
188