NativeAggregateSerializer::fromSerialized()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

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
rs 10
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