MajoraSerializer::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Majora\Framework\Serializer;
4
5
use Majora\Framework\Serializer\Handler\FormatHandlerInterface;
6
use Symfony\Component\Serializer\SerializerInterface;
7
8
/**
9
 * Base class for fixtures repository.
10
 *
11
 * @group legacy
12
 */
13
class MajoraSerializer implements SerializerInterface
14
{
15
    /**
16
     * FormatHandlerInterface[].
17
     */
18
    protected $handlers;
19
20
    /**
21
     * construct.
22
     *
23
     * @param FormatHandlerInterface[] $handlers
24
     */
25 10
    public function __construct(array $handlers)
26
    {
27 10
        $this->handlers = array();
28 10
        foreach ($handlers as $alias => $handler) {
29 6
            $this->registerFormatHandler($alias, $handler);
30 5
        }
31 10
    }
32
33
    /**
34
     * Register a format handler under given alias
35
     *
36
     * @param string                 $alias
37
     * @param FormatHandlerInterface $handler
38
     */
39 6
    public function registerFormatHandler($alias, FormatHandlerInterface $handler)
40
    {
41 6
        $this->handlers[$alias] = $handler;
42 6
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @see SerializerInterface::serialize()
48
     */
49 6
    public function serialize($data, $format, array $context = array())
50
    {
51 6 View Code Duplication
        if (!isset($this->handlers[$format])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52 2
            throw new \BadMethodCallException(sprintf(
53 2
                'Unsupported format "%s", only [%s] are',
54 2
                $format, implode(', ', array_keys($this->handlers))
55 1
            ));
56
        }
57
58 4
        return $this->handlers[$format]->serialize(
59 2
            $data,
60 4
            empty($context['scope']) ? 'default' : $context['scope']
61 2
        );
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @see SerializerInterface::deserialize()
68
     */
69 4
    public function deserialize($data, $type, $format, array $context = array())
70
    {
71 4 View Code Duplication
        if (!isset($this->handlers[$format])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72 2
            throw new \BadMethodCallException(sprintf(
73 2
                'Unsupported format "%s", only [%s] are',
74 2
                $format, implode(', ', array_keys($this->handlers))
75 1
            ));
76
        }
77
78 2
        return $this->handlers[$format]->deserialize($data, $type);
79
    }
80
}
81