Completed
Push — master ( 607289...edbd19 )
by Pavel
01:31
created

AssociationNormalizer::denormalize()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 3
crap 30
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\DoctrineOrm;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
9
final class AssociationNormalizer
10
{
11
    /** @var  ManagerRegistry */
12
    private $registry;
13
14
    /**
15
     * AssociationAccessor constructor.
16
     *
17
     * @param ManagerRegistry $registry
18
     */
19 6
    public function __construct(ManagerRegistry $registry)
20
    {
21 6
        $this->registry = $registry;
22 6
    }
23
24 5
    public function normalize($rawValue, $object, $attribute)
25
    {
26 5
        $normalizer = new EntityToIdNormalizer($this->registry);
27
28 5
        if (null === $rawValue) {
29 3
            return null;
30
        }
31
32 5
        $metadata = $this->getObjectMetadata($object, $attribute);
33 5
        if (null === $metadata) {
34 5
            return $rawValue;
35
        }
36
37 5
        if ($metadata->isSingleValuedAssociation($attribute)) {
38 4
            return $normalizer->normalize($rawValue);
39
        }
40
41 5
        if ($rawValue instanceof \Traversable) {
42 5
            $rawValue = iterator_to_array($rawValue);
43 5
        }
44
45 5
        return array_map([$normalizer, 'normalize'], $rawValue);
46
    }
47
48
    public function denormalize($object, $attribute, $value)
49
    {
50
        if (null === $value) {
51
            return null;
52
        }
53
54
        $normalizer = new EntityToIdNormalizer($this->registry);
55
        $metadata   = $this->getObjectMetadata($object, $attribute);
56
57
        if (null === $metadata) {
58
            return $value;
59
        }
60
61
        if ($metadata->isSingleValuedAssociation($attribute)) {
62
            return $normalizer->denormalize($value, $metadata->getAssociationTargetClass($attribute));
63
        }
64
65
        $objects = new ArrayCollection();
66
        foreach ($value as $item) {
67
            $objects->add($normalizer->denormalize($item, $metadata->getAssociationTargetClass($attribute)));
68
        }
69
70
        return $objects;
71
    }
72
73
    /**]
74
     * @param $object
75
     * @param $attribute
76
     *
77
     * @return ClassMetadata|null
78
     */
79 5
    private function getObjectMetadata($object, $attribute)
80
    {
81 5
        $class = get_class($object);
82
83 5
        $manager = $this->registry->getManagerForClass($class);
84 5
        if (null === $manager) {
85
            return null;
86
        }
87
88 5
        $metadata = $manager->getClassMetadata($class);
89 5
        if (!$metadata->hasAssociation($attribute)) {
90 5
            return null;
91
        }
92
93 5
        return $metadata;
94
    }
95
}
96