Test Failed
Push — master ( 25b8d1...f70cb8 )
by Pavel
03:49
created

RelationAwareProcessorDecorator::updateEntity()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 4
nop 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\Doctrine;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use ScayTrase\Api\Cruds\EntityProcessorInterface;
7
use Symfony\Bridge\Doctrine\RegistryInterface;
8
9
final class RelationAwareProcessorDecorator implements EntityProcessorInterface
10
{
11
    /** @var  RegistryInterface */
12
    private $registry;
13
    /** @var  EntityProcessorInterface */
14
    private $processor;
15
16
    /**
17
     * RelationAwareProcessorDecorator constructor.
18
     *
19
     * @param EntityProcessorInterface $processor
20
     * @param RegistryInterface        $registry
21
     */
22
    public function __construct(EntityProcessorInterface $processor, RegistryInterface $registry)
23
    {
24
        $this->registry  = $registry;
25
        $this->processor = $processor;
26
    }
27
28
    /** {@inheritdoc} */
29
    public function updateEntity($entity, $data)
30
    {
31
        $class = get_class($entity);
32
33
        $manager  = $this->registry->getManagerForClass($class);
34
        $metadata = $manager->getClassMetadata($class);
35
36
        foreach ($data as $property => &$value) {
37
            if ($metadata->hasAssociation($property)) {
38
                $assoc = $metadata->getAssociationTargetClass($property);
39
40
                if ($manager instanceof EntityManagerInterface) {
41
                    $value = $manager->getReference($assoc, $value);
42
                } else {
43
                    $value = $manager->find($assoc, $value);
44
                }
45
            }
46
        }
47
48
        return $this->processor->updateEntity($entity, $data);
49
    }
50
}
51