Completed
Push — master ( 767511...1b60d7 )
by Freek
04:00
created

EventSerializer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 29
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A serialize() 0 8 1
A deserialize() 0 6 1
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Symfony\Component\Serializer\Encoder\JsonEncoder;
6
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
7
use Symfony\Component\Serializer\Serializer;
8
9
class EventSerializer
10
{
11
    /** @var \Symfony\Component\Serializer\Serializer */
12
    protected $serializer;
13
14
    public function __construct()
15
    {
16
        $encoders = array(new JsonEncoder());
17
        $normalizers = array(new ObjectNormalizer());
18
19
        $this->serializer = new Serializer($normalizers, $encoders);
20
    }
21
22
    public function serialize(ShouldBeStored $event): string
23
    {
24
        $event->__sleep();
0 ignored issues
show
Bug introduced by
The method __sleep() does not seem to exist on object<Spatie\EventProjector\ShouldBeStored>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
26
        $json = $this->serializer->serialize($event, 'json');
27
28
        return $json;
29
    }
30
31
    public function deserialize(string $eventClass, string $json): ShouldBeStored
32
    {
33
        $restoredEvent = $this->serializer->deserialize($json, $eventClass, 'json');
34
35
        return unserialize(serialize($restoredEvent));
36
    }
37
}