EventSerializer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Eole\Sandstone\Push;
4
5
use Symfony\Component\EventDispatcher\Event;
6
use JMS\Serializer\SerializerInterface;
7
use JMS\Serializer\SerializationContext;
8
9
class EventSerializer
10
{
11
    /**
12
     * @var SerializerInterface
13
     */
14
    private $serializer;
15
16
    /**
17
     * @param SerializerInterface $serializer
18
     */
19
    public function __construct(SerializerInterface $serializer)
20
    {
21
        $this->serializer = $serializer;
22
    }
23
24
    /**
25
     * @param string $name
26
     * @param Event $event
27
     *
28
     * @return string
29
     */
30
    public function serializeEvent($name, Event $event)
31
    {
32
        $context = SerializationContext::create();
33
34
        return serialize(array(
35
            'name' => $name,
36
            'class' => get_class($event),
37
            'event' => $this->serializer->serialize($event, 'json', $context),
38
        ));
39
    }
40
41
    /**
42
     * @param string $serial
43
     *
44
     * @return array
45
     */
46
    public function deserializeEvent($serial)
47
    {
48
        $data = unserialize($serial);
49
50
        return array(
51
            'name' => $data['name'],
52
            'class' => $data['class'],
53
            'event' => $this->serializer->deserialize($data['event'], $data['class'], 'json'),
54
        );
55
    }
56
}
57