Passed
Push — master ( b8999c...a2f941 )
by Curtis
09:12 queued 03:15
created

Video   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 5 1
A tags() 0 7 1
A store() 0 11 2
A syncTags() 0 3 1
A tagList() 0 3 1
A poster() 0 3 1
1
<?php
2
3
namespace App\Models\enso\howto;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Facades\DB;
8
use App\Contracts\enso\files\Attachable;
9
use App\Traits\enso\files\HasFile;
10
use LaravelEnso\Helpers\Traits\CascadesMorphMap;
11
use LaravelEnso\HowTo\Exceptions\Video as Exception;
12
13
class Video extends Model implements Attachable
14
{
15
    use CascadesMorphMap, HasFile;
0 ignored issues
show
introduced by
The trait App\Traits\enso\files\HasFile requires some properties which are not provided by App\Models\enso\howto\Video: $optimizeImages, $resizeImages, $saved_name, $mimeTypes, $extensions, $file
Loading history...
16
17
    protected $table = 'how_to_videos';
18
19
    protected $guarded = ['id'];
20
21
    protected string $folder = 'howToVideos';
22
23
    public function poster()
24
    {
25
        return $this->hasOne(Poster::class);
26
    }
27
28
    public function tags()
29
    {
30
        return $this->belongsToMany(
31
            Tag::class,
32
            'how_to_tag_how_to_video',
33
            'how_to_video_id',
34
            'how_to_tag_id'
35
        );
36
    }
37
38
    public function store(UploadedFile $file, array $attributes)
39
    {
40
        if (self::whereHas('file', fn ($query) => $query
41
            ->whereOriginalName($file->getClientOriginalName()))->exists()) {
42
            throw Exception::exists();
43
        }
44
45
        return DB::transaction(function () use ($file, $attributes) {
46
            $video = $this->create($attributes);
47
48
            return tap($video)->upload($file);
49
        });
50
    }
51
52
    public function syncTags($tagList)
53
    {
54
        $this->tags()->sync($tagList);
55
    }
56
57
    public function tagList()
58
    {
59
        return $this->tags()->pluck('id');
60
    }
61
62
    public function delete()
63
    {
64
        optional($this->poster)->delete();
65
66
        parent::delete();
67
    }
68
}
69