Passed
Push — drop-old-code ( e26609...a3823a )
by Matias
02:11
created

FaceController::getFaceThumb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 3
dl 0
loc 32
ccs 0
cts 25
cp 0
crap 2
rs 9.536
c 0
b 0
f 0
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
	public function __construct($AppName, IRequest $request, IRootFolder $rootFolder, FaceMapper $facemapper, FaceNewMapper $facenewmapper, ImageMapper $imagemapper, $UserId) {
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
	 * @NoCSRFRequired
41
	 */
42
	public function getThumb ($id, $size) {
43
		$face = $this->faceNewMapper->find($id);
44
		$image = $this->imageMapper->find($this->userId, $face->getImage());
0 ignored issues
show
Bug introduced by
The method getImage() does not exist on OCA\FaceRecognition\Db\FaceNew. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
		$image = $this->imageMapper->find($this->userId, $face->/** @scrutinizer ignore-call */ getImage());
Loading history...
Bug introduced by
It seems like $face->getImage() can also be of type null; however, parameter $imageId of OCA\FaceRecognition\Db\ImageMapper::find() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
		$image = $this->imageMapper->find($this->userId, /** @scrutinizer ignore-type */ $face->getImage());
Loading history...
45
		$fileId = $image->getFile();
46
47
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
48
		$nodes = $userFolder->getById($fileId);
49
		$file = $nodes[0];
50
51
		$ownerView = new \OC\Files\View('/'. $this->userId . '/files');
0 ignored issues
show
Bug introduced by
The type OC\Files\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
		$path = $userFolder->getRelativePath($file->getPath());
53
54
		$img = new \OC_Image();
0 ignored issues
show
Bug introduced by
The type OC_Image was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
		$fileName = $ownerView->getLocalFile($path);
56
		$img->loadFromFile($fileName);
57
58
		$x = $face->getLeft ();
0 ignored issues
show
Bug introduced by
The method getLeft() does not exist on OCA\FaceRecognition\Db\FaceNew. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
		/** @scrutinizer ignore-call */ 
59
  $x = $face->getLeft ();
Loading history...
59
		$y = $face->getTop ();
0 ignored issues
show
Bug introduced by
The method getTop() does not exist on OCA\FaceRecognition\Db\FaceNew. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
		/** @scrutinizer ignore-call */ 
60
  $y = $face->getTop ();
Loading history...
60
		$w = $face->getRight () - $x;
0 ignored issues
show
Bug introduced by
The method getRight() does not exist on OCA\FaceRecognition\Db\FaceNew. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
		$w = $face->/** @scrutinizer ignore-call */ getRight () - $x;
Loading history...
61
		$h = $face->getBottom () - $y;
0 ignored issues
show
Bug introduced by
The method getBottom() does not exist on OCA\FaceRecognition\Db\FaceNew. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
		$h = $face->/** @scrutinizer ignore-call */ getBottom () - $y;
Loading history...
62
63
		$padding = $h*0.25;
64
		$x -= $padding;
65
		$y -= $padding;
66
		$w += $padding*2;
67
		$h += $padding*2;
68
69
		$img->crop($x, $y, $w, $h);
70
		$img->scaleDownToFit($size, $size);
71
72
		$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]);
73
		$resp->setETag((string)crc32($img->data()));
74
		$resp->cacheFor(7 * 24 * 60 * 60);
75
		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
76
77
		return $resp;
78
	}
79
80
	/**
81
	 * @NoAdminRequired
82
	 *
83
	 * @param int $id
84
	 */
85
	public function invalidate($id) {
86
		$face = $this->faceMapper->find($id, $this->userId);
87
		$note->setDistance(1.0);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $note seems to be never defined.
Loading history...
88
		$newFace = $this->faceMapper->update($face);
89
		return new DataResponse($newFace);
90
	}
91
92
}
93