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

ReferenceManyFieldNormalizer::resolveProxy()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 5

Importance

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