Completed
Push — standalone ( 8da518...787e4c )
by Philip
09:19
created

DoctrineDriver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 57
ccs 29
cts 29
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C loadMetadataForClass() 0 41 7
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata\Driver;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Mapping\MappingException;
7
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
8
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
9
use Metadata\Driver\DriverInterface;
10
11
class DoctrineDriver implements DriverInterface
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $entityManager;
17
18 38
    function __construct(EntityManagerInterface $entityManager)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20 38
        $this->entityManager = $entityManager;
21 38
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 38
    public function loadMetadataForClass(\ReflectionClass $class)
27
    {
28 38
        $ddrRestClassMetadata = new ClassMetadata($class->getName());
29
        try {
30 38
            $doctrineClassMetadata = $this->entityManager->getClassMetadata($class->getName());
31 2
        } catch (MappingException $e) {
32 2
            return $ddrRestClassMetadata;
33
        }
34
35 38
        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...
36 24
            $ddrRestPropertyMetadata = new PropertyMetadata($doctrineClassMetadata->getName(), $fieldName);
37 24
            $ddrRestPropertyMetadata->setType($embeddedClass['class']);
38 24
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
39
        }
40
41 38
        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...
42 38
            if (!array_key_exists('declared', $fieldMapping) && !array_key_exists('declaredField', $fieldMapping)) {
43 38
                $ddrRestPropertyMetadata = new PropertyMetadata(
44 38
                    $doctrineClassMetadata->getName(),
45 38
                    $fieldMapping['fieldName']
46
                );
47 38
                $ddrRestPropertyMetadata->setType($fieldMapping['type']);
48 38
                $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
49
            }
50
        }
51
52 38
        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...
53 26
            $ddrRestPropertyMetadata = new PropertyMetadata(
54 26
                $doctrineClassMetadata->getName(),
55 26
                $associationMapping['fieldName']
56
            );
57 26
            $ddrRestPropertyMetadata->setAssociation(true);
58 26
            $ddrRestPropertyMetadata->setType($associationMapping['targetEntity']);
59 26
            $ddrRestPropertyMetadata->setCollection(
60 26
                $doctrineClassMetadata->isCollectionValuedAssociation($associationMapping['fieldName'])
61
            );
62 26
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
63
        }
64
65 38
        return $ddrRestClassMetadata;
66
    }
67
}
68