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

DoctrineDriver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.77%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 61
ccs 30
cts 31
cp 0.9677
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C loadMetadataForClass() 0 45 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
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