Passed
Push — master ( 96a906...adf096 )
by Dominik
02:00
created

EmbedOneFieldNormalizer::normalizeField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 4
crap 12
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 View Code Duplication
final class EmbedOneFieldNormalizer implements FieldNormalizerInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /**
16
     * @var AccessorInterface
17
     */
18
    private $accessor;
19
20
    /**
21
     * @param AccessorInterface $accessor
22
     */
23
    public function __construct(AccessorInterface $accessor)
24
    {
25
        $this->accessor = $accessor;
26
    }
27
28
    /**
29
     * @param string                     $path
30
     * @param object                     $object
31
     * @param NormalizerContextInterface $context
32
     * @param NormalizerInterface|null   $normalizer
33
     *
34
     * @return mixed
35
     *
36
     * @throws SerializerLogicException
37
     */
38
    public function normalizeField(
39
        string $path,
40
        $object,
41
        NormalizerContextInterface $context,
42
        NormalizerInterface $normalizer = null
43
    ) {
44
        if (null === $normalizer) {
45
            throw SerializerLogicException::createMissingNormalizer($path);
46
        }
47
48
        if (null === $childObject = $this->accessor->getValue($object)) {
49
            return null;
50
        }
51
52
        return $normalizer->normalize($childObject, $context, $path);
53
    }
54
}
55