Refactor GeoJsonSubscriber to an listener.

This commit is contained in:
David Molineus
2017-10-17 17:57:40 +02:00
parent 57fe57b8ea
commit d261e8b4ba
2 changed files with 37 additions and 37 deletions

View File

@@ -61,12 +61,12 @@ services:
arguments: arguments:
- '%netzmacht.contao_leaflet.styles%' - '%netzmacht.contao_leaflet.styles%'
netzmacht.contao_leaflet.listeners.geo_json_subscriber: netzmacht.contao_leaflet.listeners.geo_json_listener:
class: Netzmacht\Contao\Leaflet\Subscriber\GeoJsonSubscriber class: Netzmacht\Contao\Leaflet\Listener\GeoJsonListener
arguments: arguments:
- '%netzmacht.contao_leaflet.feature_model_properties%' - '%netzmacht.contao_leaflet.feature_model_properties%'
tags: tags:
- { name: 'kernel.event_subscriber' } - { name: 'kernel.event_listener', event: 'netzmacht.contao_leaflet.mapper.convert_to_geojson', method: 'handle' }
netzmacht.contao_leaflet.listeners.hash_subscriber: netzmacht.contao_leaflet.listeners.hash_subscriber:
class: Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber class: Netzmacht\Contao\Leaflet\Subscriber\HashSubscriber

View File

@@ -10,24 +10,28 @@
* @filesource * @filesource
*/ */
namespace Netzmacht\Contao\Leaflet\Subscriber; declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Listener;
use Contao\Model;
use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent; use Netzmacht\Contao\Leaflet\Event\ConvertToGeoJsonEvent;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\LeafletPHP\Definition as LeafletDefinition;
use Netzmacht\LeafletPHP\Definition\HasPopup; use Netzmacht\LeafletPHP\Definition\HasPopup;
use Netzmacht\LeafletPHP\Definition\UI\Marker; use Netzmacht\LeafletPHP\Definition\UI\Marker;
use Netzmacht\LeafletPHP\Definition\Vector; use Netzmacht\LeafletPHP\Definition\Vector;
use Netzmacht\LeafletPHP\Definition\Vector\Circle; use Netzmacht\LeafletPHP\Definition\Vector\Circle;
use Netzmacht\LeafletPHP\Definition\Vector\CircleMarker; use Netzmacht\LeafletPHP\Definition\Vector\CircleMarker;
use Netzmacht\LeafletPHP\Value\GeoJson\Feature; use Netzmacht\LeafletPHP\Value\GeoJson\Feature;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Netzmacht\LeafletPHP\Value\GeoJson\GeoJsonObject;
/** /**
* Class GeoJsonSubscriber provides subscribers when a definition is converted to a geo json feature. * Class GeoJsonSubscriber provides subscribers when a definition is converted to a geo json feature.
* *
* @package Netzmacht\Contao\Leaflet\Subscriber * @package Netzmacht\Contao\Leaflet\Subscriber
*/ */
class GeoJsonSubscriber implements EventSubscriberInterface final class GeoJsonListener
{ {
/** /**
* Property mapping between models and features. * Property mapping between models and features.
@@ -47,32 +51,34 @@ class GeoJsonSubscriber implements EventSubscriberInterface
} }
/** /**
* {@inheritdoc} * Handle the event.
*
* @param ConvertToGeoJsonEvent $event The event.
*
* @return void
*/ */
public static function getSubscribedEvents() public function handle(ConvertToGeoJsonEvent $event)
{ {
return array( $feature = $event->getGeoJson();
ConvertToGeoJsonEvent::NAME => array( $definition = $event->getDefinition();
array('addPopup'), $model = $event->getModel();
array('enrichObjects'),
array('enrichCircle'), $this->addPopup($feature, $definition);
array('setModelData') $this->enrichObjects($feature, $definition, $model);
) $this->enrichCircle($feature, $definition);
); $this->setModelData($feature, $model);
} }
/** /**
* Add popup property for definitions with an popup. * Add popup property for definitions with an popup.
* *
* @param ConvertToGeoJsonEvent $event The subscribed event. * @param GeoJsonObject $feature The geojson feature object.
* @param LeafletDefinition $definition The definition.
* *
* @return void * @return void
*/ */
public function addPopup(ConvertToGeoJsonEvent $event) public function addPopup(GeoJsonObject $feature, LeafletDefinition $definition)
{ {
$feature = $event->getGeoJson();
$definition = $event->getDefinition();
if ($definition instanceof HasPopup && $feature instanceof Feature) { if ($definition instanceof HasPopup && $feature instanceof Feature) {
if ($definition->getPopup()) { if ($definition->getPopup()) {
$feature->setProperty('popup', $definition->getPopup()); $feature->setProperty('popup', $definition->getPopup());
@@ -91,16 +97,14 @@ class GeoJsonSubscriber implements EventSubscriberInterface
/** /**
* Enrich map object with feature data and bounds information. * Enrich map object with feature data and bounds information.
* *
* @param ConvertToGeoJsonEvent $event The subscribed event. * @param GeoJsonObject $feature The geojson feature object.
* @param LeafletDefinition $definition The definition.
* @param Model|object $model The data model.
* *
* @return void * @return void
*/ */
public function enrichObjects(ConvertToGeoJsonEvent $event) public function enrichObjects(GeoJsonObject $feature, LeafletDefinition $definition, $model)
{ {
$feature = $event->getGeoJson();
$definition = $event->getDefinition();
$model = $event->getModel();
if (($definition instanceof Marker || $definition instanceof Vector) if (($definition instanceof Marker || $definition instanceof Vector)
&& $model instanceof \Model && $feature instanceof Feature) { && $model instanceof \Model && $feature instanceof Feature) {
$this->setDataProperty($model, $feature); $this->setDataProperty($model, $feature);
@@ -111,15 +115,13 @@ class GeoJsonSubscriber implements EventSubscriberInterface
/** /**
* Enrich the the circle with constructor arguments. * Enrich the the circle with constructor arguments.
* *
* @param ConvertToGeoJsonEvent $event The subscribed events. * @param GeoJsonObject $feature The geojson feature object.
* @param LeafletDefinition $definition The definition.
* *
* @return void * @return void
*/ */
public function enrichCircle(ConvertToGeoJsonEvent $event) public function enrichCircle(GeoJsonObject $feature, LeafletDefinition $definition)
{ {
$feature = $event->getGeoJson();
$definition = $event->getDefinition();
if ($definition instanceof Circle && !$definition instanceof CircleMarker && $feature instanceof Feature) { if ($definition instanceof Circle && !$definition instanceof CircleMarker && $feature instanceof Feature) {
$feature->setProperty('arguments', array($definition->getLatLng(), $definition->getRadius())); $feature->setProperty('arguments', array($definition->getLatLng(), $definition->getRadius()));
} }
@@ -128,15 +130,13 @@ class GeoJsonSubscriber implements EventSubscriberInterface
/** /**
* Pass configured properties on an model to the properties.model key. * Pass configured properties on an model to the properties.model key.
* *
* @param ConvertToGeoJsonEvent $event The subscribed events. * @param GeoJsonObject $feature The geojson feature object.
* @param Model|object $model The data model.
* *
* @return void * @return void
*/ */
public function setModelData(ConvertToGeoJsonEvent $event) public function setModelData(GeoJsonObject $feature, $model)
{ {
$feature = $event->getGeoJson();
$model = $event->getModel();
if (!$model instanceof \Model || !$feature instanceof Feature if (!$model instanceof \Model || !$feature instanceof Feature
|| empty($this->featureModelProperties[$model->getTable()])) { || empty($this->featureModelProperties[$model->getTable()])) {
return; return;