Completed
Push — master ( 8e1256...eafa32 )
by Brent
01:41
created

MediaObserver::isSoftDeleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary;
4
5
use Spatie\MediaLibrary\Filesystem\Filesystem;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
class MediaObserver
9
{
10
    public function creating(Media $media)
11
    {
12
        $media->setHighestOrderNumber();
13
    }
14
15
    public function updating(Media $media)
16
    {
17
        if ($media->file_name !== $media->getOriginal('file_name')) {
18
            app(Filesystem::class)->renameFile($media, $media->getOriginal('file_name'));
19
        }
20
    }
21
22
    public function updated(Media $media)
23
    {
24
        if (is_null($media->getOriginal('model_id'))) {
25
            return;
26
        }
27
28
        if ($media->manipulations !== json_decode($media->getOriginal('manipulations'))) {
29
            app(FileManipulator::class)->createDerivedFiles($media);
30
        }
31
    }
32
33
    public function deleted(Media $media)
34
    {
35
        $softDeleted = false;
36
        
37
        if (in_array(SoftDeletes::class, trait_uses_recursive($media))) {
38
            $softDeleted = $this->isSoftDeleted($media);
39
        }
40
        
41
        if(!$softDeleted) {
42
            app(Filesystem::class)->removeFiles($media);
43
        }
44
    }
45
46
    protected function isSoftDeleted(Media $media)
47
    {
48
        return $media->isDirty($media->getDeletedAtColumn());
49
    }
50
}
51