FileUploadTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getRealFilePath() 0 4 1
A getRealFileName() 0 7 2
A getFileName() 0 3 1
A startUpload() 0 8 1
A fileExist() 0 3 1
A deleteFile() 0 11 3
A init() 0 4 1
A isImage() 0 7 3
A eraseOnDelete() 0 4 3
1
<?php
2
3
4
namespace carono\yii2file;
5
6
use yii\db\ActiveRecord;
7
use yii\helpers\FileHelper;
8
use carono\yii2file\Uploader;
9
10
/**
11
 * Trait FileUploadTrait
12
 *
13
 * @package carono\yii2file
14
 * @property integer $id
15
 * @property string $uid
16
 * @property integer $user_id
17
 * @property string $name
18
 * @property string $extension
19
 * @property string $folder
20
 * @property string $mime_type
21
 * @property integer $size
22
 * @property string $data
23
 * @property string $session
24
 * @property string $md5
25
 * @property string $slug
26
 * @property integer $is_active
27
 * @property integer $is_exist
28
 * @property resource $binary
29
 * @property string $created_at
30
 * @property string $updated_at
31
 *
32
 * @property string $fileName
33
 * @property string $realFileName
34
 * @property string $realFilePath
35
 *
36
 * @mixin ActiveRecord
37
 */
38
trait FileUploadTrait
39
{
40
    public $fileNameAsUid = true;
41
    public $eraseOnDelete = true;
42
    public $uploaderClass = Uploader::class;
43
    public $fileUploadFolder = '@app/files';
44
45
    public function init()
46
    {
47
        $this->on(self::EVENT_BEFORE_DELETE, [$this, 'eraseOnDelete']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
        $this->/** @scrutinizer ignore-call */ 
48
               on(self::EVENT_BEFORE_DELETE, [$this, 'eraseOnDelete']);
Loading history...
Bug introduced by
The constant carono\yii2file\FileUplo...it::EVENT_BEFORE_DELETE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48
        parent::init();
49
    }
50
51
    /**
52
     * @param $file
53
     * @return Uploader|mixed
54
     */
55
    public static function startUpload($file)
56
    {
57
        $model = new static();
58
        return \Yii::createObject([
59
            'class' => $model->uploaderClass,
60
            'modelClass' => static::class,
61
            'file' => $file,
62
            'folder' => $model->fileUploadFolder,
63
        ]);
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function deleteFile()
70
    {
71
        if ($this->fileExist()) {
72
            unlink($this->getRealFilePath());
73
            if ($f = !$this->fileExist()) {
74
                $this->updateAttributes(['is_exist' => false]);
0 ignored issues
show
Bug introduced by
It seems like updateAttributes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

74
                $this->/** @scrutinizer ignore-call */ 
75
                       updateAttributes(['is_exist' => false]);
Loading history...
75
            }
76
            return $f;
77
        }
78
79
        return true;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getRealFileName()
86
    {
87
        if ($this->fileNameAsUid) {
88
            return $this->uid . '.' . $this->extension;
89
        }
90
91
        return $this->name . '.' . $this->extension;
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97
    public function getRealFilePath()
98
    {
99
        $path = \Yii::getAlias($this->folder) . DIRECTORY_SEPARATOR . $this->getRealFileName();
100
        return str_replace('/', DIRECTORY_SEPARATOR, $path);
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    public function fileExist()
107
    {
108
        return file_exists($this->getRealFilePath());
109
    }
110
111
    /**
112
     * @return bool|null
113
     */
114
    public function isImage()
115
    {
116
        if (($mime = $this->mime_type) || ($mime = FileHelper::getMimeType($this->getRealFilePath()))) {
117
            return strpos($mime, 'image') === 0;
118
        }
119
120
        return null;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getFileName()
127
    {
128
        return implode('.', array_filter([$this->name, $this->extension]));
129
    }
130
131
    public function eraseOnDelete()
132
    {
133
        if ($this->eraseOnDelete && $this->fileExist()) {
134
            $this->deleteFile();
135
        }
136
    }
137
}