1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Shared Kernel library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace LIN3S\SharedKernel\Event; |
15
|
|
|
|
16
|
|
|
use App\Domain\Model\Post\PostId; |
17
|
|
|
use App\Domain\Model\Post\PostWasCreated; |
18
|
|
|
use LIN3S\SharedKernel\Domain\Model\DomainEvent; |
19
|
|
|
use LIN3S\SharedKernel\Exception\InvalidArgumentException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Beñat Espiña <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class StoredEvent |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
private $id; |
27
|
|
|
private $type; |
28
|
|
|
private $payload; |
29
|
|
|
private $occurredOn; |
30
|
|
|
private $streamName; |
31
|
|
|
private $streamVersion; |
32
|
|
|
|
33
|
|
|
public static function fromDomainEvent(DomainEvent $event, StreamName $name, StreamVersion $version) : self |
34
|
|
|
{ |
35
|
|
|
$instance = new self(get_class($event), $event->occurredOn(), $name, $version); |
36
|
|
|
$instance->setPayload($event); |
37
|
|
|
|
38
|
|
|
return $instance; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function setPayload(DomainEvent $event) : void |
42
|
|
|
{ |
43
|
|
|
$this->payload = []; |
44
|
|
|
$eventReflection = new \ReflectionClass($event); |
45
|
|
|
foreach ($eventReflection->getProperties() as $property) { |
46
|
|
|
if ('occurredOn' === $property->name) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
$property->setAccessible(true); |
50
|
|
|
$this->payload[$property->getName()] = $this->serialize($property, $event); |
51
|
|
|
} |
52
|
|
|
$this->payload = json_encode($this->payload); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function __construct(string $type, \DateTimeInterface $occurredOn, StreamName $name, StreamVersion $version) |
56
|
|
|
{ |
57
|
|
|
$this->type = $type; |
58
|
|
|
$this->setOccurredOn($occurredOn); |
59
|
|
|
$this->streamName = $name->name(); |
60
|
|
|
$this->streamVersion = $version->version(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function setOccurredOn(\DateTimeInterface $occurredOn) : void |
64
|
|
|
{ |
65
|
|
|
$this->checkDateTimeIsValid($occurredOn); |
66
|
|
|
$occurredOn->setTimezone(new \DateTimeZone('UTC')); |
67
|
|
|
$this->occurredOn = $occurredOn->getTimestamp(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function toArray() : array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
$this->type, |
74
|
|
|
$this->payload, |
75
|
|
|
$this->occurredOn, |
76
|
|
|
$this->streamName, |
77
|
|
|
$this->streamVersion, |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function checkDateTimeIsValid(\DateTimeInterface $occurredOn) : void |
82
|
|
|
{ |
83
|
|
|
if (!($occurredOn instanceof \DateTimeImmutable) && !($occurredOn instanceof \DateTime)) { |
84
|
|
|
throw new InvalidArgumentException('Given occurredOn is not a \DateTime or \DateTimeImmutable instance'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function serialize(\ReflectionProperty $property, $object, array $result = []) |
89
|
|
|
{ |
90
|
|
|
$property->setAccessible(true); |
91
|
|
|
$value = $property->getValue($object); |
92
|
|
|
if (is_scalar($value)) { |
93
|
|
|
return $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$className = get_class($value); |
97
|
|
|
$reflectionClass = new \ReflectionClass($value); |
98
|
|
|
$properties = $reflectionClass->getProperties(); |
99
|
|
|
|
100
|
|
|
foreach ($properties as $property) { |
101
|
|
|
$result[$className][$property->getName()] = $this->serialize($property, $value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|