AbstractService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 4 1
A deserialize() 0 11 2
1
<?php
2
/*
3
 * This file is part of the Adlogix package.
4
 *
5
 * (c) Allan Segebarth <[email protected]>
6
 * (c) Jean-Jacques Courtens <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Adlogix\ConfluenceClient\Service;
13
14
use JMS\Serializer\DeserializationContext;
15
use JMS\Serializer\SerializationContext;
16
use JMS\Serializer\SerializerInterface;
17
18
/**
19
 * Class AbstractService
20
 * @package Adlogix\ConfluenceClient\Service
21
 * @author  Cedric Michaux <[email protected]>
22
 */
23
class AbstractService implements ServiceInterface
24
{
25
    /**
26
     * @var SerializerInterface
27
     */
28
    protected $serializer;
29
30
    /**
31
     * AbstractService constructor.
32
     *
33
     * @param SerializerInterface $serializer
34
     */
35
    public function __construct(SerializerInterface $serializer)
36
    {
37
        $this->serializer = $serializer;
38
    }
39
40
41
    /**
42
     * @param mixed                $entity
43
     * @param SerializationContext $context
44
     *
45
     * @return string
46
     */
47
    protected function serialize($entity, SerializationContext $context = null)
48
    {
49
        return $this->serializer->serialize($entity, 'json', $context);
50
    }
51
52
    /**
53
     * @param string                 $json
54
     * @param string                 $type
55
     * @param DeserializationContext $context
56
     *
57
     * @return object|null
58
     */
59
    protected function deserialize($json, $type, DeserializationContext $context = null)
60
    {
61
62
        $object = $this->serializer->deserialize($json, $type, 'json', $context);
63
64
        if(!$object instanceof $type){
65
            return null;
66
        }
67
68
        return $object;
69
    }
70
71
}
72