GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 403b1f...e5b3de )
by Rémi
02:35
created

Serializer::deserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace RemiSan\Serializer;
4
5
use Doctrine\Instantiator\InstantiatorInterface;
6
use RemiSan\Serializer\Hydrator\HydratorFactory;
7
8
class Serializer
9
{
10
    /** @var SerializableClassMapper */
11
    private $classMapper;
12
13
    /** @var HydratorFactory */
14
    private $hydratorFactory;
15
16
    /** @var DataFormatter */
17
    private $dataFormatter;
18
19
    /** @var InstantiatorInterface */
20
    private $instantiator;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param SerializableClassMapper $classMapper
26
     * @param HydratorFactory         $hydratorFactory
27
     * @param DataFormatter           $dataFormatter
28
     * @param InstantiatorInterface   $instantiator
29
     */
30 9
    public function __construct(
31
        SerializableClassMapper $classMapper,
32
        HydratorFactory $hydratorFactory,
33
        DataFormatter $dataFormatter,
34
        InstantiatorInterface $instantiator
35
    ) {
36 9
        $this->classMapper = $classMapper;
37 9
        $this->hydratorFactory = $hydratorFactory;
38 9
        $this->dataFormatter = $dataFormatter;
39 9
        $this->instantiator = $instantiator;
40 9
    }
41
42
    /**
43
     * @param mixed $object
44
     *
45
     * @return array
46
     */
47 6
    public function serialize($object)
48
    {
49 6
        if (!(is_array($object) || is_object($object))) {
50 3
            throw new \InvalidArgumentException();
51
        }
52
53 3
        return $this->innerSerialize($object);
54
    }
55
56
    /**
57
     * @param mixed $object
58
     *
59
     * @return array
60
     */
61 3
    private function innerSerialize($object)
62
    {
63 3
        if (is_array($object)) {
64 3
            $serializedArray = [];
65 3
            foreach ($object as $key => $value) {
66 3
                $serializedArray[$key] = $this->innerSerialize($value);
67 2
            }
68
69 3
            return $serializedArray;
70 3
        } elseif (is_object($object)) {
71 3
            return $this->serializeObject($object);
72
        } else {
73 3
            return $object;
74
        }
75
    }
76
77
    /**
78
     * @param object $object
79
     *
80
     * @return array
81
     */
82 3
    private function serializeObject($object)
83
    {
84 3
        $payload = $this->hydratorFactory->getHydrator(get_class($object))->extract($object);
85
86 3
        $curatedPayload = [];
87 3
        foreach ($payload as $key => $value) {
88 3
            $curatedPayload[$key] = $this->innerSerialize($value);
89 2
        }
90
91 3
        return $this->dataFormatter->format($this->classMapper->extractName(get_class($object)), $curatedPayload);
92
    }
93
94
    /**
95
     * @param array $serializedObject
96
     *
97
     * @return mixed
98
     */
99 3
    public function deserialize(array $serializedObject)
100
    {
101 3
        return $this->recursiveDeserialize($serializedObject);
102
    }
103
104
    /**
105
     * @param mixed $serializedObject
106
     *
107
     * @return mixed
108
     */
109 3
    private function recursiveDeserialize($serializedObject)
110
    {
111 3
        if (!is_array($serializedObject)) {
112 3
            return $serializedObject;
113 3
        } elseif ($this->dataFormatter->isSerializedObject($serializedObject)) {
114 3
            return $this->deserializeObject($serializedObject);
115
        } else {
116 3
            $deserializedArray = [];
117 3
            foreach ($serializedObject as $key => $value) {
118 3
                $deserializedArray[$key] = $this->recursiveDeserialize($value);
119 2
            }
120
121 3
            return $deserializedArray;
122
        }
123
    }
124
125
    /**
126
     * @param array $serializedObject
127
     *
128
     * @return object
129
     */
130 3
    private function deserializeObject(array $serializedObject)
131
    {
132 3
        list($name, $payload) = $this->dataFormatter->getNameAndPayload($serializedObject);
133
134 3
        $curatedPayload = [];
135 3
        foreach ($payload as $key => $value) {
136 3
            $curatedPayload[$key] = $this->recursiveDeserialize($value);
137 2
        }
138
139 3
        $objectFqcn = $this->classMapper->getClassName($name);
140 3
        $object = $this->instantiator->instantiate($objectFqcn);
141
142 3
        return $this->hydratorFactory
143 3
            ->getHydrator($objectFqcn)
144 3
            ->hydrate($curatedPayload, $object);
145
    }
146
}
147