Passed
Push — v4.2 ( 50993c )
by Masiukevich
07:39
created

DefaultEventSerializer::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 2
cts 4
cp 0.5
crap 2.5
rs 10
1
<?php
2
3
/**
4
 * Event Sourcing implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\EventSourcing\EventStream\Serializer;
14
15
use ServiceBus\EventSourcing\EventStream\Serializer\Exceptions\SerializeEventFailed;
16
use ServiceBus\MessageSerializer\Symfony\SymfonyMessageSerializer;
17
18
/**
19
 *
20
 */
21
final class DefaultEventSerializer implements EventSerializer
22
{
23
    /** @var SymfonyMessageSerializer */
24
    private $serializer;
25
26 9
    public function __construct(SymfonyMessageSerializer $serializer)
27
    {
28 9
        $this->serializer = $serializer;
29 9
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 7
    public function serialize(object $event): string
35
    {
36
        try
37
        {
38 7
            return $this->serializer->encode($event);
39
        }
40
        catch (\Throwable $throwable)
41
        {
42
            throw SerializeEventFailed::fromThrowable($throwable);
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 5
    public function unserialize(string $eventClass, string $payload): object
50
    {
51
        try
52
        {
53 5
            return $this->serializer->decode($payload);
54
        }
55 1
        catch (\Throwable $throwable)
56
        {
57 1
            throw SerializeEventFailed::fromThrowable($throwable);
58
        }
59
    }
60
}
61