Completed
Pull Request — master (#14)
by Pavel
03:24
created

EntityManager   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 179
Duplicated Lines 3.35 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 68.57%

Importance

Changes 0
Metric Value
dl 6
loc 179
c 0
b 0
f 0
wmc 27
lcom 1
cbo 12
ccs 48
cts 70
cp 0.6857
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getConfiguration() 0 4 1
A find() 3 12 3
A getClassMetadata() 0 4 1
A getMetadataFactory() 0 4 1
A getUnitOfWork() 0 4 1
A persist() 0 8 2
A remove() 0 5 1
A merge() 0 4 1
A clear() 0 4 1
A detach() 0 4 1
A refresh() 0 4 1
A getRepository() 0 12 2
A flush() 0 4 1
A initializeObject() 0 8 4
A contains() 0 4 1
A getReference() 3 15 3
A getProxyFactory() 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\Mapping\ApiMetadata;
6
use Bankiru\Api\Doctrine\Mapping\EntityMetadata;
7
use Bankiru\Api\Doctrine\Proxy\ApiCollection;
8
use Bankiru\Api\Doctrine\Proxy\ProxyFactory;
9
use Bankiru\Api\Doctrine\Utility\IdentifierFixer;
10
use Doctrine\Common\Collections\AbstractLazyCollection;
11
use Doctrine\Common\Persistence\ObjectRepository;
12
use Doctrine\Common\Proxy\Proxy;
13
14
class EntityManager implements ApiEntityManager
15
{
16
    /** @var EntityMetadataFactory */
17
    private $metadataFactory;
18
    /** @var  Configuration */
19
    private $configuration;
20
    /** @var ObjectRepository[] */
21
    private $repositories = [];
22
    /** @var UnitOfWork */
23
    private $unitOfWork;
24
    /** @var ProxyFactory */
25
    private $proxyFactory;
26
27
    /**
28
     * EntityManager constructor.
29
     *
30
     * @param Configuration $configuration
31
     */
32 18
    public function __construct(Configuration $configuration)
33
    {
34 18
        $this->configuration = $configuration;
35
36 18
        $this->metadataFactory = $configuration->getMetadataFactory();
37 18
        $this->metadataFactory->setEntityManager($this);
38
39 18
        $this->unitOfWork   = new UnitOfWork($this);
40 18
        $this->proxyFactory = new ProxyFactory($this);
41 18
    }
42
43
    /** {@inheritdoc} */
44 18
    public function getConfiguration()
45
    {
46 18
        return $this->configuration;
47
    }
48
49
50
    /** {@inheritdoc} */
51 10
    public function find($className, $id)
52
    {
53 10
        $metadata = $this->getClassMetadata($className);
54 10
        $id = IdentifierFixer::fixScalarId($id, $metadata);
55
56
        /** @var EntityMetadata $metadata */
57 10 View Code Duplication
        if (false !== ($entity = $this->getUnitOfWork()->tryGetById($id, $metadata->rootEntityName))) {
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...
58
            return $entity instanceof $metadata->name ? $entity : null;
59
        }
60
61 10
        return $this->getUnitOfWork()->getEntityPersister($className)->loadById($id);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     * @return ApiMetadata
67
     */
68 18
    public function getClassMetadata($className)
69
    {
70 18
        return $this->getMetadataFactory()->getMetadataFor($className);
71
    }
72
73
    /** {@inheritdoc} */
74 18
    public function getMetadataFactory()
75
    {
76 18
        return $this->metadataFactory;
77
    }
78
79
    /** {@inheritdoc} */
80 18
    public function getUnitOfWork()
81
    {
82 18
        return $this->unitOfWork;
83
    }
84
85
    /** {@inheritdoc} */
86 4
    public function persist($object)
87
    {
88 4
        if (!is_object($object)) {
89
            throw new \InvalidArgumentException('Not an object to persist');
90
        }
91
92 4
        $this->getUnitOfWork()->persist($object);
93 4
    }
94
95
    /** {@inheritdoc} */
96 1
    public function remove($object)
97
    {
98 1
        $this->getUnitOfWork()->getEntityPersister(get_class($object))
99 1
            ->delete($object);
100 1
    }
101
102
    /** {@inheritdoc} */
103
    public function merge($object)
104
    {
105
        throw new \BadMethodCallException('Merge is not supported');
106
    }
107
108
    /** {@inheritdoc} */
109
    public function clear($objectName = null)
110
    {
111
        $this->getUnitOfWork()->clear($objectName);
112
    }
113
114
    /** {@inheritdoc} */
115
    public function detach($object)
116
    {
117
        $this->getUnitOfWork()->detach($object);
0 ignored issues
show
Bug introduced by
The method detach() does not exist on Bankiru\Api\Doctrine\UnitOfWork. Did you maybe mean doDetach()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
118
    }
119
120
    /** {@inheritdoc} */
121
    public function refresh($object)
122
    {
123
        $this->getRepository(get_class($object))->find($object->getId());
124
    }
125
126
    /** {@inheritdoc} */
127 12
    public function getRepository($className)
128
    {
129 12
        if (!array_key_exists($className, $this->repositories)) {
130
            /** @var ApiMetadata $metadata */
131 12
            $metadata        = $this->getClassMetadata($className);
132 12
            $repositoryClass = $metadata->getRepositoryClass();
133
            /** @noinspection PhpInternalEntityUsedInspection */
134 12
            $this->repositories[$className] = new $repositoryClass($this, $className);
135 12
        }
136
137 12
        return $this->repositories[$className];
138
    }
139
140
    /** {@inheritdoc} */
141 4
    public function flush($entity = null)
142
    {
143 4
        $this->unitOfWork->commit($entity);
144 4
    }
145
146
    /** {@inheritdoc} */
147
    public function initializeObject($obj)
148
    {
149
        if ($obj instanceof Proxy && !$obj->__isInitialized()) {
150
            $obj->__load();
151
        } elseif ($obj instanceof ApiCollection) {
152
            $obj->initialize();
153
        }
154
    }
155
156
    /** {@inheritdoc} */
157
    public function contains($object)
158
    {
159
        return false;
160
    }
161
162
    /**
163
     * Gets a reference to the entity identified by the given type and identifier
164
     * without actually loading it, if the entity is not yet loaded.
165
     *
166
     * @param string $entityName The name of the entity type.
167
     * @param mixed  $id         The entity identifier.
168
     *
169
     * @return object The entity reference.
170
     */
171 5
    public function getReference($entityName, $id)
172
    {
173
        /** @var EntityMetadata $metadata */
174 5
        $metadata = $this->getClassMetadata($entityName);
175 5
        $id = IdentifierFixer::fixScalarId($id, $metadata);
176
177 5 View Code Duplication
        if (false !== ($entity = $this->getUnitOfWork()->tryGetById($id, $metadata->rootEntityName))) {
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 4
            return $entity instanceof $metadata->name ? $entity : null;
179
        }
180
181 3
        $proxy = $this->getProxyFactory()->getProxy($entityName, $id);
182 3
        $this->getUnitOfWork()->registerManaged($proxy, $id, null);
183
184 3
        return $proxy;
185
    }
186
187
    /** {@inheritdoc} */
188 3
    public function getProxyFactory()
189
    {
190 3
        return $this->proxyFactory;
191
    }
192
}
193