Passed
Push — master ( edadc9...bfe05e )
by Dominik
02:02
created

ReferenceOneFieldNormalizer::normalizeField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 4
crap 2
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\NormalizerContextInterface;
9
use Chubbyphp\Serialization\Normalizer\NormalizerInterface;
10
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
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
    /**
26
     * @param AccessorInterface $identifierAccessor
27
     * @param AccessorInterface $accessor
28
     */
29 2
    public function __construct(AccessorInterface $identifierAccessor, AccessorInterface $accessor)
30
    {
31 2
        $this->identifierAccessor = $identifierAccessor;
32 2
        $this->accessor = $accessor;
33 2
    }
34
35
    /**
36
     * @param string                     $path
37
     * @param object                     $object
38
     * @param NormalizerContextInterface $context
39
     * @param NormalizerInterface|null   $normalizer
40
     *
41
     * @return mixed
42
     *
43
     * @throws SerializerLogicException
44
     */
45 2
    public function normalizeField(
46
        string $path,
47
        $object,
48
        NormalizerContextInterface $context,
49
        NormalizerInterface $normalizer = null
50
    ) {
51 2
        if (null === $childObject = $this->accessor->getValue($object)) {
52 1
            return null;
53
        }
54
55 1
        return $this->identifierAccessor->getValue($childObject);
56
    }
57
}
58