Passed
Push — master ( 02be02...16ebe1 )
by Aleksandr
04:27
created

FileUploadTrait::fileExist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace carono\yii2file;
5
6
use yii\db\ActiveRecord;
7
use yii\helpers\FileHelper;
8
9
/**
10
 * Trait FileUploadTrait
11
 *
12
 * @package carono\yii2file
13
 * @property integer $id
14
 * @property string $uid
15
 * @property integer $user_id
16
 * @property string $name
17
 * @property string $extension
18
 * @property string $folder
19
 * @property string $mime_type
20
 * @property integer $size
21
 * @property string $data
22
 * @property string $session
23
 * @property string $md5
24
 * @property string $slug
25
 * @property integer $is_active
26
 * @property integer $is_exist
27
 * @property resource $binary
28
 * @property string $created_at
29
 * @property string $updated_at
30
 *
31
 * @property string $fileName
32
 * @property string $realFileName
33
 * @property string $realFilePath
34
 *
35
 * @mixin ActiveRecord
36
 */
37
trait FileUploadTrait
38
{
39
    public $fileNameAsUid = true;
40
    public $eraseOnDelete = true;
41
    public $uploaderClass = 'carono\yii2file\Uploader';
42
    public $fileUploadFolder = '@app/files';
43
44
    public function init()
45
    {
46
        $this->on(self::EVENT_BEFORE_DELETE, [$this, 'eraseOnDelete']);
0 ignored issues
show
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...
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

46
        $this->/** @scrutinizer ignore-call */ 
47
               on(self::EVENT_BEFORE_DELETE, [$this, 'eraseOnDelete']);
Loading history...
47
        parent::init();
48
    }
49
50
    /**
51
     * @param $file
52
     * @return Uploader|mixed
53
     */
54
    public static function startUpload($file)
55
    {
56
        $model = new self();
57
        return \Yii::createObject([
58
            'class' => $model->uploaderClass,
59
            'modelClass' => self::className(),
60
            'file' => $file,
61
            'folder' => $model->fileUploadFolder
62
        ]);
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function deleteFile()
69
    {
70
        if ($this->fileExist()) {
71
            @unlink($this->getRealFilePath());
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

71
            /** @scrutinizer ignore-unhandled */ @unlink($this->getRealFilePath());

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...
72
            if ($f = !$this->fileExist()) {
73
                self::updateAttributes(["is_exist" => false]);
74
            }
75
            return $f;
76
        } else {
77
            return true;
78
        }
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getRealFileName()
85
    {
86
        if ($this->fileNameAsUid == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

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