1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/cqrs-tools |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\CQRSTools\Event; |
11
|
|
|
|
12
|
|
|
use DateTimeImmutable; |
13
|
|
|
use Exception; |
14
|
|
|
use League\Event\AbstractEvent as LeagueEvent; |
15
|
|
|
use Slick\CQRSTools\Domain\AggregateRootIdentifier; |
16
|
|
|
use Slick\CQRSTools\Domain\Event\EventId; |
17
|
|
|
use Slick\CQRSTools\Event; |
18
|
|
|
use Slick\CQRSTools\Exception\FailToCreateDateException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract Event |
22
|
|
|
* |
23
|
|
|
* @package Slick\CQRSTools\Event |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractEvent extends LeagueEvent implements Event |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventId |
30
|
|
|
*/ |
31
|
|
|
protected $eventId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DateTimeImmutable |
35
|
|
|
*/ |
36
|
|
|
protected $occurredOn; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var null|AggregateRootIdentifier |
40
|
|
|
*/ |
41
|
|
|
protected $author; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates an Abstract Event |
45
|
|
|
* |
46
|
|
|
* @param AggregateRootIdentifier|null $author |
47
|
|
|
*/ |
48
|
|
|
public function __construct(AggregateRootIdentifier $author = null) |
49
|
|
|
{ |
50
|
|
|
$this->eventId = new EventId(); |
51
|
|
|
$this->author = $author; |
52
|
|
|
try { |
53
|
|
|
$this->occurredOn = new DateTimeImmutable(); |
54
|
|
|
} catch (Exception $caught) { |
55
|
|
|
throw new FailToCreateDateException($caught->getMessage(), 1, $caught); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Event identifier |
61
|
|
|
* |
62
|
|
|
* @return EventId |
63
|
|
|
*/ |
64
|
|
|
public function eventId(): EventId |
65
|
|
|
{ |
66
|
|
|
return $this->eventId; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Date and time event has occurred |
71
|
|
|
* |
72
|
|
|
* @return DateTimeImmutable |
73
|
|
|
*/ |
74
|
|
|
public function occurredOn(): DateTimeImmutable |
75
|
|
|
{ |
76
|
|
|
return $this->occurredOn; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The aggregate identifier of the entity responsible for this event |
81
|
|
|
* |
82
|
|
|
* @return null|AggregateRootIdentifier |
83
|
|
|
*/ |
84
|
|
|
public function author(): ?AggregateRootIdentifier |
85
|
|
|
{ |
86
|
|
|
return $this->author; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Specify data which should be serialized to JSON |
91
|
|
|
* |
92
|
|
|
* @return mixed data which can be serialized by json_encode(), |
93
|
|
|
* which is a value of any type other than a resource. |
94
|
|
|
*/ |
95
|
|
|
abstract public function jsonSerialize(); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Used to unserialize from a stored event |
99
|
|
|
* |
100
|
|
|
* @param mixed $data |
101
|
|
|
*/ |
102
|
|
|
abstract public function unserializeEvent($data): void; |
103
|
|
|
} |
104
|
|
|
|