EmbedOneFieldNormalizer::normalizeField()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
nc 3
nop 4
dl 0
loc 15
ccs 3
cts 3
cp 1
crap 3
rs 10
c 2
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer\Relation;
6
7
use Chubbyphp\Serialization\Accessor\AccessorInterface;
8
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
9
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
10
use Chubbyphp\Serialization\Normalizer\NormalizerInterface;
11
use Chubbyphp\Serialization\SerializerLogicException;
12
13
final class EmbedOneFieldNormalizer implements FieldNormalizerInterface
14
{
15
    /**
16
     * @var AccessorInterface
17
     */
18
    private $accessor;
19
20
    public function __construct(AccessorInterface $accessor)
21
    {
22
        $this->accessor = $accessor;
23 3
    }
24
25 3
    /**
26 3
     * @param object $object
27
     *
28
     * @throws SerializerLogicException
29
     *
30
     * @return mixed
31
     */
32
    public function normalizeField(
33
        string $path,
34
        $object,
35
        NormalizerContextInterface $context,
36
        NormalizerInterface $normalizer = null
37
    ) {
38 3
        if (null === $normalizer) {
39
            throw SerializerLogicException::createMissingNormalizer($path);
40
        }
41
42
        if (null === $relatedObject = $this->accessor->getValue($object)) {
43
            return null;
44 3
        }
45 1
46
        return $normalizer->normalize($relatedObject, $context, $path);
47
    }
48
}
49