SerializableEventNormalizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 11 1
A supportsNormalization() 0 4 1
A denormalize() 0 13 2
A supportsDenormalization() 0 4 1
1
<?php
2
namespace Workana\AsyncJobs\Normalizer;
3
4
use Bernard\Normalizer\AbstractAggregateNormalizerAware;
5
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
6
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7
use Workana\AsyncJobs\Parameter;
8
use Workana\AsyncJobs\SerializableEvent;
9
10
/**
11
 * @author Carlos Frutos <[email protected]>
12
 */
13
class SerializableEventNormalizer extends AbstractAggregateNormalizerAware  implements NormalizerInterface, DenormalizerInterface
0 ignored issues
show
Coding Style introduced by
Expected 1 space before implements keyword; 2 found
Loading history...
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18
    public function normalize($object, $format = null, array $context = array())
19
    {
20
        $parameters = Accesor::get($object, 'parameters');
21
22
        return [
23
            'class' => get_class($object),
24
            'parameters' => array_map(function($param) {
25
                return $this->aggregate->normalize($param);
26
            }, $parameters),
27
        ];
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function supportsNormalization($data, $format = null)
34
    {
35
        return ($data instanceof SerializableEvent);
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function denormalize($data, $class, $format = null, array $context = array())
42
    {
43
        $denormalizedParams = [];
44
        foreach ($data['parameters'] as $name => $paramData) {
45
            $denormalizedParams[$name] = $this->aggregate->denormalize($paramData, Parameter::class);
46
        }
47
48
        $event = Accesor::newInstanceWithoutConstructor($data['class']);
49
50
        Accesor::set($event, 'parameters', $denormalizedParams);
51
52
        return $event;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function supportsDenormalization($data, $type, $format = null)
59
    {
60
        return is_a($type, SerializableEvent::class, true);
61
    }
62
}