Completed
Push — master ( 5ba1dc...b85ec4 )
by Tomasz
02:09
created

SerializardFacade   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 17.24 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 8
dl 10
loc 58
ccs 28
cts 28
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A addFormat() 0 4 1
A addNormalizer() 0 4 1
A addHydrator() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1
A getFormat() 10 10 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
namespace Thunder\Serializard;
3
4
use Thunder\Serializard\Format\ArrayFormat;
5
use Thunder\Serializard\Format\FormatInterface;
6
use Thunder\Serializard\Format\JsonFormat;
7
use Thunder\Serializard\Format\XmlFormat;
8
use Thunder\Serializard\Format\YamlFormat;
9
use Thunder\Serializard\FormatContainer\FormatContainer;
10
use Thunder\Serializard\HydratorContainer\HydratorContainer;
11
use Thunder\Serializard\NormalizerContainer\NormalizerContainer;
12
13
/**
14
 * @author Tomasz Kowalczyk <[email protected]>
15
 */
16
final class SerializardFacade
17
{
18
    /** @var NormalizerContainer */
19
    private $normalizers;
20
    /** @var HydratorContainer */
21
    private $hydrators;
22
    /** @var FormatContainer */
23
    private $formats;
24
25 2
    public function __construct()
26
    {
27 2
        $this->normalizers = new NormalizerContainer();
28 2
        $this->hydrators = new HydratorContainer();
29
30 2
        $formats = new FormatContainer();
31 2
        $formats->add('json', new JsonFormat());
32 2
        $formats->add('array', new ArrayFormat());
33 2
        $formats->add('yaml', new YamlFormat());
34 2
        $formats->add('xml', new XmlFormat());
35 2
        $this->formats = $formats;
36 2
    }
37
38 1
    public function addFormat($alias, FormatInterface $format)
39
    {
40 1
        $this->formats->add($alias, $format);
41 1
    }
42
43 1
    public function addNormalizer($class, $root, $handler)
44
    {
45 1
        $this->normalizers->add($class, $root, $handler);
46 1
    }
47
48 1
    public function addHydrator($class, $root, $handler)
49
    {
50 1
        $this->hydrators->add($class, $root, $handler);
51 1
    }
52
53 2
    public function serialize($var, $format)
54
    {
55 2
        return $this->getFormat($format)->serialize($var, $this->normalizers);
56
    }
57
58 1
    public function unserialize($var, $class, $format)
59
    {
60 1
        return $this->getFormat($format)->unserialize($var, $class, $this->hydrators);
61
    }
62
63 2 View Code Duplication
    private function getFormat($alias)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
64
    {
65 2
        $format = $this->formats->get($alias);
66
67 2
        if(false === $format instanceof FormatInterface) {
68 1
            throw new \RuntimeException(sprintf('No registered format for alias %s!', $alias));
69
        }
70
71 1
        return $format;
72
    }
73
}
74