1
|
|
|
<?php namespace C4tech\RayEmitter\Event; |
2
|
|
|
|
3
|
|
|
use C4tech\RayEmitter\Contracts\Domain\Event as EventInterface; |
4
|
|
|
use C4tech\RayEmitter\Contracts\Event\Store as StoreInterface; |
5
|
|
|
use Illuminate\Support\Facades\Event as EventBus; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
class Store extends Model implements StoreInterface |
10
|
|
|
{ |
11
|
|
|
protected static $queue = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @inheritDoc |
15
|
|
|
*/ |
16
|
|
|
protected $table = 'event_store'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @inheritDoc |
20
|
|
|
*/ |
21
|
|
|
protected $fillable = [ |
22
|
|
|
'identifier', |
23
|
|
|
'sequence', |
24
|
|
|
'event', |
25
|
|
|
'payload' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
31
|
1 |
|
public function enqueue(EventInterface $event) |
32
|
|
|
{ |
33
|
1 |
|
$class = get_class($event); |
34
|
1 |
|
static::$queue[] = [ |
35
|
1 |
|
'event' => $class, |
36
|
1 |
|
'identifier' => $event->getId(), |
37
|
1 |
|
'payload' => $event->serialize(), |
38
|
|
|
'raw' => $event |
39
|
1 |
|
]; |
40
|
|
|
|
41
|
1 |
|
EventBus::fire('queue:' . $class, [$event]); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
1 |
|
public function getFor($identifier) |
48
|
|
|
{ |
49
|
1 |
|
$events = new Collection; |
50
|
|
|
|
51
|
1 |
|
$recorded_events = $this->newQuery()->forEntity($identifier)->get(); |
52
|
1 |
|
foreach ($recorded_events as $record) { |
53
|
1 |
|
$events->append($this->restoreEvent($record)); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
1 |
|
return $events; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function replayAll() |
60
|
|
|
{ |
61
|
1 |
|
$records = $this->all(); |
62
|
|
|
|
63
|
1 |
|
$records->each(function ($record) { |
64
|
1 |
|
$event = $this->restoreEvent($record); |
65
|
1 |
|
EventBus::fire('replay:' . $record->event, [$event]); |
66
|
1 |
|
}); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Restore Event |
71
|
|
|
* |
72
|
|
|
* Unserialize saved record back into an Event. |
73
|
|
|
* @param static $record Event Store model record |
74
|
|
|
* @return EventInterface |
75
|
|
|
*/ |
76
|
1 |
|
protected function restoreEvent($record) |
77
|
|
|
{ |
78
|
1 |
|
$class = $record->event; |
|
|
|
|
79
|
|
|
|
80
|
1 |
|
return $class::unserialize($record); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Save Queue |
85
|
|
|
* |
86
|
|
|
* Persist all queued Events into Event Store. |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
1 |
|
public function saveQueue() |
90
|
|
|
{ |
91
|
1 |
|
foreach (static::$queue as $record) { |
92
|
1 |
|
$event = $record['raw']; |
93
|
1 |
|
unset($record['raw']); |
94
|
|
|
|
95
|
1 |
|
$record['sequence'] = $this->newQuery()->forEntity($record['identifier']) |
96
|
1 |
|
->count(); |
97
|
1 |
|
static::create($record); |
98
|
|
|
|
99
|
1 |
|
EventBus::fire('save:' . $record['event'], [$event]); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
static::$queue = []; |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Scope: For Entity |
107
|
|
|
* |
108
|
|
|
* Query scope for Entity identifier. |
109
|
|
|
* @param Query $query Query Builder |
110
|
|
|
* @param string $identifier Entity identifier |
111
|
|
|
* @return Query |
112
|
|
|
*/ |
113
|
1 |
|
public function scopeForEntity($query, $identifier) |
114
|
|
|
{ |
115
|
1 |
|
return $query->where('identifier', '=', $identifier); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.