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

FaceController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 146
Duplicated Lines 14.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 21
loc 146
ccs 0
cts 80
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A __construct() 8 8 1
A find() 0 4 1
A getFaceThumb() 0 33 1
A random() 0 4 1
A findFile() 0 6 1
A updateName() 7 7 1
A invalidate() 6 6 1
A getThumb() 0 10 1
A getThumbV2() 0 9 1

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\Files\IRootFolder;
6
use OCP\AppFramework\Http;
7
use OCP\AppFramework\Http\DataResponse;
8
use OCP\AppFramework\Http\JSONResponse;
9
use OCP\AppFramework\Http\DataDisplayResponse;
10
use OCP\AppFramework\Controller;
11
12
use OCA\FaceRecognition\Db\Face;
13
use OCA\FaceRecognition\Db\FaceMapper;
14
15
use OCA\FaceRecognition\Db\FaceNew;
16
use OCA\FaceRecognition\Db\FaceNewMapper;
17
18
use OCA\FaceRecognition\Db\Image;
19
use OCA\FaceRecognition\Db\ImageMapper;
20
21
class FaceController extends Controller {
22
23
	private $rootFolder;
24
	private $faceMapper;
25
	private $faceNewMapper;
26
	private $imageMapper;
27
	private $userId;
28
29 View Code Duplication
	public function __construct($AppName, IRequest $request, IRootFolder $rootFolder, FaceMapper $facemapper, FaceNewMapper $facenewmapper, ImageMapper $imagemapper, $UserId) {
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...
30
		parent::__construct($AppName, $request);
31
		$this->rootFolder = $rootFolder;
32
		$this->faceMapper = $facemapper;
33
		$this->faceNewMapper = $facenewmapper;
34
		$this->imageMapper = $imagemapper;
35
		$this->userId = $UserId;
36
	}
37
38
	/**
39
	 * @NoAdminRequired
40
	 */
41
	 public function index() {
42
		$faces = $this->faceMapper->findAll($this->userId);
43
		return new DataResponse($faces);
44
	}
45
46
	/**
47
	 * @NoAdminRequired
48
	 *
49
	 * @param int $id
50
	 */
51
	public function find ($id) {
52
		$face = $this->faceMapper->find($this->userId, $id);
53
		return new DataResponse($face);
54
	}
55
56
	/**
57
	 * @NoAdminRequired
58
	 * @NoCSRFRequired
59
	 */
60
	public function getThumb ($id) {
61
		\OC_Util::tearDownFS();
62
		\OC_Util::setupFS($this->userId);
63
64
		$face = $this->faceMapper->find($id, $this->userId);
65
66
		$fileId = $face->getFile();
67
68
		return $this->getFaceThumb ($fileId, $face);
69
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 * @NoCSRFRequired
74
	 */
75
	public function getThumbV2 ($id) {
76
		\OC_Util::tearDownFS();
77
		\OC_Util::setupFS($this->userId);
78
79
		$face = $this->faceNewMapper->find($id);
80
		$image = $this->imageMapper->find($this->userId, $face->getImage());
81
		$fileId = $image->getFile();
82
		return $this->getFaceThumb ($fileId, $face);
83
	}
84
85
	private function getFaceThumb ($fileId, $face) {
86
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
87
		$nodes = $userFolder->getById($fileId);
88
		$file = $nodes[0];
89
90
		$ownerView = new \OC\Files\View('/'. $this->userId . '/files');
91
		$path = $userFolder->getRelativePath($file->getPath());
92
93
		$img = new \OC_Image();
94
		$fileName = $ownerView->getLocalFile($path);
95
		$img->loadFromFile($fileName);
96
97
		$x = $face->getLeft ();
98
		$y = $face->getTop ();
99
		$w = $face->getRight () - $x;
100
		$h = $face->getBottom () - $y;
101
102
		$padding = $h*0.25;
103
		$x -= $padding;
104
		$y -= $padding;
105
		$w += $padding*2;
106
		$h += $padding*2;
107
108
		$img->crop($x, $y, $w, $h);
109
		$img->scaleDownToFit(32, 32);
110
111
		$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]);
112
		$resp->setETag((string)crc32($img->data()));
113
		$resp->cacheFor(7 * 24 * 60 * 60);
114
		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
115
116
		return $resp;
117
	}
118
119
	/**
120
	 * @NoAdminRequired
121
	 *
122
	 */
123
	public function random () {
124
		$faces = $this->faceMapper->findRandom($this->userId);
125
		return new DataResponse($faces);
126
	}
127
128
	/**
129
	 * @NoAdminRequired
130
	 *
131
	 * @param string $fullpath
132
	 */
133
	public function findFile ($fullpath) {
134
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
135
		$fileId = $userFolder->get($fullpath)->getId();
136
		$faces = $this->faceMapper->findFile($this->userId, $fileId);
137
		return new DataResponse($faces);
138
	}
139
140
	/**
141
	 * @NoAdminRequired
142
	 *
143
	 * @param int $id
144
	 * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
145
	 */
146 View Code Duplication
	public function updateName ($id, $newName) {
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...
147
		$face = $this->faceMapper->find($id, $this->userId);
148
		$face->setName($newName);
149
		$face->setDistance(0.0);
150
		$newFace = $this->faceMapper->update($face);
151
		return new DataResponse($newFace);
152
	}
153
154
	/**
155
	 * @NoAdminRequired
156
	 *
157
	 * @param int $id
158
	 */
159 View Code Duplication
	public function invalidate($id) {
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...
160
		$face = $this->faceMapper->find($id, $this->userId);
161
		$note->setDistance(1.0);
0 ignored issues
show
Bug introduced by
The variable $note does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
162
		$newFace = $this->faceMapper->update($face);
163
		return new DataResponse($newFace);
164
	}
165
166
}
167