Passed
Push — order-clusters ( 426304 )
by Matias
05:24
created

PersonController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
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 20
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
namespace OCA\FaceRecognition\Controller;
3
4
use OCP\IRequest;
5
use OCP\IConfig;
6
use OCP\Files\IRootFolder;
7
use OCP\Files\File;
8
use OCP\IUserSession;
9
use OCP\IURLGenerator;
10
use OCP\AppFramework\Http;
11
use OCP\AppFramework\Http\DataResponse;
12
use OCP\AppFramework\Http\JSONResponse;
13
use OCP\AppFramework\Http\DataDisplayResponse;
14
use OCP\AppFramework\Controller;
15
16
use OCA\FaceRecognition\Db\Face;
17
use OCA\FaceRecognition\Db\FaceMapper;
18
19
use OCA\FaceRecognition\Db\Image;
20
use OCA\FaceRecognition\Db\ImageMapper;
21
22
use OCA\FaceRecognition\Db\Person;
23
use OCA\FaceRecognition\Db\PersonMapper;
24
25
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
26
27
class PersonController extends Controller {
28
29
	private $config;
30
	private $rootFolder;
31
	private $userSession;
32
	private $urlGenerator;
33
	private $faceMapper;
34
	private $imageMapper;
35
	private $personMapper;
36
	private $userId;
37
38
	public function __construct($AppName,
39
	                            IRequest      $request,
40
	                            IConfig       $config,
41
	                            IRootFolder   $rootFolder,
42
	                            IUserSession  $userSession,
43
	                            IURLGenerator $urlGenerator,
44
	                            FaceMapper    $faceMapper,
45
	                            ImageMapper   $imageMapper,
46
	                            PersonMapper  $personmapper,
47
	                            $UserId)
48
	{
49
		parent::__construct($AppName, $request);
50
		$this->config       = $config;
51
		$this->rootFolder   = $rootFolder;
52
		$this->userSession  = $userSession;
53
		$this->urlGenerator = $urlGenerator;
54
		$this->imageMapper  = $imageMapper;
55
		$this->faceMapper   = $faceMapper;
56
		$this->personMapper = $personmapper;
57
		$this->userId       = $UserId;
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 */
63
	public function index() {
64
		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
65
		$notGrouped = $this->config->getAppValue('facerecognition', 'show-not-grouped', 'false');
66
		$userEnabled = $this->config->getUserValue($this->userId, 'facerecognition', 'enabled', 'false');
67
68
		$resp = array();
69
		$resp['enabled'] = $userEnabled;
70
		$resp['clusters'] = array();
71
72
		if ($userEnabled === 'true') {
73
			$persons = $this->personMapper->findAll($this->userId);
74
			foreach ($persons as $person) {
75
				$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $model);
76
				if ($notGrouped === 'false' && count($personFaces) <= 1)
77
					continue;
78
79
				$limit = 14;
80
				$faces = [];
81
				foreach ($personFaces as $personFace) {
82
					if ($limit-- === 0)
83
						break;
84
85
					$image = $this->imageMapper->find($this->userId, $personFace->getImage());
86
					$fileUrl = $this->getRedirectToFileUrl($image->getFile());
87
					if (NULL === $fileUrl)
88
						continue;
89
90
					$face = [];
91
					$face['thumb-url'] = $this->getThumbUrl($personFace->getId());
92
					$face['file-url'] = $fileUrl;
93
					$faces[] = $face;
94
				}
95
96
				$cluster = [];
97
				$cluster['name'] = $person->getName();
98
				$cluster['count'] = count($personFaces);
99
				$cluster['id'] = $person->getId();
100
				$cluster['faces'] = $faces;
101
102
				$resp['clusters'][] = $cluster;
103
			}
104
		}
105
		return new DataResponse($resp);
106
	}
107
108
	/**
109
	 * @NoAdminRequired
110
	 */
111
	public function find($id) {
112
		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
113
114
		$person = $this->personMapper->find($this->userId, $id);
115
116
		$resp = [];
117
		$faces = [];
118
		$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $model);
119
		foreach ($personFaces as $personFace) {
120
			$image = $this->imageMapper->find($this->userId, $personFace->getImage());
121
			$fileUrl = $this->getRedirectToFileUrl($image->getFile());
122
			if (NULL === $fileUrl)
123
				continue;
124
			$face = [];
125
			$face['thumb-url'] = $this->getThumbUrl($personFace->getId());
126
			$face['file-url'] = $fileUrl;
127
			$faces[] = $face;
128
		}
129
		$resp['name'] = $person->getName();
130
		$resp['id'] = $person->getId();
131
		$resp['faces'] = $faces;
132
133
		return new DataResponse($resp);
134
	}
135
136
	/**
137
	 * @NoAdminRequired
138
	 *
139
	 * @param int $id
140
	 * @param string $name
141
	 */
142
	public function updateName($id, $name) {
143
		$person = $this->personMapper->find ($this->userId, $id);
144
		$person->setName($name);
145
		$this->personMapper->update($person);
146
147
		$newPerson = $this->personMapper->find($this->userId, $id);
148
		return new DataResponse($newPerson);
149
	}
150
151
	private function getThumbUrl($faceId) {
152
		$params = [];
153
		$params['id'] = $faceId;
154
		$params['size'] = 50;
155
		return $this->urlGenerator->linkToRoute('facerecognition.face.getThumb', $params);
156
	}
157
158
	private function getRedirectToFileUrl($fileId) {
159
		$uid        = $this->userSession->getUser()->getUID();
160
		$baseFolder = $this->rootFolder->getUserFolder($uid);
161
		$files      = $baseFolder->getById($fileId);
162
		$file       = current($files);
163
164
		if(!($file instanceof File))
165
			return NULL;
166
167
		$params = [];
168
		$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
169
		$params['scrollto'] = $file->getName();
170
171
		return $this->urlGenerator->linkToRoute('files.view.index', $params);
172
	}
173
174
}
175