Completed
Push — master ( c2ba48...1fd225 )
by Markus
14s queued 12s
created

EventMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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