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

DefaultEventSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 37
ccs 9
cts 11
cp 0.8182
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A serialize() 0 9 2
A unserialize() 0 9 2
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