DoctrineDriver   A
last analyzed

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 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 61
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B 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
/**
13
 * @author Philip Washington Sorst <[email protected]>
14
 */
15
class DoctrineDriver implements DriverInterface
16
{
17
    /**
18
     * @var EntityManagerInterface
19
     */
20
    private $entityManager;
21
22 90
    public function __construct(EntityManagerInterface $entityManager)
23
    {
24 90
        $this->entityManager = $entityManager;
25 90
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 90
    public function loadMetadataForClass(\ReflectionClass $class)
31
    {
32 90
        $ddrRestClassMetadata = new ClassMetadata($class->getName());
33
        try {
34 90
            $doctrineClassMetadata = $this->entityManager->getClassMetadata($class->getName());
35 10
        } catch (CommonMappingException $e) {
36
            /* If this is not a doctrine entity just generate a fresh metaclass */
37
            return $ddrRestClassMetadata;
38 10
        } catch (OrmMappingException $e) {
39
            /* If this is not a doctrine entity just generate a fresh metaclass */
40 10
            return $ddrRestClassMetadata;
41
        }
42
43 90
        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 90
        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 90
            if (!array_key_exists('declared', $fieldMapping) && !array_key_exists('declaredField', $fieldMapping)) {
51 90
                $ddrRestPropertyMetadata = new PropertyMetadata(
52 90
                    $doctrineClassMetadata->getName(),
53 90
                    $fieldMapping['fieldName']
54
                );
55 90
                $ddrRestPropertyMetadata->setType($fieldMapping['type']);
56 90
                $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
57
            }
58
        }
59
60 90
        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 74
            $ddrRestPropertyMetadata = new PropertyMetadata(
62 74
                $doctrineClassMetadata->getName(),
63 74
                $associationMapping['fieldName']
64
            );
65 74
            $ddrRestPropertyMetadata->setAssociation(true);
66 74
            $ddrRestPropertyMetadata->setType($associationMapping['targetEntity']);
67 74
            $ddrRestPropertyMetadata->setCollection(
68 74
                $doctrineClassMetadata->isCollectionValuedAssociation($associationMapping['fieldName'])
69
            );
70 74
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
71
        }
72
73 90
        return $ddrRestClassMetadata;
74
    }
75
}
76