Completed
Pull Request — master (#14)
by Richan
01:10
created

VarnishableObserver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 2
dl 0
loc 108
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A deleted() 0 4 1
A handleModelInitialization() 0 8 2
A handleModelUpdates() 0 4 1
A restored() 0 4 1
A retrieved() 0 4 1
A saved() 0 4 1
A wakeup() 0 4 1
1
<?php
2
3
namespace RichanFongdasen\Varnishable;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
use RichanFongdasen\Varnishable\Events\ModelHasUpdated;
8
9
class VarnishableObserver
10
{
11
    /**
12
     * Varnishable Service Object.
13
     *
14
     * @var \RichanFongdasen\Varnishable\VarnishableService
15
     */
16
    protected $varnishable;
17
18
    /**
19
     * Varnishable Observer constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->varnishable = app(VarnishableService::class);
24
    }
25
26
    /**
27
     * Listening to any saved events.
28
     *
29
     * @param \Illuminate\Database\Eloquent\Model $model
30
     *
31
     * @return void
32
     */
33
    public function deleted(Model $model) :void
34
    {
35
        $this->handleModelUpdates($model);
36
    }
37
38
    /**
39
     * Handle any retrieved and wakeup events on
40
     * the observed models.
41
     *
42
     * @param \Illuminate\Database\Eloquent\Model $model
43
     *
44
     * @throws Exception
45
     *
46
     * @return void
47
     */
48
    protected function handleModelInitialization(Model $model) :void
49
    {
50
        $updatedAt = $model->getAttribute('updated_at');
51
52
        if ($updatedAt !== null) {
53
            $this->varnishable->setLastModifiedHeader($updatedAt);
54
        }
55
    }
56
57
    /**
58
     * Handle any update events on the observed models.
59
     *
60
     * @param \Illuminate\Database\Eloquent\Model $model
61
     *
62
     * @return void
63
     */
64
    protected function handleModelUpdates(Model $model) :void
65
    {
66
        event(new ModelHasUpdated($model));
67
    }
68
69
    /**
70
     * Listening to any saved events.
71
     *
72
     * @param \Illuminate\Database\Eloquent\Model $model
73
     *
74
     * @return void
75
     */
76
    public function restored(Model $model) :void
77
    {
78
        $this->handleModelUpdates($model);
79
    }
80
81
    /**
82
     * Listening to any retrieved events.
83
     *
84
     * @param \Illuminate\Database\Eloquent\Model $model
85
     *
86
     * @return void
87
     */
88
    public function retrieved(Model $model) :void
89
    {
90
        $this->handleModelInitialization($model);
91
    }
92
93
    /**
94
     * Listening to any saved events.
95
     *
96
     * @param \Illuminate\Database\Eloquent\Model $model
97
     *
98
     * @return void
99
     */
100
    public function saved(Model $model) :void
101
    {
102
        $this->handleModelUpdates($model);
103
    }
104
105
    /**
106
     * Listening to any wakeup events.
107
     *
108
     * @param \Illuminate\Database\Eloquent\Model $model
109
     *
110
     * @return void
111
     */
112
    public function wakeup(Model $model) :void
113
    {
114
        $this->handleModelInitialization($model);
115
    }
116
}
117