Completed
Push — master ( 2afc29...29656f )
by Philip
06:07
created

DoctrineDriver::loadMetadataForClass()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 45
ccs 28
cts 28
cp 1
rs 5.3846
c 2
b 0
f 0
cc 8
eloc 29
nc 14
nop 1
crap 8
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
/**
13
 * @author Philip Washington Sorst <[email protected]>
14
 */
15
class DoctrineDriver implements DriverInterface
16
{
17
    /**
18
     * @var EntityManagerInterface
19
     */
20
    private $entityManager;
21
22 100
    public function __construct(EntityManagerInterface $entityManager)
23
    {
24 100
        $this->entityManager = $entityManager;
25 100
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 92
    public function loadMetadataForClass(\ReflectionClass $class)
31
    {
32 92
        $ddrRestClassMetadata = new ClassMetadata($class->getName());
33
        try {
34 92
            $doctrineClassMetadata = $this->entityManager->getClassMetadata($class->getName());
35 12
        } catch (CommonMappingException $e) {
36
            /* If this is not a doctrine entity just generate a fresh metaclass */
37 2
            return $ddrRestClassMetadata;
38 12
        } catch (OrmMappingException $e) {
39
            /* If this is not a doctrine entity just generate a fresh metaclass */
40 12
            return $ddrRestClassMetadata;
41
        }
42
43 92
        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...
44 44
            $ddrRestPropertyMetadata = new PropertyMetadata($doctrineClassMetadata->getName(), $fieldName);
45 44
            $ddrRestPropertyMetadata->setType($embeddedClass['class']);
46 44
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
47
        }
48
49 92
        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...
50 92
            if (!array_key_exists('declared', $fieldMapping) && !array_key_exists('declaredField', $fieldMapping)) {
51 92
                $ddrRestPropertyMetadata = new PropertyMetadata(
52 92
                    $doctrineClassMetadata->getName(),
53 92
                    $fieldMapping['fieldName']
54
                );
55 92
                $ddrRestPropertyMetadata->setType($fieldMapping['type']);
56 92
                $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
57
            }
58
        }
59
60 92
        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...
61 76
            $ddrRestPropertyMetadata = new PropertyMetadata(
62 76
                $doctrineClassMetadata->getName(),
63 76
                $associationMapping['fieldName']
64
            );
65 76
            $ddrRestPropertyMetadata->setAssociation(true);
66 76
            $ddrRestPropertyMetadata->setType($associationMapping['targetEntity']);
67 76
            $ddrRestPropertyMetadata->setCollection(
68 76
                $doctrineClassMetadata->isCollectionValuedAssociation($associationMapping['fieldName'])
69
            );
70 76
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
71
        }
72
73 92
        return $ddrRestClassMetadata;
74
    }
75
}
76