UploadModelTrait::upload()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace RazonYang\Yii2\Uploader;
3
4
use yii\di\Instance;
5
use yii\web\UploadedFile;
6
use Yii;
7
8
/**
9
 * UploadModelTrait defines an universal upload logic.
10
 */
11
trait UploadModelTrait
12
{
13
    /**
14
     * @var UploadedFile
15
     */
16
    public $file;
17
18
    /**
19
     * @var UploaderInterface
20
     */
21
    public $uploader = 'uploader';
22
23
    /**
24
     * @var string
25
     */
26
    public $tooBig;
27
28
    /**
29
     * @var string
30
     */
31
    public $tooSmall;
32
33 5
    public function init()
34
    {
35 5
        parent::init();
36
37 5
        if ($this->tooBig === null) {
38 5
            $this->tooBig = Yii::t('yii', 'The file "{file}" is too big. Its size cannot exceed {formattedLimit}.');
39
        }
40 5
        if ($this->tooSmall === null) {
41 5
            $this->tooSmall = Yii::t('yii', 'The file "{file}" is too small. Its size cannot be smaller than {formattedLimit}.');
42
        }
43
44 5
        $this->uploader = Instance::ensure($this->uploader, UploaderInterface::class);
45 5
    }
46
47
    /**
48
     * @var array a list of file name extensions that are allowed to be uploaded.
49
     */
50
    private $extensions = [];
51
52
    /**
53
     * Returns allowed extensions.
54
     *
55
     * @return array
56
     */
57 4
    public function getExtensions(): array
58
    {
59 4
        return $this->extensions;
60
    }
61
62
    /**
63
     * Sets allowed extensions.
64
     *
65
     * @param string|array $extensions
66
     */
67 4
    public function setExtensions($extensions)
68
    {
69 4
        $this->extensions = is_array($extensions) ? $extensions : explode(',', $extensions);
70 4
    }
71
72
    /**
73
     * @var int the minimum number of bytes required for the uploaded file.
74
     */
75
    public $minSize;
76
77
    /**
78
     * @var int the maximum number of bytes required for the uploaded file.
79
     */
80
    public $maxSize;
81
82
    /**
83
     * @var array|string a list of file MIME types that are allowed to be uploaded.
84
     */
85
    public $mimeTypes;
86
87
    /**
88
     * @var bool whether to check file type (extension) with mime-type. If extension produced by
89
     * file mime-type check differs from uploaded file extension, the file will be considered as invalid.
90
     */
91
    public $checkExtensionByMimeType = true;
92
93 1
    public function rules()
94
    {
95
        return [
96 1
            [['file'], 'required'],
97
            [
98 1
                'file',
99 1
                'file',
100 1
                'extensions' => $this->extensions,
101 1
                'mimeTypes' => $this->mimeTypes,
102 1
                'checkExtensionByMimeType' => $this->checkExtensionByMimeType,
103 1
                'minSize' => $this->minSize,
104 1
                'maxSize' => $this->maxSize,
105 1
                'tooBig' => $this->tooBig,
106 1
                'tooSmall' => $this->tooSmall,
107
            ],
108
        ];
109
    }
110
111
    /**
112
     * Returns the filename.
113
     *
114
     * @return string
115
     */
116
    protected function getFilename(): string
117
    {
118
        $filename = md5(uniqid('', true));
119
        $extension = $this->file->getExtension();
120
        if ($extension) {
121
            $filename .= '.' . $extension;
122
        }
123
124
        $paths = [
125
            date('Ymd'),
126
            $filename
127
        ];
128
        
129
        return implode('/', array_filter($paths));
130
    }
131
132
    /**
133
     * Uploads file and returns the URL, it is your responsibility to validate before uploading.
134
     *
135
     * @return string
136
     */
137
    protected function upload(): string
138
    {
139
        $filename = $this->getFilename();
140
        return $this->uploader->saveFile($filename, $this->file->tempName);
141
    }
142
}
143