Completed
Push — master ( 67a2f0...6275d0 )
by Richan
01:24
created

CacheableObserver::fireEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Suitmedia\Cacheable;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Suitmedia\Cacheable\Contracts\CacheableModel;
7
use Suitmedia\Cacheable\Events\CacheableInvalidated;
8
use Suitmedia\Cacheable\Events\CacheableInvalidating;
9
10
class CacheableObserver
11
{
12
    /**
13
     * Tell the cacheable service to flush all cache
14
     * that related to the given model.
15
     *
16
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
17
     *
18
     * @return void
19
     */
20
    protected function flushCache(CacheableModel $model)
21
    {
22
        $tags = $model->cacheTags();
23
24
        event(new CacheableInvalidating($model, $tags));
25
26
        \Cacheable::flush($tags);
27
28
        event(new CacheableInvalidated($model, $tags));
29
    }
30
31
    /**
32
     * Saved event handler.
33
     *
34
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
35
     *
36
     * @return void
37
     */
38
    public function saved(CacheableModel $model)
39
    {
40
        $this->flushCache($model);
41
    }
42
43
    /**
44
     * Deleted event handler.
45
     *
46
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
47
     *
48
     * @return void
49
     */
50
    public function deleted(CacheableModel $model)
51
    {
52
        $this->flushCache($model);
53
    }
54
55
    /**
56
     * Restored event handler.
57
     *
58
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
59
     *
60
     * @return void
61
     */
62
    public function restored(CacheableModel $model)
63
    {
64
        $this->flushCache($model);
65
    }
66
}
67