|
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\Infrastructure\Serializer\SimpleBus; |
|
16
|
|
|
|
|
17
|
|
|
use Kreta\SharedKernel\Event\AsyncEvent; |
|
18
|
|
|
use SimpleBus\Serialization\Envelope\DefaultEnvelope; |
|
19
|
|
|
use SimpleBus\Serialization\ObjectSerializer; |
|
20
|
|
|
|
|
21
|
|
|
class Serializer implements ObjectSerializer |
|
22
|
|
|
{ |
|
23
|
|
|
public function serialize($object) |
|
24
|
|
|
{ |
|
25
|
|
|
if ($object instanceof DefaultEnvelope) { |
|
26
|
|
|
return json_encode([ |
|
27
|
|
|
'name' => 'identity_access.user_register', |
|
28
|
|
|
'occurredOn' => $object->message()->occurredOn()->format('Y-m-d H:i:s'), |
|
29
|
|
|
'values' => [ |
|
30
|
|
|
'user_id' => 'id', |
|
31
|
|
|
'email' => '[email protected]', |
|
32
|
|
|
], |
|
33
|
|
|
]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return json_encode([ |
|
37
|
|
|
'name' => 'identity_access.user_register', |
|
38
|
|
|
'occurredOn' => $object->occurredOn()->format('Y-m-d H:i:s'), |
|
39
|
|
|
'values' => [ |
|
40
|
|
|
'user_id' => 'id', |
|
41
|
|
|
'email' => '[email protected]', |
|
42
|
|
|
], |
|
43
|
|
|
]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function deserialize($serializedObject, $type) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($type === DefaultEnvelope::class) { |
|
49
|
|
|
return DefaultEnvelope::forSerializedMessage( |
|
50
|
|
|
AsyncEvent::class, $serializedObject |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($type === AsyncEvent::class) { |
|
55
|
|
|
$serializedObject = json_decode($serializedObject, true); |
|
56
|
|
|
|
|
57
|
|
|
if (!isset($serializedObject['name']) |
|
58
|
|
|
|| !isset($serializedObject['occurredOn']) |
|
59
|
|
|
|| !isset($serializedObject['values']) |
|
60
|
|
|
) { |
|
61
|
|
|
throw new \Exception(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return new AsyncEvent( |
|
65
|
|
|
$serializedObject['name'], |
|
66
|
|
|
new \DateTimeImmutable($serializedObject['occurredOn']), |
|
67
|
|
|
$serializedObject['values'] |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new \Exception('Object not supported'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|