Refactor mapper to reduce duplicity.

This commit is contained in:
David Molineus
2015-01-22 10:01:30 +01:00
parent 52440c35dd
commit 25d4dace71

View File

@@ -20,9 +20,9 @@ use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
/** /**
* Class DefinitionBuilder is the main builder instance which contains all other builders as children. * Class DefinitionMapper is the main mapper instance which contains all other mappers as children.
* *
* @package Netzmacht\Contao\Leaflet\Builder * @package Netzmacht\Contao\Leaflet\Mapper
*/ */
class DefinitionMapper class DefinitionMapper
{ {
@@ -31,7 +31,7 @@ class DefinitionMapper
* *
* @var Mapper[][] * @var Mapper[][]
*/ */
private $builders = array(); private $mappers = array();
/** /**
* The event dispatcher. * The event dispatcher.
@@ -58,18 +58,18 @@ class DefinitionMapper
} }
/** /**
* Add a builder. * Add a mapper.
* *
* @param Mapper $builder The builder. * @param Mapper $mapper The mapper.
* @param int $priority The priority. The higher priorities get called first. * @param int $priority The priority. The higher priorities get called first.
* *
* @return $this * @return $this
*/ */
public function register(Mapper $builder, $priority = 0) public function register(Mapper $mapper, $priority = 0)
{ {
$this->builders[$priority][] = $builder; $this->mappers[$priority][] = $mapper;
krsort($this->builders); krsort($this->mappers);
return $this; return $this;
} }
@@ -88,16 +88,11 @@ class DefinitionMapper
*/ */
public function handle($model, LatLngBounds $bounds = null, $elementId = null, $parent = null) public function handle($model, LatLngBounds $bounds = null, $elementId = null, $parent = null)
{ {
$hash = $this->getHash($model, $elementId); $hash = $this->hash($model, $elementId);
if (isset($this->mapped[$hash])) { if (!isset($this->mapped[$hash])) {
return $this->mapped[$hash]; $mapper = $this->getMapper($model);
} $definition = $mapper->handle($model, $this, $bounds, $elementId, $parent);
foreach ($this->builders as $builders) {
foreach ($builders as $builder) {
if ($builder->match($model)) {
$definition = $builder->handle($model, $this, $bounds, $elementId, $parent);
if ($definition) { if ($definition) {
$event = new BuildDefinitionEvent($definition, $model, $bounds); $event = new BuildDefinitionEvent($definition, $model, $bounds);
@@ -105,19 +100,9 @@ class DefinitionMapper
} }
$this->mapped[$hash] = $definition; $this->mapped[$hash] = $definition;
return $definition;
}
}
} }
throw new \RuntimeException( return $this->mapped[$hash];
sprintf(
'Could not build model "%s::%s". No matching builders found.',
$model->getTable(),
$model->{$model->getPk()}
)
);
} }
/** /**
@@ -132,29 +117,15 @@ class DefinitionMapper
*/ */
public function handleGeoJson($model, LatLngBounds $bounds = null) public function handleGeoJson($model, LatLngBounds $bounds = null)
{ {
foreach ($this->builders as $builders) { $mapper = $this->getMapper($model);
foreach ($builders as $builder) {
if (!$builder->match($model)) {
continue;
}
if ($builder instanceof GeoJsonMapper) { if ($mapper instanceof GeoJsonMapper) {
return $builder->handleGeoJson($model, $this, $bounds); return $mapper->handleGeoJson($model, $this, $bounds);
} }
throw new \RuntimeException( throw new \RuntimeException(
sprintf( sprintf(
'Builder for model "%s::%s" is not a GeoJsonMapper', 'Mapper for model "%s::%s" is not a GeoJsonMapper',
$model->getTable(),
$model->{$model->getPk()}
)
);
}
}
throw new \RuntimeException(
sprintf(
'Could not build geo json of model "%s::%s". No matching builders found.',
$model->getTable(), $model->getTable(),
$model->{$model->getPk()} $model->{$model->getPk()}
) )
@@ -171,7 +142,7 @@ class DefinitionMapper
* *
* @throws \RuntimeException If no hash was created. * @throws \RuntimeException If no hash was created.
*/ */
protected function getHash($model, $elementId) private function hash($model, $elementId = null)
{ {
$event = new GetHashEvent($model); $event = new GetHashEvent($model);
$this->eventDispatcher->dispatch($event::NAME, $event); $this->eventDispatcher->dispatch($event::NAME, $event);
@@ -187,4 +158,29 @@ class DefinitionMapper
return $hash; return $hash;
} }
/**
* Get the mapper for a definition model.
*
* @param mixed $model The data model.
*
* @return Mapper
*/
private function getMapper($model)
{
foreach ($this->mappers as $mappers) {
foreach ($mappers as $mapper) {
if ($mapper->match($model)) {
return $mapper;
}
}
}
throw new \RuntimeException(
sprintf(
'Could not build model "". No matching mappers found.',
$this->hash($model)
)
);
}
} }