Completed
Pull Request — master (#14)
by Pavel
28:57
created

EntityManager.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    public function __construct(Configuration $configuration)
33
    {
34
        $this->configuration = $configuration;
35
36
        $this->metadataFactory = $configuration->getMetadataFactory();
37
        $this->metadataFactory->setEntityManager($this);
38
39
        $this->unitOfWork   = new UnitOfWork($this);
40
        $this->proxyFactory = new ProxyFactory($this);
41
    }
42
43
    /** {@inheritdoc} */
44
    public function getConfiguration()
45
    {
46
        return $this->configuration;
47
    }
48
49
50
    /** {@inheritdoc} */
51
    public function find($className, $id)
52
    {
53
        $metadata = $this->getClassMetadata($className);
54
        $id = IdentifierFixer::fixScalarId($id, $metadata);
55
56
        /** @var EntityMetadata $metadata */
57 View Code Duplication
        if (false !== ($entity = $this->getUnitOfWork()->tryGetById($id, $metadata->rootEntityName))) {
0 ignored issues
show
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
        return $this->getUnitOfWork()->getEntityPersister($className)->loadById($id);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     * @return ApiMetadata
67
     */
68
    public function getClassMetadata($className)
69
    {
70
        return $this->getMetadataFactory()->getMetadataFor($className);
71
    }
72
73
    /** {@inheritdoc} */
74
    public function getMetadataFactory()
75
    {
76
        return $this->metadataFactory;
77
    }
78
79
    /** {@inheritdoc} */
80
    public function getUnitOfWork()
81
    {
82
        return $this->unitOfWork;
83
    }
84
85
    /** {@inheritdoc} */
86
    public function persist($object)
87
    {
88
        if (!is_object($object)) {
89
            throw new \InvalidArgumentException('Not an object to persist');
90
        }
91
92
        $this->getUnitOfWork()->persist($object);
93
    }
94
95
    /** {@inheritdoc} */
96
    public function remove($object)
97
    {
98
        $this->getUnitOfWork()->getEntityPersister(get_class($object))
99
            ->delete($object);
100
    }
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
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
    public function getRepository($className)
128
    {
129
        if (!array_key_exists($className, $this->repositories)) {
130
            /** @var ApiMetadata $metadata */
131
            $metadata        = $this->getClassMetadata($className);
132
            $repositoryClass = $metadata->getRepositoryClass();
133
            /** @noinspection PhpInternalEntityUsedInspection */
134
            $this->repositories[$className] = new $repositoryClass($this, $className);
135
        }
136
137
        return $this->repositories[$className];
138
    }
139
140
    /** {@inheritdoc} */
141
    public function flush($entity = null)
142
    {
143
        $this->unitOfWork->commit($entity);
144
    }
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
    public function getReference($entityName, $id)
172
    {
173
        /** @var EntityMetadata $metadata */
174
        $metadata = $this->getClassMetadata($entityName);
175
        $id = IdentifierFixer::fixScalarId($id, $metadata);
176
177 View Code Duplication
        if (false !== ($entity = $this->getUnitOfWork()->tryGetById($id, $metadata->rootEntityName))) {
0 ignored issues
show
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
            return $entity instanceof $metadata->name ? $entity : null;
179
        }
180
181
        $proxy = $this->getProxyFactory()->getProxy($entityName, $id);
182
        $this->getUnitOfWork()->registerManaged($proxy, $id, null);
183
184
        return $proxy;
185
    }
186
187
    /** {@inheritdoc} */
188
    public function getProxyFactory()
189
    {
190
        return $this->proxyFactory;
191
    }
192
}
193