bankiru /
doctrine-api-client
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 | 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))) { |
| 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
|
|||
| 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))) { |
| 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 |
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.