Skip to content

Commit

Permalink
Refactor object extraction method to reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratadox committed Apr 28, 2018
1 parent ed5b286 commit a55d6c8
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/Objects.php
Expand Up @@ -3,8 +3,10 @@

namespace Stratadox\TableLoader;

use Stratadox\Hydrator\CannotHydrate;
use Stratadox\Hydrator\Hydrates;
use Stratadox\IdentityMap\MapsObjectsByIdentity;
use Stratadox\IdentityMap\AlreadyThere;
use Stratadox\IdentityMap\MapsObjectsByIdentity as Map;
use Throwable;

/**
Expand Down Expand Up @@ -47,28 +49,37 @@ public static function producedByThis(
}

/** @inheritdoc */
public function from(
array $input,
MapsObjectsByIdentity $map
): ContainsResultingObjects {
public function from(array $input, Map $map): ContainsResultingObjects {
$data = $this->relevantData->only($input);
$label = $this->relevantData->label();
$objects = [];
foreach ($data as $row) {
$hash = $this->identifier->forLoading($row);
if (!isset($objects[$hash])) {
try {
$class = $this->hydrate->classFor($row);
$id = $this->identifier->forIdentityMap($row);
if (!$map->has($class, $id)) {
$map = $map->add($id, $this->hydrate->fromArray($row));
}
$objects[$hash] = $map->get($class, $id);
} catch (Throwable $exception) {
throw UnmappableRow::encountered($exception, $label, $row);
}
if (isset($objects[$hash])) {
continue;
}
try {
$class = $this->hydrate->classFor($row);
$id = $this->identifier->forIdentityMap($row);
$map = $this->addToMapIfNew($class, $id, $row, $map);
$objects[$hash] = $map->get($class, $id);
} catch (Throwable $exception) {
throw UnmappableRow::encountered($exception, $label, $row);
}
}
return Result::fromArray([$label => $objects], $map);
}

/** @throws CannotHydrate|AlreadyThere */
private function addToMapIfNew(
string $class,
string $id,
array $row,
Map $map
): Map {
if (!$map->has($class, $id)) {
$map = $map->add($id, $this->hydrate->fromArray($row));
}
return $map;
}
}

0 comments on commit a55d6c8

Please sign in to comment.