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

EventSerializer::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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
}