Serializer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 54
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A deserialize() 0 9 3
A serialize() 0 4 1
A create() 0 13 1
1
<?php
2
3
namespace Bangpound\oEmbed;
4
5
use Symfony\Component\Serializer\Encoder\JsonEncoder;
6
use Symfony\Component\Serializer\Encoder\XmlEncoder;
7
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
8
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
9
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
10
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
11
use Symfony\Component\Serializer\SerializerInterface;
12
13
class Serializer implements SerializerInterface
14
{
15
    /**
16
     * @var SymfonySerializer
17
     */
18
    private $serializer;
19
20
    /**
21
     * @var array
22
     */
23
    private $map = array(
24
        'video' => 'Bangpound\\oEmbed\\Response\\VideoResponse',
25
        'photo' => 'Bangpound\\oEmbed\\Response\\PhotoResponse',
26
        'link' => 'Bangpound\\oEmbed\\Response\\LinkResponse',
27
        'rich' => 'Bangpound\\oEmbed\\Response\\RichResponse',
28
    );
29
30 28
    public function __construct(SymfonySerializer $serializer, array $map = null)
31
    {
32 28
        $this->serializer = $serializer;
33 28
        if (isset($map)) {
34 8
            $this->map = $map;
35 10
        }
36 28
    }
37
38 24
    public function deserialize($data, $type, $format, array $context = array())
39
    {
40 24
        if (!$type) {
41 8
            $temp = $this->serializer->decode($data, $format, $context);
42 8
            $type = isset($this->map[$temp['type']]) ? $this->map[$temp['type']] : 'Bangpound\\oEmbed\\Response\\Response';
43 8
        }
44
45 24
        return $this->serializer->deserialize($data, $type, $format, $context);
46
    }
47
48 16
    public function serialize($data, $format, array $context = array())
49
    {
50 16
        return $this->serializer->serialize($data, $format, $context);
51
    }
52
53 28
    public static function create(array $map = null)
54
    {
55 28
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
56 28
        $serializer = new SymfonySerializer([
57 28
          new PropertyNormalizer(null, $nameConverter),
58 28
          new GetSetMethodNormalizer(null, $nameConverter),
59 28
        ], [
60 28
          new JsonEncoder(),
61 28
          new XmlEncoder('oembed'),
62 28
        ]);
63
64 28
        return new self($serializer, $map);
65
    }
66
}
67