FileCreateFromPath::execute()   B
last analyzed

Complexity

Conditions 9
Paths 28

Size

Total Lines 50
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 38
c 3
b 0
f 1
dl 0
loc 50
rs 7.7564
cc 9
nc 28
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 07.01.2018
6
 * Time: 8:43
7
 */
8
9
namespace floor12\files\logic;
10
11
12
use floor12\files\components\SimpleImage;
13
use floor12\files\models\FileType;
14
use yii\base\ErrorException;
15
use yii\db\ActiveRecordInterface;
16
17
class FileCreateFromPath
18
{
19
20
    private $className;
21
    private $fieldName;
22
    private $filePath;
23
    private $fileName;
24
    private $storagePath;
25
    private $model;
26
27
    public function __construct(ActiveRecordInterface $model, string $filePath, string $className, string $fieldName, string $storagePath, string $fileName = null)
28
    {
29
30
        $this->model = $model;
31
32
        if (!$filePath || !$className || !$fieldName || !$storagePath)
33
            throw new ErrorException("Empty params not allowed.");
34
35
        if (!file_exists($storagePath))
36
            throw new ErrorException("File storage not found on disk.");
37
38
        if (!file_exists($filePath))
39
            throw new ErrorException("File not found on disk.");
40
41
        if (!is_writable($storagePath))
42
            throw new ErrorException("File storage is not writable.");
43
        $this->filePath = $filePath;
44
        $this->fileName = $fileName;
45
        $this->fieldName = $fieldName;
46
        $this->className = $className;
47
        $this->storagePath = $storagePath;
48
49
    }
50
51
    /** Основная  работка
52
     * @return bool
53
     * @throws ErrorException
54
     */
55
    public function execute()
56
    {
57
        // копируем файл в хранилище
58
        $tmp_extansion = explode('?', pathinfo($this->filePath, PATHINFO_EXTENSION));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($this->filePath...gic\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of explode() 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

58
        $tmp_extansion = explode('?', /** @scrutinizer ignore-type */ pathinfo($this->filePath, PATHINFO_EXTENSION));
Loading history...
59
        $extansion = $tmp_extansion[0];
60
        $filename = new PathGenerator($this->storagePath) . "." . $extansion;
61
        $new_path = $this->storagePath . $filename;
62
        copy($this->filePath, $new_path);
63
64
        // создаем запись в базе
65
        $this->model->field = $this->fieldName;
0 ignored issues
show
Bug introduced by
Accessing field on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
66
        $this->model->class = $this->className;
0 ignored issues
show
Bug introduced by
Accessing class on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
67
        $this->model->filename = $filename;
0 ignored issues
show
Bug introduced by
Accessing filename on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
68
        if ($this->model->filename)
69
            $this->model->title = $this->model->filename;
0 ignored issues
show
Bug introduced by
Accessing title on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
70
        else
71
            $this->model->title = rand(0, 99999); #такой прикол )
72
        $this->model->content_type = $this->model->mime_content_type($new_path);
0 ignored issues
show
Bug introduced by
Accessing content_type on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
73
        $this->model->type = $this->detectType();
0 ignored issues
show
Bug introduced by
Accessing type on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
74
        $this->model->size = filesize($new_path);
0 ignored issues
show
Bug introduced by
Accessing size on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
75
        $this->model->created = time();
0 ignored issues
show
Bug introduced by
Accessing created on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
76
        if ($this->model->type == FileType::VIDEO)
77
            $this->model->video_status = 0;
0 ignored issues
show
Bug introduced by
Accessing video_status on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
78
79
        if ($this->model->save()) {
80
81
            if ($this->model->type == FileType::IMAGE) {
82
                $exif = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $exif is dead and can be removed.
Loading history...
83
                @$exif = exif_read_data($new_path);
84
                if (isset($exif['Orientation'])) {
85
                    $ort = $exif['Orientation'];
86
                    $rotatingImage = new SimpleImage();
87
                    $rotatingImage->load($new_path);
88
                    switch ($ort) {
89
90
                        case 3: // 180 rotate left
91
                            $rotatingImage->rotateDegrees(180);
92
                            break;
93
                        case 6: // 90 rotate right
94
                            $rotatingImage->rotateDegrees(270);
95
                            break;
96
                        case 8:    // 90 rotate left
97
                            $rotatingImage->rotateDegrees(90);
98
                    }
99
                    $rotatingImage->save($new_path);
100
                }
101
            }
102
            return true;
103
        }
104
        return false;
105
    }
106
107
    /**
108
     * @return integer
109
     */
110
    private function detectType()
111
    {
112
        $contentTypeArray = explode('/', $this->model->content_type);
0 ignored issues
show
Bug introduced by
Accessing content_type on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
113
        if ($contentTypeArray[0] == 'image')
114
            return FileType::IMAGE;
115
        if ($contentTypeArray[0] == 'video')
116
            return FileType::VIDEO;
117
        return FileType::FILE;
118
    }
119
}
120