EmbedOneFieldNormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 8
dl 0
loc 34
ccs 6
cts 6
cp 1
rs 10
c 2
b 0
f 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeField() 0 15 3
A __construct() 0 3 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