CacheableObserver::flushCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Suitmedia\Cacheable;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use Suitmedia\Cacheable\Contracts\CacheableModel;
9
use Suitmedia\Cacheable\Events\CacheableInvalidated;
10
use Suitmedia\Cacheable\Events\CacheableInvalidating;
11
12
class CacheableObserver
13
{
14
    /**
15
     * The dirty fields of all observed models.
16
     *
17
     * @var \Illuminate\Support\Collection
18
     */
19
    protected $dirtyFields;
20
21
    /**
22
     * Class constructor.
23
     *
24
     * @param \Illuminate\Support\Collection $dirtyFields
25
     */
26
    public function __construct(Collection $dirtyFields)
27
    {
28
        $this->dirtyFields = $dirtyFields;
29
    }
30
31
    /**
32
     * Tell the cacheable service to flush all cache
33
     * that related to the given model.
34
     *
35
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
36
     *
37
     * @return void
38
     */
39
    protected function flushCache(CacheableModel $model): void
40
    {
41
        $affectedFields = $this->dirtyFields->get(get_class($model));
42
        $tags = $model->cacheTags();
43
44
        event(new CacheableInvalidating($model, $tags, $affectedFields));
45
46
        app(CacheableService::class)->flush($tags);
47
48
        event(new CacheableInvalidated($model, $tags, $affectedFields));
49
    }
50
51
    /**
52
     * Saved event handler.
53
     *
54
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
55
     *
56
     * @return void
57
     */
58
    public function saved(CacheableModel $model): void
59
    {
60
        $this->flushCache($model);
61
    }
62
63
    /**
64
     * saving event handler.
65
     *
66
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
67
     *
68
     * @return void
69
     */
70
    public function saving(CacheableModel $model): void
71
    {
72
        $this->dirtyFields->put(get_class($model), $model->getDirty());
73
    }
74
75
    /**
76
     * Deleted event handler.
77
     *
78
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
79
     *
80
     * @return void
81
     */
82
    public function deleted(CacheableModel $model): void
83
    {
84
        $this->flushCache($model);
85
    }
86
87
    /**
88
     * deleting event handler.
89
     *
90
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
91
     *
92
     * @return void
93
     */
94
    public function deleting(CacheableModel $model): void
95
    {
96
        $this->dirtyFields->put(get_class($model), ['deleted_at' => Carbon::now()]);
97
    }
98
99
    /**
100
     * Restored event handler.
101
     *
102
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
103
     *
104
     * @return void
105
     */
106
    public function restored(CacheableModel $model): void
107
    {
108
        $this->flushCache($model);
109
    }
110
111
    /**
112
     * restoring event handler.
113
     *
114
     * @param \Suitmedia\Cacheable\Contracts\CacheableModel $model
115
     *
116
     * @return void
117
     */
118
    public function restoring(CacheableModel $model): void
119
    {
120
        $this->dirtyFields->put(get_class($model), ['deleted_at' => null]);
121
    }
122
}
123