Test Failed
Push — master ( e430c9...8e0c78 )
by Dominik
01:47
created

Serializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 74
ccs 6
cts 12
cp 0.5
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A serialize() 0 8 1
A normalize() 0 4 1
A getContentTypes() 0 4 1
A encode() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization;
6
7
use Chubbyphp\Serialization\Encoder\EncoderInterface;
8
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
9
use Chubbyphp\Serialization\Normalizer\NormalizerInterface;
10
11
final class Serializer implements SerializerInterface
12
{
13
    /**
14
     * @var NormalizerInterface
15
     */
16
    private $normalizer;
17
18
    /**
19
     * @var EncoderInterface
20
     */
21
    private $encoder;
22
23
    /**
24
     * @param NormalizerInterface $normalizer
25
     * @param EncoderInterface    $encoder
26
     */
27 4
    public function __construct(NormalizerInterface $normalizer, EncoderInterface $encoder)
28
    {
29 4
        $this->normalizer = $normalizer;
30 4
        $this->encoder = $encoder;
31 4
    }
32
33
    /**
34
     * @param object                     $object
35
     * @param string                     $contentType
36
     * @param NormalizerContextInterface $context
37
     * @param string                     $path
38
     *
39
     * @return string
40
     */
41 4
    public function serialize(
42
        $object,
43
        string $contentType,
44
        NormalizerContextInterface $context = null,
45
        $path = ''
46
    ): string {
47 4
        return $this->encoder->encode($this->normalizer->normalize($object, $context, $path), $contentType);
48
    }
49
50
    /**
51
     * @param object                          $object
52
     * @param NormalizerContextInterface|null $context
53
     * @param string                          $path
54
     *
55
     * @return array
56
     *
57
     * @throws SerializerLogicException
58
     */
59
    public function normalize($object, NormalizerContextInterface $context = null, string $path = ''): array
60
    {
61
        return $this->normalizer->normalize($object, $context, $path);
62
    }
63
64
    /**
65
     * @return string[]
66
     */
67
    public function getContentTypes(): array
68
    {
69
        return $this->encoder->getContentTypes();
70
    }
71
72
    /**
73
     * @param array  $data
74
     * @param string $contentType
75
     *
76
     * @return string
77
     *
78
     * @throws SerializerLogicException
79
     */
80
    public function encode(array $data, string $contentType): string
81
    {
82
        return $this->encoder->encode($data, $contentType);
83
    }
84
}
85