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

EmbedManyFieldNormalizer::normalizeField()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 4
crap 4
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 EmbedManyFieldNormalizer implements FieldNormalizerInterface
15
{
16
    /**
17
     * @var AccessorInterface
18
     */
19
    private $accessor;
20
21
    /**
22
     * @param AccessorInterface $accessor
23
     */
24 5
    public function __construct(AccessorInterface $accessor)
25
    {
26 5
        $this->accessor = $accessor;
27 5
    }
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 5
    public function normalizeField(
40
        string $path,
41
        $object,
42
        NormalizerContextInterface $context,
43
        NormalizerInterface $normalizer = null
44
    ) {
45 5
        if (null === $normalizer) {
46 1
            throw SerializerLogicException::createMissingNormalizer($path);
47
        }
48
49 4
        if (null === $relatedObjects = $this->accessor->getValue($object)) {
50 1
            return null;
51
        }
52
53 3
        $values = [];
54 3
        foreach ($relatedObjects as $i => $relatedObject) {
55 2
            $this->resolveProxy($relatedObject);
56
57 2
            $subPath = $path.'['.$i.']';
58 2
            $values[$i] = $normalizer->normalize($relatedObject, $context, $subPath);
59
        }
60
61 3
        return $values;
62
    }
63
64 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...
65
    {
66 2
        if (null !== $relatedObject && interface_exists('Doctrine\Common\Persistence\Proxy')
67 2
            && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()
68
        ) {
69 1
            $relatedObject->__load();
70
        }
71 2
    }
72
}
73