Completed
Pull Request — master (#19)
by Şəhriyar
33:48
created

File   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 3
c 3
b 1
f 1
lcom 0
cbo 1
dl 0
loc 31
ccs 0
cts 0
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeOfType() 0 8 2
A uploader() 0 4 1
1
<?php namespace App\Models;
2
3
/**
4
 * App\Models\File
5
 *
6
 * @property integer $id
7
 * @property string $hash
8
 * @property string $mime
9
 * @property integer $size
10
 * @property string $metadata
11
 * @property \Carbon\Carbon $created_at
12
 * @property \Carbon\Carbon $updated_at
13
 * @property \Carbon\Carbon $deleted_at
14
 * @method static \Illuminate\Database\Query\Builder|\App\Models\File whereId($value)
15
 * @method static \Illuminate\Database\Query\Builder|\App\Models\File whereHash($value)
16
 * @method static \Illuminate\Database\Query\Builder|\App\Models\File whereMime($value)
17
 * @method static \Illuminate\Database\Query\Builder|\App\Models\File whereSize($value)
18
 * @method static \Illuminate\Database\Query\Builder|\App\Models\File whereMetadata($value)
19
 * @method static \Illuminate\Database\Query\Builder|\App\Models\File whereCreatedAt($value)
20
 * @method static \Illuminate\Database\Query\Builder|\App\Models\File whereUpdatedAt($value)
21
 * @method static \Illuminate\Database\Query\Builder|\App\Models\File whereDeletedAt($value)
22
 * @method static \App\Models\File ofType($type = 'image')
23
 */
24
class File extends \Eloquent
25
{
26
    protected $table = 'files';
27
28
    protected $primaryKey = 'hash';
29
30
    protected $guarded = ['*'];
31
32
    protected $dates = ['deleted_at'];
33
34
    /**
35
     * @param \Illuminate\Database\Query\Builder $query
36
     * @param string                             $type
37
     *
38
     * @return \Illuminate\Database\Query\Builder
39
     * @throws \Exception
40
     */
41
    public function scopeOfType($query, $type = 'image')
42
    {
43
        if (!in_array($type, ['plain', 'image', 'audio', 'video', 'application'])) {
44
            throw new \Exception();
45
        }
46
47
        return $query->where('mime', 'like', $type . '/%');
48
    }
49
50
    public function uploader()
51
    {
52
        return $this->belongsToMany(User::class, 'files_users', 'file_hash', 'user_id')->withTimestamps()->withPivot(['uuid', 'original_client_name', 'original_client_extension']);
53
    }
54
}
55