1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codefocus\ManagedCache; |
4
|
|
|
|
5
|
|
|
use Codefocus\ManagedCache\Events\Event; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Iterator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ConditionBuilder. |
11
|
|
|
*/ |
12
|
|
|
class ConditionBuilder implements Iterator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $conditions = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Adds a Condition that tags a cache to get invalidated |
21
|
|
|
* when a new Model of the specified class is created. |
22
|
|
|
* |
23
|
|
|
* @param string $modelClassName model class name |
24
|
|
|
* |
25
|
|
|
* @return self |
26
|
|
|
*/ |
27
|
|
|
public function modelCreated(string $modelClassName): self |
28
|
|
|
{ |
29
|
|
|
return $this->addEloquentEventCondition( |
30
|
|
|
Event::EVENT_ELOQUENT_CREATED, |
31
|
|
|
$modelClassName |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Adds a Condition that tags a cache to get invalidated |
37
|
|
|
* when the specified Model instance, or any Model of the specified class |
38
|
|
|
* is updated. |
39
|
|
|
* |
40
|
|
|
* @param mixed $model model instance or class name |
41
|
|
|
* @param int|null $modelId (default: null) The Model id |
42
|
|
|
* |
43
|
|
|
* @return self |
44
|
|
|
*/ |
45
|
|
|
public function modelUpdated($model, ?int $modelId = null): self |
46
|
|
|
{ |
47
|
|
|
if ($this->isModel($model)) { |
|
|
|
|
48
|
|
|
$modelClassName = get_class($model); |
49
|
|
|
$modelId = $model->getKey(); |
50
|
|
|
} else { |
51
|
|
|
$modelClassName = $model; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->addEloquentEventCondition( |
55
|
|
|
Event::EVENT_ELOQUENT_UPDATED, |
56
|
|
|
$modelClassName, |
57
|
|
|
$modelId |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adds a Condition that tags a cache to get invalidated |
63
|
|
|
* when the specified Model instance, or any Model of the specified class |
64
|
|
|
* is saved. |
65
|
|
|
* |
66
|
|
|
* @param mixed $model model instance or class name |
67
|
|
|
* @param int|null $modelId (default: null) The Model id |
68
|
|
|
* |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
|
|
public function modelSaved($model, ?int $modelId = null): self |
72
|
|
|
{ |
73
|
|
|
if ($this->isModel($model)) { |
74
|
|
|
$modelClassName = get_class($model); |
75
|
|
|
$modelId = $model->getKey(); |
76
|
|
|
} else { |
77
|
|
|
$modelClassName = $model; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->addEloquentEventCondition( |
81
|
|
|
Event::EVENT_ELOQUENT_SAVED, |
82
|
|
|
$modelClassName, |
83
|
|
|
$modelId |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Adds a Condition that tags a cache to get invalidated |
89
|
|
|
* when the specified Model instance, or any Model of the specified class |
90
|
|
|
* is deleted. |
91
|
|
|
* |
92
|
|
|
* @param mixed $model model instance or class name |
93
|
|
|
* @param int|null $modelId (default: null) The Model id |
94
|
|
|
* |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
|
|
public function modelDeleted($model, ?int $modelId = null): self |
98
|
|
|
{ |
99
|
|
|
if ($this->isModel($model)) { |
100
|
|
|
$modelClassName = get_class($model); |
101
|
|
|
$modelId = $model->getKey(); |
102
|
|
|
} else { |
103
|
|
|
$modelClassName = $model; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->addEloquentEventCondition( |
107
|
|
|
Event::EVENT_ELOQUENT_DELETED, |
108
|
|
|
$modelClassName, |
109
|
|
|
$modelId |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Adds a Condition that tags a cache to get invalidated |
115
|
|
|
* when the specified Model instance, or any Model of the specified class |
116
|
|
|
* is restored. |
117
|
|
|
* |
118
|
|
|
* @param mixed $model model instance or class name |
119
|
|
|
* @param int|null $modelId (default: null) The Model id |
120
|
|
|
* |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
|
|
public function modelRestored($model, ?int $modelId = null): self |
124
|
|
|
{ |
125
|
|
|
if ($this->isModel($model)) { |
126
|
|
|
$modelClassName = get_class($model); |
127
|
|
|
$modelId = $model->getKey(); |
128
|
|
|
} else { |
129
|
|
|
$modelClassName = $model; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->addEloquentEventCondition( |
133
|
|
|
Event::EVENT_ELOQUENT_RESTORED, |
134
|
|
|
$modelClassName, |
135
|
|
|
$modelId |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Add an Eloquent event Condition. |
141
|
|
|
* |
142
|
|
|
* @param string $eventName |
143
|
|
|
* @param string|null $modelClassName (default: null) |
144
|
|
|
* @param int|null $modelId (default: null) |
145
|
|
|
* @param string|null $relatedModelClassName (default: null) |
146
|
|
|
* @param int|null $relatedModelId (default: null) |
147
|
|
|
* |
148
|
|
|
* @return self |
149
|
|
|
*/ |
150
|
|
|
private function addEloquentEventCondition( |
151
|
|
|
string $eventName, |
152
|
|
|
string $modelClassName, |
153
|
|
|
?int $modelId = null, |
154
|
|
|
?string $relatedModelClassName = null, |
155
|
|
|
?int $relatedModelId = null |
156
|
|
|
): self { |
157
|
|
|
$this->conditions[] = new Condition( |
158
|
|
|
$eventName, |
159
|
|
|
$modelClassName, |
160
|
|
|
$modelId, |
161
|
|
|
$relatedModelClassName, |
162
|
|
|
$relatedModelId |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return the value of the current item in the conditions array. |
173
|
|
|
* |
174
|
|
|
* @return Condition|false |
175
|
|
|
*/ |
176
|
|
|
public function current() |
177
|
|
|
{ |
178
|
|
|
return current($this->conditions); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return the key of the current item in the conditions array. |
183
|
|
|
* |
184
|
|
|
* @return mixed |
185
|
|
|
*/ |
186
|
|
|
public function key() |
187
|
|
|
{ |
188
|
|
|
return key($this->conditions); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Move the conditions array pointer to the next item. |
193
|
|
|
*/ |
194
|
|
|
public function next(): void |
195
|
|
|
{ |
196
|
|
|
next($this->conditions); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Move the conditions array pointer to the first item. |
201
|
|
|
*/ |
202
|
|
|
public function rewind(): void |
203
|
|
|
{ |
204
|
|
|
reset($this->conditions); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns whether the current item in the conditions array is valid. |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public function valid(): bool |
213
|
|
|
{ |
214
|
|
|
return (current($this->conditions) !== false); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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.