Completed
Push — master ( 2e0cd6...c623e8 )
by Pavel
12:31
created

JmsDoctrineMetadataDriver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 48
ccs 22
cts 25
cp 0.88
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B loadMetadataForClass() 0 24 4
A setPropertyType() 0 20 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata as DoctrineClassMetadata;
6
use JMS\Serializer\Metadata\ClassMetadata;
7
use JMS\Serializer\Metadata\Driver\AbstractDoctrineTypeDriver;
8
use JMS\Serializer\Metadata\PropertyMetadata;
9
10
final class JmsDoctrineMetadataDriver extends AbstractDoctrineTypeDriver
11
{
12 2
    public function loadMetadataForClass(\ReflectionClass $class)
13
    {
14
        /** @var $classMetadata ClassMetadata */
15 2
        $classMetadata = $this->delegate->loadMetadataForClass($class);
16
17
        // Abort if the given class is not a mapped entity
18 2
        if ( ! $doctrineMetadata = $this->tryLoadingDoctrineMetadata($class->name)) {
19
            return $classMetadata;
20
        }
21
22 2
        $this->setDiscriminator($doctrineMetadata, $classMetadata);
23
24
        // We base our scan on the internal driver's property list so that we
25
        // respect any internal white/blacklisting like in the AnnotationDriver
26 2
        foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) {
27 2
            if ($this->hideProperty($doctrineMetadata, $propertyMetadata)) {
28
                unset($classMetadata->propertyMetadata[$key]);
29
            }
30
31 2
            $this->setPropertyType($doctrineMetadata, $propertyMetadata);
32 2
        }
33
34 2
        return $classMetadata;
35
    }
36
37 2
    protected function setPropertyType(DoctrineClassMetadata $doctrineMetadata, PropertyMetadata $propertyMetadata)
38
    {
39 2
        parent::setPropertyType($doctrineMetadata, $propertyMetadata);
40
41 2
        if (!$doctrineMetadata->hasAssociation($propertyMetadata->name)) {
42 2
            return;
43
        }
44
45 2
        $template = '%s<%s>';
46
47
        // Makes the My\Ns\TargetEntity be DoctrineAssociation<My\Ns\TargetEntity>
48
        // And ArrayCollection<My\Ns\TargetEntity> be DoctrineAssociation<ArrayCollection<My\Ns\TargetEntity>>
49 2
        $propertyMetadata->setType(
50 2
            sprintf(
51 2
                $template,
52 2
                JmsDoctrineHandler::TYPE,
53 2
                $propertyMetadata->type['name']
54 2
            )
55 2
        );
56 2
    }
57
}
58