Completed
Push — master ( 0e7db1...c18576 )
by Tomasz
12s
created

Serializard::getNormalizerContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 3
crap 2
1
<?php
2
namespace Thunder\Serializard;
3
4
use Thunder\Serializard\Format\FormatInterface;
5
use Thunder\Serializard\FormatContainer\FormatContainerInterface as Formats;
6
use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators;
7
use Thunder\Serializard\NormalizerContainer\NormalizerContainerInterface as Normalizers;
8
use Thunder\Serializard\NormalizerContext\NormalizerContextInterface;
9
use Thunder\Serializard\NormalizerContext\ParentNormalizerContext;
10
11
/**
12
 * @author Tomasz Kowalczyk <[email protected]>
13
 */
14
final class Serializard
15
{
16
    private $normalizers;
17
    private $hydrators;
18
    /** @var Formats */
19
    private $formats;
20
21 13
    public function __construct(Formats $formats, Normalizers $normalizers, Hydrators $hydrators)
22
    {
23 13
        $this->normalizers = $normalizers;
24 13
        $this->hydrators = $hydrators;
25 13
        $this->formats = $formats;
26 13
    }
27
28 12
    public function serialize($var, $format, NormalizerContextInterface $context = null)
29
    {
30 12
        return $this->getFormat($format)->serialize($var, $this->normalizers, $this->getNormalizerContext($context, $var, $format));
31
    }
32
33 4
    public function unserialize($var, $class, $format)
34
    {
35 4
        return $this->getFormat($format)->unserialize($var, $class, $this->hydrators);
36
    }
37
38 11
    private function getNormalizerContext(NormalizerContextInterface $context = null, $var, $format)
39
    {
40 11
        $context = $context ?: new ParentNormalizerContext();
41
42 11
        return $context->withRoot($var)->withFormat($format);
43
    }
44
45 13 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...
46
    {
47 13
        $format = $this->formats->get($alias);
48
49 13
        if(false === $format instanceof FormatInterface) {
50 2
            throw new \RuntimeException(sprintf('No registered format for alias %s!', $alias));
51
        }
52
53 11
        return $format;
54
    }
55
}
56