Completed
Push — master ( 06e52a...98c39c )
by Pavel
13:39 queued 10:37
created

AssociationNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public function __construct(ManagerRegistry $registry)
20
    {
21
        $this->registry = $registry;
22
    }
23
24
    public function normalize($rawValue, $object, $attribute)
25
    {
26
        $normalizer = new EntityToIdNormalizer($this->registry);
27
28
        if (null === $rawValue) {
29
            return null;
30
        }
31
32
        $metadata = $this->getObjectMetadata($object, $attribute);
33
        if (null === $metadata) {
34
            return $rawValue;
35
        }
36
37
        if ($metadata->isSingleValuedAssociation($attribute)) {
38
            return $normalizer->normalize($rawValue);
39
        }
40
41
        if ($rawValue instanceof \Traversable) {
42
            $rawValue = iterator_to_array($rawValue);
43
        }
44
45
        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
    private function getObjectMetadata($object, $attribute)
80
    {
81
        $class = get_class($object);
82
83
        $manager = $this->registry->getManagerForClass($class);
84
        if (null === $manager) {
85
            return null;
86
        }
87
88
        $metadata = $manager->getClassMetadata($class);
89
        if (!$metadata->hasAssociation($attribute)) {
90
            return null;
91
        }
92
93
        return $metadata;
94
    }
95
}
96