Passed
Push — master ( 2173e1...800b2f )
by Mostafa
05:03 queued 02:26
created

LaraCacheObserver::created()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Mostafaznv\LaraCache\Observers;
4
5
use Mostafaznv\LaraCache\Cache;
6
7
final class LaraCacheObserver
8
{
9
    public function created(mixed $model): void
10
    {
11
        $model->cache()->refresh($model, Cache::$created);
12
    }
13
14
    public function updated(mixed $model): void
15
    {
16
        if (!$this->isRestored($model)) {
17
            $model->cache()->refresh($model, Cache::$updated);
18
        }
19
    }
20
21
    public function deleted(mixed $model): void
22
    {
23
        $model->cache()->refresh($model, Cache::$deleted);
24
    }
25
26
    public function restored(mixed $model): void
27
    {
28
        $model->cache()->refresh($model, Cache::$restored);
29
    }
30
31
    private function isRestored(mixed $model): bool
32
    {
33
        return $model->wasChanged('deleted_at')
34
            and is_null($model->deleted_at)
35
            and !$model->originalIsEquivalent('deleted_at');
36
    }
37
}
38