Completed
Push — master ( d466fc...57558f )
by Pavel
03:56
created

EntityMetadataFactory   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 197
Duplicated Lines 9.14 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 80.82%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 28
c 2
b 1
f 0
lcom 2
cbo 6
dl 18
loc 197
ccs 59
cts 73
cp 0.8082
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getFqcnFromAlias() 0 8 2
A wakeupReflection() 0 9 2
A initializeReflection() 0 9 2
A isEntity() 0 4 1
B doLoadMetadata() 0 24 4
B addInheritedFields() 9 15 6
A registerAlias() 0 8 2
A setEntityManager() 0 4 1
A initialize() 0 5 1
B addInheritedRelations() 9 12 5
A getDriver() 0 4 1
A newClassMetadataInstance() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bankiru\Api\Doctrine;
4
5
use Bankiru\Api\Doctrine\Exception\MappingException;
6
use Bankiru\Api\Doctrine\Mapping\EntityMetadata;
7
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
9
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
10
use Doctrine\Common\Persistence\Mapping\ReflectionService;
11
use ReflectionException;
12
13
class EntityMetadataFactory extends AbstractClassMetadataFactory
14
{
15
    /** @var  EntityManager */
16
    private $manager;
17
    /** @var  MappingDriver */
18
    private $driver;
19
20
    /** @var string[] */
21
    private $aliases = [];
22
23
    public function registerAlias($namespaceAlias, $namespace)
24
    {
25
        if (array_key_exists($namespaceAlias, $this->aliases)) {
26
            throw new \LogicException(sprintf('Alias "%s" is already registered', $namespaceAlias));
27
        }
28
29
        $this->aliases[$namespaceAlias] = rtrim($namespace, '\\');
30
    }
31
32
    /**
33
     * @param EntityManager $manager
34
     */
35 14
    public function setEntityManager($manager)
36
    {
37 14
        $this->manager = $manager;
38 14
    }
39
40
41
    /** {@inheritdoc} */
42 14
    protected function initialize()
43
    {
44 14
        $this->driver      = $this->manager->getConfiguration()->getDriver();
45 14
        $this->initialized = true;
46 14
    }
47
48
    /**
49
     * {@inheritdoc}
50
     * @throws MappingException
51
     */
52
    protected function getFqcnFromAlias($namespaceAlias, $simpleClassName)
53
    {
54
        if (!array_key_exists($namespaceAlias, $this->aliases)) {
55
            throw MappingException::unknownAlias($namespaceAlias);
56
        }
57
58
        return $this->aliases[$namespaceAlias] . $simpleClassName;
59
    }
60
61
    /** {@inheritdoc} */
62 14
    protected function wakeupReflection(ClassMetadata $class, ReflectionService $reflService)
63
    {
64 14
        if (!($class instanceof EntityMetadata)) {
65
            throw new \LogicException('Metadata is not supported');
66
        }
67
68
        /** @var EntityMetadata $class */
69 14
        $class->wakeupReflection($reflService);
70 14
    }
71
72
    /**
73
     * Initializes Reflection after ClassMetadata was constructed.
74
     *
75
     * @param ClassMetadata     $class
76
     * @param ReflectionService $reflService
77
     *
78
     * @return void
79
     */
80 14
    protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService)
81
    {
82 14
        if (!($class instanceof EntityMetadata)) {
83
            throw new \LogicException('Metadata is not supported');
84
        }
85
86
        /** @var EntityMetadata $class */
87 14
        $class->initializeReflection($reflService);
88 14
    }
89
90
    /**
91
     * Checks whether the class metadata is an entity.
92
     *
93
     * This method should return false for mapped superclasses or embedded classes.
94
     *
95
     * @param ClassMetadata $class
96
     *
97
     * @return boolean
98
     */
99 14
    protected function isEntity(ClassMetadata $class)
100
    {
101 14
        return true;
102
    }
103
104
    /**
105
     * Actually loads the metadata from the underlying metadata.
106
     *
107
     * @param EntityMetadata      $class
108
     * @param EntityMetadata|null $parent
109
     * @param bool                $rootEntityFound
110
     * @param array               $nonSuperclassParents    All parent class names
111
     *                                                     that are not marked as mapped superclasses.
112
     *
113
     * @return void
114
     * @throws MappingException
115
     */
116 14
    protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents)
117
    {
118
        /* @var $class EntityMetadata */
119
        /* @var $parent EntityMetadata */
120 14
        if ($parent) {
121 6
            $this->addInheritedFields($class, $parent);
122 6
            $this->addInheritedRelations($class, $parent);
123 6
            $class->setIdentifier($parent->identifier);
124 6
            $class->clientName     = $parent->clientName;
125 6
            $class->searcher       = $parent->searcher;
126 6
            $class->methodProvider = $parent->methodProvider;
127
128 6
            if ($parent->isMappedSuperclass) {
129
                $class->setCustomRepositoryClass($parent->repositoryClass);
130
            }
131 6
        }
132
133
        // Invoke driver
134
        try {
135 14
            $this->getDriver()->loadMetadataForClass($class->getName(), $class);
136 14
        } catch (ReflectionException $e) {
137
            throw MappingException::nonExistingClass($class->getName());
138
        }
139 14
    }
140
141
    /**
142
     * Adds inherited fields to the subclass mapping.
143
     *
144
     * @param EntityMetadata $subClass
145
     * @param EntityMetadata $parentClass
146
     *
147
     * @return void
148
     */
149 6
    private function addInheritedFields(EntityMetadata $subClass, EntityMetadata $parentClass)
150
    {
151 6 View Code Duplication
        foreach ($parentClass->fields as $mapping) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152 6
            if (!isset($mapping['inherited']) && !$parentClass->isMappedSuperclass) {
153 6
                $mapping['inherited'] = $parentClass->name;
154 6
            }
155 6
            if (!isset($mapping['declared'])) {
156 6
                $mapping['declared'] = $parentClass->name;
157 6
            }
158 6
            $subClass->addInheritedFieldMapping($mapping);
159 6
        }
160 6
        foreach ($parentClass->reflFields as $name => $field) {
161 6
            $subClass->reflFields[$name] = $field;
162 6
        }
163 6
    }
164
165
    /**
166
     * Adds inherited association mappings to the subclass mapping.
167
     *
168
     * @param EntityMetadata $subClass
169
     * @param EntityMetadata $parentClass
170
     *
171
     * @return void
172
     *
173
     * @throws MappingException
174
     */
175 6
    private function addInheritedRelations(EntityMetadata $subClass, EntityMetadata $parentClass)
176
    {
177 6 View Code Duplication
        foreach ($parentClass->associations as $mapping) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178 6
            if (!isset($mapping['inherited']) && !$parentClass->isMappedSuperclass) {
179 6
                $mapping['inherited'] = $parentClass->name;
180 6
            }
181 6
            if (!isset($mapping['declared'])) {
182 6
                $mapping['declared'] = $parentClass->name;
183 6
            }
184 6
            $subClass->addInheritedAssociationMapping($mapping);
185 6
        }
186 6
    }
187
188
    /**
189
     * Returns the mapping driver implementation.
190
     *
191
     * @return \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver
192
     */
193 14
    protected function getDriver()
194
    {
195 14
        return $this->driver;
196
    }
197
198
    /**
199
     * Creates a new ClassMetadata instance for the given class name.
200
     *
201
     * @param string $className
202
     *
203
     * @return ClassMetadata
204
     */
205 14
    protected function newClassMetadataInstance($className)
206
    {
207 14
        return new EntityMetadata($className);
208
    }
209
}
210