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

LaraCacheObserver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 9
c 1
b 0
f 0
dl 0
loc 29
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updated() 0 4 2
A created() 0 3 1
A deleted() 0 3 1
A isRestored() 0 5 3
A restored() 0 3 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