Completed
Pull Request — master (#49)
by Matias
02:08
created

FileController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 24.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 13
loc 53
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A getPersonsFromPath() 0 22 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\FaceNew;
14
use OCA\FaceRecognition\Db\FaceNewMapper;
15
16
use OCA\FaceRecognition\Db\Person;
17
use OCA\FaceRecognition\Db\PersonMapper;
18
19
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
20
21
class FileController extends Controller {
22
23
	private $config;
24
25
	private $personMapper;
26
27
	private $faceNewMapper;
28
29
	private $rootFolder;
30
31
	private $userId;
32
33 View Code Duplication
	public function __construct($AppName, IRequest $request, IConfig $config,
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
	                            PersonMapper $personmapper,
35
	                            FaceNewMapper $facenewmapper,
36
	                            IRootFolder $rootFolder,
37
	                            $UserId)
38
	{
39
		parent::__construct($AppName, $request);
40
		$this->config = $config;
41
		$this->personMapper = $personmapper;
42
		$this->faceNewMapper = $facenewmapper;
43
		$this->rootFolder = $rootFolder;
44
		$this->userId = $UserId;
45
	}
46
47
	/**
48
	 * @NoAdminRequired
49
	 */
50
	public function getPersonsFromPath(string $fullpath) {
51
		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
52
53
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
54
		$fileId = $userFolder->get($fullpath)->getId();
55
56
		$resp = array();
57
		$persons = $this->personMapper->findFromFile($this->userId, $fileId);
58
		foreach ($persons as $person) {
59
			$face = $this->faceNewMapper->getPersonOnFile($this->userId, $person->getId(), $fileId, $model);
60
			if (!count($face))
61
				continue;
62
63
			$facePerson = array();
64
			$facePerson['name'] = $person->getName();
65
			$facePerson['person_id'] = $person->getId();
66
			$facePerson['face'] = $face[0];
67
68
			$resp[] = $facePerson;
69
		}
70
		return new DataResponse($resp);
71
	}
72
73
}
74