1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cubiche\Domain\EventPublisher; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\EventBus\Event\Event; |
14
|
|
|
use DateTime; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* DomainEvent class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class DomainEvent extends Event implements DomainEventInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $metadata = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $payload = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* DomainEvent constructor. |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->setMetadata('occurredOn', new DateTime()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return DateTime |
43
|
|
|
*/ |
44
|
|
|
public function occurredOn() |
45
|
|
|
{ |
46
|
|
|
return $this->getMetadata('occurredOn'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $property |
51
|
|
|
* @param mixed $value |
52
|
|
|
*/ |
53
|
|
|
protected function setMetadata($property, $value) |
54
|
|
|
{ |
55
|
|
|
$this->metadata[$property] = $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $property |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
protected function getMetadata($property) |
64
|
|
|
{ |
65
|
|
|
return $this->metadata[$property]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $property |
70
|
|
|
* @param mixed $value |
71
|
|
|
*/ |
72
|
|
|
protected function setPayload($property, $value) |
73
|
|
|
{ |
74
|
|
|
$this->payload[$property] = $value; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $property |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
protected function getPayload($property) |
83
|
|
|
{ |
84
|
|
|
return $this->payload[$property]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public static function fromArray(array $data) |
91
|
|
|
{ |
92
|
|
|
$reflectionClass = new \ReflectionClass(static::class); |
93
|
|
|
|
94
|
|
|
/** @var DomainEvent $domainEvent */ |
95
|
|
|
$domainEvent = $reflectionClass->newInstanceWithoutConstructor(); |
96
|
|
|
|
97
|
|
|
$domainEvent->eventName = $data['eventType']; |
98
|
|
|
$domainEvent->metadata = $data['metadata']; |
99
|
|
|
$domainEvent->payload = $data['payload']; |
100
|
|
|
|
101
|
|
|
return $domainEvent; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function toArray() |
108
|
|
|
{ |
109
|
|
|
return array( |
110
|
|
|
'eventType' => $this->eventName(), |
111
|
|
|
'metadata' => $this->metadata, |
112
|
|
|
'payload' => $this->payload, |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|