1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Models; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Spatie\EventProjector\Facades\Projectionist; |
9
|
|
|
use Spatie\EventProjector\Exceptions\InvalidStoredEvent; |
10
|
|
|
use Spatie\EventProjector\EventSerializers\EventSerializer; |
11
|
|
|
|
12
|
|
|
class StoredEvent implements Arrayable |
13
|
|
|
{ |
14
|
|
|
/** @var int|null */ |
15
|
|
|
public $id; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
public $event_properties; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
public $aggregate_uuid; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
public $event_class; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
public $meta_data; |
28
|
|
|
|
29
|
|
|
/** @var \Carbon\Carbon */ |
30
|
|
|
public $created_at; |
31
|
|
|
|
32
|
|
|
/** @var \Spatie\EventProjector\ShouldBeStored|null */ |
33
|
|
|
public $event; |
34
|
|
|
|
35
|
|
|
public function __construct(array $data) |
36
|
|
|
{ |
37
|
|
|
$this->id = $data['id'] ?? null; |
38
|
|
|
$this->event_properties = $data['event_properties']; |
39
|
|
|
$this->aggregate_uuid = $data['aggregate_uuid']; |
40
|
|
|
$this->event_class = self::getActualClassForEvent($data['event_class']); |
41
|
|
|
$this->meta_data = $data['meta_data']; |
42
|
|
|
$this->created_at = $data['created_at']; |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
$this->event = app(EventSerializer::class)->deserialize( |
46
|
|
|
self::getActualClassForEvent($this->event_class), |
47
|
|
|
json_encode($this->event_properties) |
48
|
|
|
); |
49
|
|
|
} catch (Exception $exception) { |
50
|
|
|
throw InvalidStoredEvent::couldNotUnserializeEvent($this, $exception); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function toArray() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
'id' => $this->id, |
58
|
|
|
'event_properties' => $this->event_properties, |
59
|
|
|
'aggregate_uuid' => $this->aggregate_uuid, |
60
|
|
|
'event_class' => self::getEventClass($this->event_class), |
61
|
|
|
'meta_data' => $this->meta_data instanceof Arrayable ? $this->meta_data->toArray() : (array) $this->meta_data, |
62
|
|
|
'created_at' => $this->created_at, |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function handle() |
67
|
|
|
{ |
68
|
|
|
Projectionist::handleWithSyncProjectors($this); |
69
|
|
|
|
70
|
|
|
if (method_exists($this->event, 'tags')) { |
71
|
|
|
$tags = $this->event->tags(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$storedEventJob = call_user_func( |
75
|
|
|
[config('event-projector.stored_event_job'), 'createForEvent'], |
76
|
|
|
$this, |
77
|
|
|
$tags ?? [] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
dispatch($storedEventJob->onQueue($this->event->queue ?? config('event-projector.queue'))); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected static function getActualClassForEvent(string $class): string |
84
|
|
|
{ |
85
|
|
|
return Arr::get(config('event-projector.event_class_map', []), $class, $class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
protected static function getEventClass(string $class): string |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$map = config('event-projector.event_class_map', []); |
91
|
|
|
|
92
|
|
|
if (! empty($map) && in_array($class, $map)) { |
93
|
|
|
return array_search($class, $map, true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $class; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.