AbstractService::deserialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
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