|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Kreta package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace Kreta\SharedKernel\Serialization; |
|
16
|
|
|
|
|
17
|
|
|
use BenGorUser\User\Domain\Model\Event\UserEvent; |
|
18
|
|
|
use Kreta\SharedKernel\Domain\Model\DomainEvent; |
|
19
|
|
|
use Kreta\SharedKernel\Domain\Model\Exception; |
|
20
|
|
|
use Kreta\SharedKernel\Event\AsyncEvent; |
|
21
|
|
|
|
|
22
|
|
|
class AsyncEventSerializer implements Serializer |
|
23
|
|
|
{ |
|
24
|
|
|
private $resolver; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Resolver $resolver) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->resolver = $resolver; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function serialize($event) : string |
|
32
|
|
|
{ |
|
33
|
|
|
// trade off checking if it is a userEvent instance |
|
34
|
|
|
// because IdentityAccess BC extends BenGorUser ecosystem |
|
35
|
|
|
if (!$event instanceof DomainEvent && !$event instanceof UserEvent) { |
|
36
|
|
|
throw new InvalidSerializationObjectException( |
|
37
|
|
|
DomainEvent::class, |
|
38
|
|
|
get_class($event) |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return json_encode([ |
|
43
|
|
|
'name' => $this->resolver->resolve(get_class($event)), |
|
44
|
|
|
'occurredOn' => $event->occurredOn()->format('Y-m-d H:i:s'), |
|
45
|
|
|
'values' => $this->propertyValuesFrom($event), |
|
46
|
|
|
]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function deserialize(string $serializedObject) |
|
50
|
|
|
{ |
|
51
|
|
|
$decodedObject = json_decode($serializedObject, true); |
|
52
|
|
|
if (!isset($decodedObject['name']) |
|
53
|
|
|
|| !isset($decodedObject['occurredOn']) |
|
54
|
|
|
|| !isset($decodedObject['values']) |
|
55
|
|
|
) { |
|
56
|
|
|
throw new Exception( |
|
57
|
|
|
'Unserialized object needs "name", "occurredOn" and "values" to be constructed' |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return new AsyncEvent( |
|
62
|
|
|
$decodedObject['name'], |
|
63
|
|
|
new \DateTimeImmutable($decodedObject['occurredOn']), |
|
64
|
|
|
$decodedObject['values'] |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function propertyValuesFrom($event) : array |
|
69
|
|
|
{ |
|
70
|
|
|
$values = []; |
|
71
|
|
|
$reflectionClass = new \ReflectionClass($event); |
|
72
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
|
73
|
|
|
$property->setAccessible(true); |
|
74
|
|
|
$value = $property->getValue($event); |
|
75
|
|
|
if ($value instanceof \DateTimeInterface) { |
|
76
|
|
|
$value = $value->format('Y-m-d H:i:s'); |
|
77
|
|
|
} |
|
78
|
|
|
$values[$property->getName()] = (string) $value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $values; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|