Passed
Push — misc-fixes-v2 ( 4a9410...e904c6 )
by Matias
01:56
created

FaceController::hipsterize()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 75
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 53
nc 7
nop 2
dl 0
loc 75
ccs 0
cts 57
cp 0
crap 30
rs 8.7143
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace OCA\FaceRecognition\Controller;
3
4
use OCP\Image as OCP_Image;
5
6
use OCP\IRequest;
7
use OCP\Files\IRootFolder;
8
use OCP\AppFramework\Http;
9
use OCP\AppFramework\Http\DataResponse;
10
use OCP\AppFramework\Http\JSONResponse;
11
use OCP\AppFramework\Http\DataDisplayResponse;
12
use OCP\AppFramework\Controller;
13
14
use OCA\FaceRecognition\Db\Face;
15
use OCA\FaceRecognition\Db\FaceMapper;
16
17
use OCA\FaceRecognition\Db\Image;
18
use OCA\FaceRecognition\Db\ImageMapper;
19
20
class FaceController extends Controller {
21
22
	private $rootFolder;
23
	private $faceMapper;
24
	private $imageMapper;
25
	private $userId;
26
27
	public function __construct($AppName,
28
	                            IRequest    $request,
29
	                            IRootFolder $rootFolder,
30
	                            FaceMapper  $faceMapper,
31
	                            ImageMapper $imageMapper,
32
	                            $UserId)
33
	{
34
		parent::__construct($AppName, $request);
35
		$this->rootFolder = $rootFolder;
36
		$this->faceMapper = $faceMapper;
37
		$this->imageMapper = $imageMapper;
38
		$this->userId = $UserId;
39
	}
40
41
	/**
42
	 * @NoAdminRequired
43
	 * @NoCSRFRequired
44
	 */
45
	public function getThumb ($id, $size) {
46
		$face = $this->faceMapper->find($id);
47
		$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

47
		$image = $this->imageMapper->find($this->userId, /** @scrutinizer ignore-type */ $face->getImage());
Loading history...
48
		$fileId = $image->getFile();
49
50
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
51
		$nodes = $userFolder->getById($fileId);
52
		$file = $nodes[0];
53
54
		$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...
55
		$path = $userFolder->getRelativePath($file->getPath());
56
57
		$img = new OCP_Image();
58
		$fileName = $ownerView->getLocalFile($path);
59
		$img->loadFromFile($fileName);
60
		$img->fixOrientation();
61
62
		$x = $face->getLeft ();
63
		$y = $face->getTop ();
64
		$w = $face->getRight () - $x;
65
		$h = $face->getBottom () - $y;
66
67
		$padding = $h*0.25;
68
		$x -= $padding;
69
		$y -= $padding;
70
		$w += $padding*2;
71
		$h += $padding*2;
72
73
		$img->crop($x, $y, $w, $h);
74
		$img->scaleDownToFit($size, $size);
75
76
		$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]);
77
		$resp->setETag((string)crc32($img->data()));
78
		$resp->cacheFor(7 * 24 * 60 * 60);
79
		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
80
81
		return $resp;
82
	}
83
84
}
85