Completed
Push — master ( 7912f2...809b2b )
by Dominik
07:10
created

EmbedOneFieldNormalizer::normalizeField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 4
crap 3
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
use Doctrine\Common\Persistence\Proxy;
13
14
final class EmbedOneFieldNormalizer implements FieldNormalizerInterface
15
{
16
    /**
17
     * @var AccessorInterface
18
     */
19
    private $accessor;
20
21
    /**
22
     * @param AccessorInterface $accessor
23
     */
24 4
    public function __construct(AccessorInterface $accessor)
25
    {
26 4
        $this->accessor = $accessor;
27 4
    }
28
29
    /**
30
     * @param string                     $path
31
     * @param object                     $object
32
     * @param NormalizerContextInterface $context
33
     * @param NormalizerInterface|null   $normalizer
34
     *
35
     * @return mixed
36
     *
37
     * @throws SerializerLogicException
38
     */
39 4
    public function normalizeField(
40
        string $path,
41
        $object,
42
        NormalizerContextInterface $context,
43
        NormalizerInterface $normalizer = null
44
    ) {
45 4
        if (null === $normalizer) {
46 1
            throw SerializerLogicException::createMissingNormalizer($path);
47
        }
48
49 3
        if (null === $relatedObject = $this->accessor->getValue($object)) {
50 1
            return null;
51
        }
52
53 2
        $this->resolveProxy($relatedObject);
54
55 2
        return $normalizer->normalize($relatedObject, $context, $path);
56
    }
57
58 2 View Code Duplication
    private function resolveProxy($relatedObject)
0 ignored issues
show
Duplication introduced by
This method 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...
59
    {
60 2
        if (null !== $relatedObject && interface_exists('Doctrine\Common\Persistence\Proxy')
61 2
            && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()
62
        ) {
63 1
            $relatedObject->__load();
64
        }
65 2
    }
66
}
67