onSerializerPreDeserialize()   D
last analyzed

Complexity

Conditions 10
Paths 16

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 16.4

Importance

Changes 6
Bugs 2 Features 3
Metric Value
c 6
b 2
f 3
dl 0
loc 35
ccs 18
cts 30
cp 0.6
rs 4.8196
cc 10
eloc 23
nc 16
nop 1
crap 16.4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tpg\ExtjsBundle\Listener;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\ORM\Mapping\JoinColumn;
7
use JMS\Serializer\EventDispatcher\PreDeserializeEvent;
8
use JMS\Serializer\Metadata\PropertyMetadata;
9
10
class DeserializationAssociateRelationship {
11
12
    /** @var  AnnotationReader */
13
    protected $reader;
14
15 30
    public function __construct($reader) {
16 30
        $this->reader = $reader;
17 30
    }
18
19 30
    public function onSerializerPreDeserialize(PreDeserializeEvent $e) {
20 30
        $className = $e->getType();
21 30
        $className = $className['name'];
22
        /** Handle ArrayCollection or array JMS type. */
23 30
        if ($className == "ArrayCollection" || $className == "array") {
24 5
            $className = $e->getType();
25 5
            $className = $className['params'][0]['name'];
26 5
        }
27 30
        $classMetadata = $e->getContext()->getMetadataFactory()->getMetadataForClass($className);
28
        /** @var PropertyMetadata $propertyMetadata */
29 30
        foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
30 30
            if ($propertyMetadata->reflection === null) continue;
31
            /** @var JoinColumn $joinColumnAnnotation */
32 30
            $joinColumnAnnotation = $this->reader->getPropertyAnnotation(
33 30
                $propertyMetadata->reflection,
34
                'Doctrine\ORM\Mapping\JoinColumn'
35 30
            );
36 30
            if ($joinColumnAnnotation !== null) {
37 15
                if (array_key_exists($joinColumnAnnotation->name, $e->getData())) {
38
                    $idValue = $e->getData();
39
                    $idValue = $idValue[$joinColumnAnnotation->name];
40
                    $referencedColumnName = $joinColumnAnnotation->referencedColumnName ?: 'id';
41
                    if ($idValue !== null && $idValue !== '') {
42
                        $e->setData(
43
                            $e->getData() + array($propertyMetadata->name => array($referencedColumnName => $idValue))
44
                        );
45
                    } else {
46
                        $e->setData(
47
                            $e->getData() + array($propertyMetadata->name => null)
48
                        );
49
                    }
50
                }
51 15
            }
52 30
        }
53
    }
54
}