SerializerTrait::deserialize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LibraryCatalog\Service\Repository;
6
7
use LibraryCatalog\Transformer\Serializer;
8
9
trait SerializerTrait
10
{
11
    /** @var Serializer */
12
    protected Serializer $serializer;
13
14
    /**
15
     * @param $data
16
     * @param string $classname
17
     * @return object|null
18
     * @throws Serializer\Exception
19
     * @throws Serializer\HydrateException
20
     * @throws \LibraryCatalog\Transformer\Encoder\Exception
21
     */
22 12
    protected function deserialize($data, string $classname): ?object
23
    {
24 12
        $object = null;
25
26 12
        if ($data != '') {
27 6
            $object = $this->serializer->deserialize((string)$data);
28
29 6
            if (!($object instanceof $classname)) {
30
                // Do not generate error, just ignore not correct value.
31
                $object = null;
32
            }
33
        }
34
35 12
        return $object;
36
    }
37
}
38