MediaObserver::deleted()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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