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

MediaObserver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 43
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A creating() 0 4 1
A updating() 0 6 2
A updated() 0 10 3
A deleted() 0 12 3
A isSoftDeleted() 0 4 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