Completed
Push — master ( 98c39c...c8155d )
by Pavel
16:50
created

AssociationNormalizer::getObjectMetadata()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.512

Importance

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