Skip to content

Instantly share code, notes, and snippets.

@justenj
Created May 12, 2020 19:13
Show Gist options
  • Save justenj/19151577737b41c5efd6676605680b25 to your computer and use it in GitHub Desktop.
Save justenj/19151577737b41c5efd6676605680b25 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace Game\PlanetBundle\Presentation\Hydrator;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Doctrine\ORM\UnitOfWork;
use Game\Components\Planet\Domain\GameOverException;
use Game\Components\Planet\Domain\Planet;
final class PlanetHydrator extends ObjectHydrator
{
protected function hydrateRowData(array $row, array &$result)
{
$ids = ['Planet' => ''];
$nonemptyComponents = [];
$gatheredData = $this->gatherRowData($row, $ids, $nonemptyComponents);
$data = $gatheredData['data']['Planet'];
[
'id' => $planetId,
'playerId' => $playerId,
'name' => $name,
'level' => $level,
'metals' => $metals,
'polymers' => $polymers,
'resourceCountedAt' => $resourceCountedAt
] = $data;
try {
$entity = Planet::create($planetId, $playerId, $name, $level, $metals, $polymers, $resourceCountedAt);
$result[] = $entity;
$this->injectEntity($entity, $this->_uow, $data);
} catch (GameOverException $e) {
$this->injectEntity($e->getPlanet(), $this->_uow, $data);
throw $e;
}
}
private function injectEntity($entity, $uow, $data)
{
/**
* Crutches.
* @todo Need to review the Planet entity creating and resource collected validating
*/
call_user_func(\Closure::bind(
function () use ($uow, $entity, $data) {
$class = $uow->em->getClassMetadata(Planet::class);
$id = $uow->identifierFlattener->flattenIdentifier($class, $data);
$idHash = implode(' ', $id);
$oid = spl_object_hash($entity);
$uow->entityIdentifiers[$oid] = $id;
$uow->entityStates[$oid] = UnitOfWork::STATE_MANAGED;
$uow->originalEntityData[$oid] = $data;
$uow->identityMap[$class->rootEntityName][$idHash] = $entity;
},
null,
$uow
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment