Passed
Push — restore-progress ( c8e1da...d00b2a )
by Matias
03:19
created

FileController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 8
dl 0
loc 16
ccs 0
cts 16
cp 0
crap 2
rs 10
c 0
b 0
f 0

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\AppFramework\Http;
8
use OCP\AppFramework\Http\DataResponse;
9
use OCP\AppFramework\Http\JSONResponse;
10
use OCP\AppFramework\Http\DataDisplayResponse;
11
use OCP\AppFramework\Controller;
12
13
use OCA\FaceRecognition\Db\Image;
14
use OCA\FaceRecognition\Db\ImageMapper;
15
16
use OCA\FaceRecognition\Db\Face;
17
use OCA\FaceRecognition\Db\FaceMapper;
18
19
use OCA\FaceRecognition\Db\Person;
20
use OCA\FaceRecognition\Db\PersonMapper;
21
22
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
23
24
class FileController extends Controller {
25
26
	private $config;
27
28
	private $imageMapper;
29
30
	private $personMapper;
31
32
	private $faceMapper;
33
34
	private $rootFolder;
35
36
	private $userId;
37
38
	public function __construct($AppName,
39
	                            IRequest     $request,
40
	                            IConfig      $config,
41
	                            ImageMapper  $imageMapper,
42
	                            PersonMapper $personMapper,
43
	                            FaceMapper   $faceMapper,
44
	                            IRootFolder  $rootFolder,
45
	                            $UserId)
46
	{
47
		parent::__construct($AppName, $request);
48
		$this->config = $config;
49
		$this->imageMapper = $imageMapper;
50
		$this->personMapper = $personMapper;
51
		$this->faceMapper = $faceMapper;
52
		$this->rootFolder = $rootFolder;
53
		$this->userId = $UserId;
54
	}
55
56
	/**
57
	 * @NoAdminRequired
58
	 */
59
	public function getPersonsFromPath(string $fullpath) {
60
		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
61
62
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
63
		$fileId = $userFolder->get($fullpath)->getId();
64
65
		$resp = array();
66
67
		$image = $this->imageMapper->findFromFile($this->userId, $fileId);
68
		$resp['image_id'] = $image ? $image->getId() : 0;
69
		$resp['is_processed'] = $image ? $image->getIsProcessed() : 0;
70
		$resp['error'] = $image ? $image->getError() : null;
71
72
		$aPersons = array();
73
		$persons = $this->personMapper->findFromFile($this->userId, $fileId);
74
		foreach ($persons as $person) {
75
			$face = $this->faceMapper->getPersonOnFile($this->userId, $person->getId(), $fileId, $model);
76
			if (!count($face))
77
				continue;
78
79
			$facePerson = array();
80
			$facePerson['name'] = $person->getName();
81
			$facePerson['person_id'] = $person->getId();
82
			$facePerson['face'] = $face[0];
83
84
			$aPersons[] = $facePerson;
85
		}
86
		$resp['persons'] = $aPersons;
87
88
		return new DataResponse($resp);
89
	}
90
91
}
92