Passed
Push — master ( c37b7a...d57880 )
by Youchen
05:17
created

File::uploadable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace UniSharp\Uploadable;
4
5
use Illuminate\Support\Facades\URL;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Database\Eloquent\Relations\MorphTo;
9
10
class File extends Model
11
{
12
    protected $fillable = ['name', 'mime', 'size', 'path', 'extra'];
13
14 8
    public static function boot(): void
15
    {
16 4
        parent::boot();
17
18 8
        static::deleted(function ($model) {
19 4
            Storage::delete($model->attributes['path']);
20 8
        });
21 4
    }
22
23 20
    public function uploadable(): MorphTo
24
    {
25 20
        return $this->morphTo();
26
    }
27
28 2
    public function getPathAttribute(): ?string
29
    {
30 2
        if (!array_key_exists('path', $this->attributes)) {
31
            return null;
32
        }
33
34 2
        return URL::to($this->attributes['path']);
35
    }
36
}
37