NativeAggregateSerializer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromSerialized() 0 9 2
A serialize() 0 3 1
1
<?php
2
3
/*
4
 * event-sourcing (https://github.com/phpgears/event-sourcing).
5
 * Event Sourcing base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event-sourcing
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\EventSourcing\Aggregate\Serializer;
15
16
use Gears\EventSourcing\Aggregate\AggregateRoot;
17
use Gears\EventSourcing\Aggregate\Serializer\Exception\AggregateSerializationException;
18
19
/**
20
 * PHP native aggregate root serializer.
21
 */
22
final class NativeAggregateSerializer implements AggregateSerializer
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function serialize(AggregateRoot $aggregateRoot): string
28
    {
29
        return \serialize($aggregateRoot);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function fromSerialized(string $serialized): AggregateRoot
36
    {
37
        $aggregateRoot = \unserialize($serialized);
38
39
        if (!$aggregateRoot instanceof AggregateRoot) {
40
            throw new AggregateSerializationException('Invalid unserialized aggregate root');
41
        }
42
43
        return $aggregateRoot;
44
    }
45
}
46