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

FaceController::invalidate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
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\FaceNew;
13
use OCA\FaceRecognition\Db\FaceNewMapper;
14
15
use OCA\FaceRecognition\Db\Image;
16
use OCA\FaceRecognition\Db\ImageMapper;
17
18
class FaceController extends Controller {
19
20
	private $rootFolder;
21
	private $faceNewMapper;
22
	private $imageMapper;
23
	private $userId;
24
25
	public function __construct($AppName,
26
	                            IRequest      $request,
27
	                            IRootFolder   $rootFolder,
28
	                            FaceNewMapper $facenewmapper,
29
	                            ImageMapper   $imagemapper,
30
	                            $UserId)
31
	{
32
		parent::__construct($AppName, $request);
33
		$this->rootFolder = $rootFolder;
34
		$this->faceNewMapper = $facenewmapper;
35
		$this->imageMapper = $imagemapper;
36
		$this->userId = $UserId;
37
	}
38
39
	/**
40
	 * @NoAdminRequired
41
	 * @NoCSRFRequired
42
	 */
43
	public function getThumb ($id, $size) {
44
		$face = $this->faceNewMapper->find($id);
45
		$image = $this->imageMapper->find($this->userId, $face->getImage());
0 ignored issues
show
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

45
		$image = $this->imageMapper->find($this->userId, /** @scrutinizer ignore-type */ $face->getImage());
Loading history...
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

45
		$image = $this->imageMapper->find($this->userId, $face->/** @scrutinizer ignore-call */ getImage());
Loading history...
46
		$fileId = $image->getFile();
47
48
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
49
		$nodes = $userFolder->getById($fileId);
50
		$file = $nodes[0];
51
52
		$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...
53
		$path = $userFolder->getRelativePath($file->getPath());
54
55
		$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...
56
		$fileName = $ownerView->getLocalFile($path);
57
		$img->loadFromFile($fileName);
58
59
		$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

59
		/** @scrutinizer ignore-call */ 
60
  $x = $face->getLeft ();
Loading history...
60
		$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

60
		/** @scrutinizer ignore-call */ 
61
  $y = $face->getTop ();
Loading history...
61
		$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

61
		$w = $face->/** @scrutinizer ignore-call */ getRight () - $x;
Loading history...
62
		$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

62
		$h = $face->/** @scrutinizer ignore-call */ getBottom () - $y;
Loading history...
63
64
		$padding = $h*0.25;
65
		$x -= $padding;
66
		$y -= $padding;
67
		$w += $padding*2;
68
		$h += $padding*2;
69
70
		$img->crop($x, $y, $w, $h);
71
		$img->scaleDownToFit($size, $size);
72
73
		$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]);
74
		$resp->setETag((string)crc32($img->data()));
75
		$resp->cacheFor(7 * 24 * 60 * 60);
76
		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
77
78
		return $resp;
79
	}
80
81
}
82