|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jellyfish\Event; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayObject; |
|
8
|
|
|
use Jellyfish\Event\Exception\MappingException; |
|
9
|
|
|
use Jellyfish\Queue\MessageFactoryInterface; |
|
10
|
|
|
use Jellyfish\Queue\MessageInterface; |
|
11
|
|
|
use Jellyfish\Serializer\SerializerInterface; |
|
12
|
|
|
|
|
13
|
|
|
use function get_class; |
|
14
|
|
|
|
|
15
|
|
|
class EventMapper implements EventMapperInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Jellyfish\Event\EventFactoryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $eventFactory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Jellyfish\Queue\MessageFactoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $messageFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Jellyfish\Serializer\SerializerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $serializer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param \Jellyfish\Event\EventFactoryInterface $eventFactory |
|
34
|
|
|
* @param \Jellyfish\Queue\MessageFactoryInterface $messageFactory |
|
35
|
|
|
* @param \Jellyfish\Serializer\SerializerInterface $serializer |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
EventFactoryInterface $eventFactory, |
|
39
|
|
|
MessageFactoryInterface $messageFactory, |
|
40
|
|
|
SerializerInterface $serializer |
|
41
|
|
|
) { |
|
42
|
|
|
$this->messageFactory = $messageFactory; |
|
43
|
|
|
$this->eventFactory = $eventFactory; |
|
44
|
|
|
$this->serializer = $serializer; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param \Jellyfish\Queue\MessageInterface $message |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Jellyfish\Event\EventInterface |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \Jellyfish\Event\Exception\MappingException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function fromMessage(MessageInterface $message): EventInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$type = $message->getHeader('body_type'); |
|
57
|
|
|
$eventName = $message->getHeader('event_name'); |
|
58
|
|
|
|
|
59
|
|
|
if ($type === null || $eventName === null) { |
|
60
|
|
|
throw new MappingException('Could not map message to event.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$payload = $this->serializer->deserialize($message->getBody(), $type, 'json'); |
|
64
|
|
|
$metaProperties = $this->mapHeadersToMetaProperties($message->getHeaders()); |
|
65
|
|
|
|
|
66
|
|
|
return $this->eventFactory->create() |
|
67
|
|
|
->setName($eventName) |
|
68
|
|
|
->setPayload($payload) |
|
69
|
|
|
->setMetaProperties($metaProperties); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \Jellyfish\Event\EventInterface $event |
|
74
|
|
|
* @return \Jellyfish\Queue\MessageInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
public function toMessage(EventInterface $event): MessageInterface |
|
77
|
|
|
{ |
|
78
|
|
|
$payload = $event->getPayload(); |
|
79
|
|
|
$metaProperties = $event->getMetaProperties(); |
|
80
|
|
|
|
|
81
|
|
|
$message = $this->messageFactory->create() |
|
82
|
|
|
->setHeaders($metaProperties) |
|
83
|
|
|
->setHeader('event_name', $event->getName()) |
|
84
|
|
|
->setHeader('body_type', get_class($payload)) |
|
85
|
|
|
->setBody($this->serializer->serialize($payload, 'json')); |
|
86
|
|
|
|
|
87
|
|
|
if (!($payload instanceof ArrayObject)) { |
|
88
|
|
|
return $message; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$type = 'stdClass[]'; |
|
92
|
|
|
|
|
93
|
|
|
if ($payload->count() !== 0) { |
|
94
|
|
|
$type = get_class($payload->offsetGet(0)) . '[]'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$message->setHeader('body_type', $type); |
|
98
|
|
|
|
|
99
|
|
|
return $message; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array $headers |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function mapHeadersToMetaProperties(array $headers): array |
|
108
|
|
|
{ |
|
109
|
|
|
$metaProperties = $headers; |
|
110
|
|
|
|
|
111
|
|
|
unset($metaProperties['body_type'], $metaProperties['event_name']); |
|
112
|
|
|
|
|
113
|
|
|
return $metaProperties; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|