FileCropRotate   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
eloc 58
c 2
b 1
f 0
dl 0
loc 107
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 6
A execute() 0 32 2
A imageCreateFromAny() 0 28 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 03.01.2018
6
 * Time: 19:37
7
 */
8
9
namespace floor12\files\logic;
10
11
12
use floor12\files\models\File;
13
use floor12\files\models\FileType;
14
use Yii;
15
use yii\base\ErrorException;
16
use yii\web\BadRequestHttpException;
17
use yii\web\NotFoundHttpException;
18
19
class FileCropRotate
20
{
21
    private $_file;
22
    private $_width;
23
    private $_height;
24
    private $_top;
25
    private $_left;
26
    private $_rotated;
27
28
29
    public function __construct(array $data)
30
    {
31
        $this->_file = File::findOne($data['id']);
32
33
        if (!$this->_file)
34
            throw new NotFoundHttpException('File not found');
35
36
        if ($this->_file->type != FileType::IMAGE)
37
            throw new BadRequestHttpException('Requested file is not an image.');
38
39
        if (!file_exists($this->_file->rootPath))
0 ignored issues
show
Bug introduced by
It seems like $this->_file->rootPath can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, 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

39
        if (!file_exists(/** @scrutinizer ignore-type */ $this->_file->rootPath))
Loading history...
40
            throw new BadRequestHttpException('File not found in file storage.');
41
42
        $this->_height = (int)$data['height'];
43
        $this->_width = (int)$data['width'];
44
        $this->_top = (int)$data['top'];
45
        $this->_left = (int)$data['left'];
46
        $this->_rotated = (int)$data['rotated'];
47
48
        if (!$this->_height && !$this->_width) {
49
            list($this->_width, $this->_height) = getimagesize($this->_file->rootPath);
0 ignored issues
show
Bug introduced by
It seems like $this->_file->rootPath can also be of type null; however, parameter $filename of getimagesize() does only seem to accept string, 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

49
            list($this->_width, $this->_height) = getimagesize(/** @scrutinizer ignore-type */ $this->_file->rootPath);
Loading history...
50
        }
51
52
53
    }
54
55
    public function execute()
56
    {
57
58
        $src = $this->imageCreateFromAny();
59
        
60
        $src = imagerotate($src, -$this->_rotated, 0);
61
62
        $dest = imagecreatetruecolor($this->_width, $this->_height);
63
64
        imagecopy($dest, $src, 0, 0, $this->_left, $this->_top, $this->_width, $this->_height);
65
66
        $newName = new PathGenerator(Yii::$app->getModule('files')->storageFullPath) . '.jpeg';
67
68
        $newPath = Yii::$app->getModule('files')->storageFullPath . '/' . $newName;
0 ignored issues
show
Bug introduced by
Are you sure Yii::app->getModule('files')->storageFullPath of type mixed|null|object can be used in concatenation? ( Ignorable by Annotation )

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

68
        $newPath = /** @scrutinizer ignore-type */ Yii::$app->getModule('files')->storageFullPath . '/' . $newName;
Loading history...
69
70
        $oldPath = $this->_file->rootPath;
71
72
        imagejpeg($dest, $newPath, 80);
73
74
        imagedestroy($dest);
75
76
        imagedestroy($src);
77
78
        $this->_file->filename = $newName;
79
        $this->_file->content_type = $this->_file->mime_content_type($newPath);
80
        $this->_file->size = filesize($newPath);
81
        $this->_file->changeHash();
82
        if ($this->_file->save()) {
83
            @unlink($oldPath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

83
            /** @scrutinizer ignore-unhandled */ @unlink($oldPath);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
It seems like $oldPath can also be of type null; however, parameter $filename of unlink() does only seem to accept string, 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

83
            @unlink(/** @scrutinizer ignore-type */ $oldPath);
Loading history...
84
            return $this->_file->href;
85
        } else
86
            throw new ErrorException("Error while saving file model.");
87
88
89
    }
90
91
92
    /**
93
     * Method to read files from any mime types
94
     * @return resource
95
     * @throws BadRequestHttpException
96
     */
97
98
    private function imageCreateFromAny()
99
    {
100
        $type = exif_imagetype($this->_file->rootPath);
0 ignored issues
show
Bug introduced by
It seems like $this->_file->rootPath can also be of type null; however, parameter $filename of exif_imagetype() does only seem to accept string, 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

100
        $type = exif_imagetype(/** @scrutinizer ignore-type */ $this->_file->rootPath);
Loading history...
101
        $allowedTypes = array(
102
            1, // [] gif
103
            2, // [] jpg
104
            3, // [] png
105
            6   // [] bmp
106
        );
107
        if (!in_array($type, $allowedTypes)) {
108
            throw new BadRequestHttpException('File must have GIF, JPG, PNG or BMP mime-type.');
109
110
        }
111
        switch ($type) {
112
            case 1 :
113
                $im = imageCreateFromGif($this->_file->rootPath);
114
                break;
115
            case 2 :
116
                $im = imageCreateFromJpeg($this->_file->rootPath);
117
                break;
118
            case 3 :
119
                $im = imageCreateFromPng($this->_file->rootPath);
120
                break;
121
            case 6 :
122
                $im = imageCreateFromBmp($this->_file->rootPath);
123
                break;
124
        }
125
        return $im;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $im does not seem to be defined for all execution paths leading up to this point.
Loading history...
126
    }
127
}