1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codefocus\ManagedCache; |
4
|
|
|
|
5
|
|
|
use Codefocus\ManagedCache\Events\Event; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Iterator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ConditionBuilder. |
12
|
|
|
*/ |
13
|
|
|
class ConditionBuilder implements Iterator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $conditions = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Adds a Condition that tags a cache to get invalidated |
22
|
|
|
* when a new Model of the specified class is created. |
23
|
|
|
* |
24
|
|
|
* @param string $modelClassName model class name |
25
|
|
|
* |
26
|
|
|
* @return self |
27
|
|
|
*/ |
28
|
|
|
public function modelCreated(string $modelClassName): self |
29
|
|
|
{ |
30
|
|
|
$this->conditions[] = new Condition( |
31
|
|
|
Event::EVENT_ELOQUENT_CREATED, |
32
|
|
|
$modelClassName |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Adds a Condition that tags a cache to get invalidated |
40
|
|
|
* when the specified Model instance, or any Model of the specified class |
41
|
|
|
* is updated. |
42
|
|
|
* |
43
|
|
|
* @param mixed $model model instance or class name |
44
|
|
|
* @param int|null $modelId (default: null) The Model id |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function modelUpdated($model, ?int $modelId = null): self |
49
|
|
|
{ |
50
|
|
|
if ($this->isModel($model)) { |
|
|
|
|
51
|
|
|
$modelClassName = get_class($model); |
52
|
|
|
$modelId = $model->getKey(); |
53
|
|
|
} else { |
54
|
|
|
$modelClassName = $model; |
55
|
|
|
} |
56
|
|
|
$this->conditions[] = new Condition( |
57
|
|
|
Event::EVENT_ELOQUENT_UPDATED, |
58
|
|
|
$modelClassName, |
59
|
|
|
$modelId |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Adds a Condition that tags a cache to get invalidated |
67
|
|
|
* when the specified Model instance, or any Model of the specified class |
68
|
|
|
* is saved. |
69
|
|
|
* |
70
|
|
|
* @param mixed $model model instance or class name |
71
|
|
|
* @param int|null $modelId (default: null) The Model id |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
|
|
public function modelSaved($model, ?int $modelId = null): self |
76
|
|
|
{ |
77
|
|
|
if ($this->isModel($model)) { |
78
|
|
|
$modelClassName = get_class($model); |
79
|
|
|
$modelId = $model->getKey(); |
80
|
|
|
} else { |
81
|
|
|
$modelClassName = $model; |
82
|
|
|
} |
83
|
|
|
$this->confitions[] = new Condition( |
|
|
|
|
84
|
|
|
Event::EVENT_ELOQUENT_SAVED, |
85
|
|
|
$modelClassName, |
86
|
|
|
$modelId |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Adds a Condition that tags a cache to get invalidated |
94
|
|
|
* when the specified Model instance, or any Model of the specified class |
95
|
|
|
* is deleted. |
96
|
|
|
* |
97
|
|
|
* @param mixed $model model instance or class name |
98
|
|
|
* @param int|null $modelId (default: null) The Model id |
99
|
|
|
* |
100
|
|
|
* @return self |
101
|
|
|
*/ |
102
|
|
|
public function modelDeleted($model, ?int $modelId = null): self |
103
|
|
|
{ |
104
|
|
|
if ($this->isModel($model)) { |
105
|
|
|
$modelClassName = get_class($model); |
106
|
|
|
$modelId = $model->getKey(); |
107
|
|
|
} else { |
108
|
|
|
$modelClassName = $model; |
109
|
|
|
} |
110
|
|
|
$this->conditions[] = new Condition( |
111
|
|
|
Event::EVENT_ELOQUENT_DELETED, |
112
|
|
|
$modelClassName, |
113
|
|
|
$modelId |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Adds a Condition that tags a cache to get invalidated |
121
|
|
|
* when the specified Model instance, or any Model of the specified class |
122
|
|
|
* is restored. |
123
|
|
|
* |
124
|
|
|
* @param mixed $model model instance or class name |
125
|
|
|
* @param int|null $modelId (default: null) The Model id |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
|
|
public function modelRestored($model, ?int $modelId = null): self |
130
|
|
|
{ |
131
|
|
|
if ($this->isModel($model)) { |
132
|
|
|
$modelClassName = get_class($model); |
133
|
|
|
$modelId = $model->getKey(); |
134
|
|
|
} else { |
135
|
|
|
$modelClassName = $model; |
136
|
|
|
} |
137
|
|
|
$this->conditions[] = new Condition( |
138
|
|
|
Event::EVENT_ELOQUENT_RESTORED, |
139
|
|
|
$modelClassName, |
140
|
|
|
$modelId |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function current(): mixed |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
return current($this->conditions); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function key(): scalar |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
return key($this->conditions); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function next(): void |
157
|
|
|
{ |
158
|
|
|
next($this->conditions); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function rewind(): void |
162
|
|
|
{ |
163
|
|
|
rewind($this->conditions); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function valid(): boolean |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
return valid($this->conditions); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.