ReferenceOneFieldNormalizer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
dl 0
loc 36
ccs 5
cts 5
cp 1
rs 10
c 1
b 0
f 1
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeField() 0 11 2
A __construct() 0 4 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 ReferenceOneFieldNormalizer implements FieldNormalizerInterface
14
{
15
    /**
16
     * @var AccessorInterface
17
     */
18
    private $identifierAccessor;
19
20
    /**
21
     * @var AccessorInterface
22
     */
23
    private $accessor;
24
25
    public function __construct(AccessorInterface $identifierAccessor, AccessorInterface $accessor)
26
    {
27
        $this->identifierAccessor = $identifierAccessor;
28
        $this->accessor = $accessor;
29 2
    }
30
31 2
    /**
32 2
     * @param object $object
33 2
     *
34
     * @throws SerializerLogicException
35
     *
36
     * @return mixed
37
     */
38
    public function normalizeField(
39
        string $path,
40
        $object,
41
        NormalizerContextInterface $context,
42
        NormalizerInterface $normalizer = null
43
    ) {
44
        if (null === $relatedObject = $this->accessor->getValue($object)) {
45 2
            return null;
46
        }
47
48
        return $this->identifierAccessor->getValue($relatedObject);
49
    }
50
}
51