Completed
Push — master ( a2ccb4...2afc29 )
by Philip
05:08
created

DoctrineDriver::loadMetadataForClass()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 8.0029

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 45
ccs 27
cts 28
cp 0.9643
rs 5.3846
c 2
b 0
f 0
cc 8
eloc 29
nc 14
nop 1
crap 8.0029
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata\Driver;
4
5
use Doctrine\Common\Persistence\Mapping\MappingException as CommonMappingException;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
8
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
9
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
10
use Metadata\Driver\DriverInterface;
11
12
class DoctrineDriver implements DriverInterface
13
{
14
    /**
15
     * @var EntityManagerInterface
16
     */
17
    private $entityManager;
18
19 88
    public function __construct(EntityManagerInterface $entityManager)
20
    {
21 88
        $this->entityManager = $entityManager;
22 88
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 88
    public function loadMetadataForClass(\ReflectionClass $class)
28
    {
29 88
        $ddrRestClassMetadata = new ClassMetadata($class->getName());
30
        try {
31 88
            $doctrineClassMetadata = $this->entityManager->getClassMetadata($class->getName());
32 10
        } catch (CommonMappingException $e) {
33
            /* If this is not a doctrine entity just generate a fresh metaclass */
34
            return $ddrRestClassMetadata;
35 10
        } catch (OrmMappingException $e) {
36
            /* If this is not a doctrine entity just generate a fresh metaclass */
37 10
            return $ddrRestClassMetadata;
38
        }
39
40 88
        foreach ($doctrineClassMetadata->embeddedClasses as $fieldName => $embeddedClass) {
0 ignored issues
show
Bug introduced by
Accessing embeddedClasses on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
41 42
            $ddrRestPropertyMetadata = new PropertyMetadata($doctrineClassMetadata->getName(), $fieldName);
42 42
            $ddrRestPropertyMetadata->setType($embeddedClass['class']);
43 42
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
44
        }
45
46 88
        foreach ($doctrineClassMetadata->fieldMappings as $fieldMapping) {
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
47 88
            if (!array_key_exists('declared', $fieldMapping) && !array_key_exists('declaredField', $fieldMapping)) {
48 88
                $ddrRestPropertyMetadata = new PropertyMetadata(
49 88
                    $doctrineClassMetadata->getName(),
50 88
                    $fieldMapping['fieldName']
51
                );
52 88
                $ddrRestPropertyMetadata->setType($fieldMapping['type']);
53 88
                $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
54
            }
55
        }
56
57 88
        foreach ($doctrineClassMetadata->associationMappings as $associationMapping) {
0 ignored issues
show
Bug introduced by
Accessing associationMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
58 72
            $ddrRestPropertyMetadata = new PropertyMetadata(
59 72
                $doctrineClassMetadata->getName(),
60 72
                $associationMapping['fieldName']
61
            );
62 72
            $ddrRestPropertyMetadata->setAssociation(true);
63 72
            $ddrRestPropertyMetadata->setType($associationMapping['targetEntity']);
64 72
            $ddrRestPropertyMetadata->setCollection(
65 72
                $doctrineClassMetadata->isCollectionValuedAssociation($associationMapping['fieldName'])
66
            );
67 72
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
68
        }
69
70 88
        return $ddrRestClassMetadata;
71
    }
72
}
73