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

SerializardFacade::getFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 10
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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