MajoraSerializer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 17.65 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 0
dl 12
loc 68
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A registerFormatHandler() 0 4 1
A serialize() 6 14 3
A deserialize() 6 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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