Passed
Push — 2.x ( c00759...e3c99f )
by Quentin
07:55
created

File::scopeUnused()   A

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 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use FileService;
0 ignored issues
show
Bug introduced by
The type FileService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\DB;
7
8
class File extends Model
9
{
10
    public $timestamps = true;
11
12
    protected $fillable = [
13
        'uuid',
14
        'filename',
15
        'size',
16
    ];
17
18 3
    public function getSizeAttribute($value)
19
    {
20 3
        return bytesToHuman($value);
21
    }
22
23 3
    public function canDeleteSafely()
24
    {
25 3
        return DB::table(config('twill.fileables_table', 'twill_fileables'))->where('file_id', $this->id)->count() === 0;
26
    }
27
28
    public function scopeUnused ($query)
29
    {
30
        $usedIds = DB::table(config('twill.fileables_table'))->get()->pluck('file_id');
31
        return $query->whereNotIn('id', $usedIds->toArray())->get();
32
    }
33
34 3
    public function toCmsArray()
35
    {
36
        return [
37 3
            'id' => $this->id,
38 3
            'name' => $this->filename,
0 ignored issues
show
Bug Best Practice introduced by
The property filename does not exist on A17\Twill\Models\File. Since you implemented __get, consider adding a @property annotation.
Loading history...
39 3
            'src' => FileService::getUrl($this->uuid),
0 ignored issues
show
Bug Best Practice introduced by
The property uuid does not exist on A17\Twill\Models\File. Since you implemented __get, consider adding a @property annotation.
Loading history...
40 3
            'original' => FileService::getUrl($this->uuid),
41 3
            'size' => $this->size,
0 ignored issues
show
Bug Best Practice introduced by
The property size does not exist on A17\Twill\Models\File. Since you implemented __get, consider adding a @property annotation.
Loading history...
42 3
            'filesizeInMb' => number_format($this->attributes['size'] / 1048576, 2),
43
        ];
44
    }
45
46 49
    public function getTable()
47
    {
48 49
        return config('twill.files_table', 'twill_files');
49
    }
50
}
51