Skip to content

Commit

Permalink
Merge pull request #3 from Format-D/refactoring-multiservices
Browse files Browse the repository at this point in the history
Refactoring multiservices
  • Loading branch information
bweinzierl authored Feb 28, 2023
2 parents d28194a + d560912 commit a67ed79
Show file tree
Hide file tree
Showing 13 changed files with 607 additions and 301 deletions.
28 changes: 28 additions & 0 deletions Classes/Aspect/LoggingAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace FormatD\GeoIndexable\Aspect;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;

/**
* An aspect
*
* @Flow\Scope("singleton")
* @Flow\Aspect
*/
class LoggingAspect
{
/**
*
*
* @param JoinPointInterface $joinPoint
* @Flow\Before("method(.*->indexByAddress(.*)) && within(FormatD\GeoIndexable\Domain\Service\AbstractGeoIndexingService)")
* @return void
*/
public function logIndexByAddress(JoinPointInterface $joinPoint) {
$className = $joinPoint->getClassName();
// echo 'Indexing by address with class: '.$className."\n";
}
}
9 changes: 5 additions & 4 deletions Classes/Aspect/RequestCachingAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ class RequestCachingAspect {
protected $requestCache;

/**
* @Flow\Around("method(FormatD\GeoIndexable\Service\GeoIndexService->sendRequest(.*))")
* @Flow\Around("method(.*->getResultFromAddress(.*)) && within(FormatD\GeoIndexable\Domain\Service\AbstractGeoIndexingService)")
* @param JoinPointInterface $joinPoint The current joinpoint
* @return string
*/
public function cacheRequests(JoinPointInterface $joinPoint) {

$cacheIdentifier = md5($joinPoint->getMethodArgument('uri'));
public function cacheResultFromAddress(JoinPointInterface $joinPoint){
$address = $joinPoint->getMethodArgument('address');
$className = $joinPoint->getClassName();
$cacheIdentifier = md5($className.'-'.$address);

if ($this->requestCache->has($cacheIdentifier)) {
$response = $this->requestCache->get($cacheIdentifier);
Expand Down
70 changes: 70 additions & 0 deletions Classes/Domain/LocationData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php


namespace FormatD\GeoIndexable\Domain;
use Doctrine\ORM\Mapping as ORM;
use Neos\Flow\Annotations as Flow;

/**
* @Flow\Scope("prototype")
*/
class LocationData implements LocationDataDetails
{
/**
* @var array
*/
protected $details;

/**
* @param array $requiredDetails
*/
public function __construct($requiredDetails) {
$this->details = array_combine($requiredDetails, array_pad([], count($requiredDetails), ''));
}

/**
* @return array
*/
public function getRequiredDetails(){
return array_keys($this->getDetails());
}

/**
* @return array
*/
public function getDetails(): array {
return $this->details;
}

/**
* @param string $name
* @return bool
*/
public function hasDetail($name){
return array_key_exists($name, $this->getDetails());
}

/**
* @param $name
* @return mixed
*/
public function getDetail($name){
if(!$this->hasDetail($name)){
return NULL;
}
return $this->details[$name];
}

/**
* @param string $name
* @param mixed $value
* @return bool (true if successful and false if no detail was found)
*/
public function setDetail($name, $value){
if(!$this->hasDetail($name)){
return false;
}
$this->details[$name] = $value;
return true;
}
}
18 changes: 18 additions & 0 deletions Classes/Domain/LocationDataDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace FormatD\GeoIndexable\Domain;

/**
* Supported Location Details
*/
interface LocationDataDetails
{
/**
* Details
*/
const LATITUDE = 'latitude';
const LONGITUDE = 'longitude';
const LABEL = 'label';
const CITY = 'city';
const COUNTRY = 'country';
const BOUNDINGBOX = 'boundingbox';
}
52 changes: 52 additions & 0 deletions Classes/Domain/Model/GeoIndexableElasticSearchTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace FormatD\GeoIndexable\Domain\Model;

use Doctrine\ORM\Mapping as ORM;
use Neos\Flow\Annotations as Flow;
use Flowpack\ElasticSearch\Annotations as ElasticSearch;
use FormatD\GeoIndexable\Domain\LocationDataDetails;

trait GeoIndexableElasticSearchTrait
{
use GeoIndexableTrait;

/**
* Lat and Lon as array for elasticsearch indexing [lat => xx.xxx, lon => xx.xxx]
* Be aware! After DB-Persist lat and lon are strings!
*
* @var array
* @ElasticSearch\Indexable
* @ElasticSearch\Mapping(type="geo_point")
*/
protected $locationGeoPoint = [];

/**
* @return array
*/
public function getLocationGeoPoint() {
if (isset($this->locationGeoPoint['lat']) && $this->locationGeoPoint['lat']) {
$this->locationGeoPoint['lat'] = (float) $this->locationGeoPoint['lat'];
}
if (isset($this->locationGeoPoint['lon']) && $this->locationGeoPoint['lon']) {
$this->locationGeoPoint['lon'] = (float) $this->locationGeoPoint['lon'];
}
return $this->locationGeoPoint;
}

/**
* @param string $locationAddress
*/
protected function onLocationChange($locationAddress){
if (strlen($locationAddress) > 1) {
$this->geoIndexService->indexByAddress($this->locationData, $locationAddress);
if(
$this->locationData->hasDetail(LocationDataDetails::LONGITUDE)
&& $this->locationData->hasDetail(LocationDataDetails::LATITUDE)
){
$this->locationGeoPoint['lat'] = $this->locationData->getDetail(LocationDataDetails::LATITUDE);
$this->locationGeoPoint['lon'] = $this->locationData->getDetail(LocationDataDetails::LONGITUDE);
}
}
}
}
144 changes: 29 additions & 115 deletions Classes/Domain/Model/GeoIndexableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace FormatD\GeoIndexable\Domain\Model;

use Doctrine\ORM\Mapping as ORM;
use FormatD\GeoIndexable\Domain\LocationData;
use FormatD\GeoIndexable\Domain\LocationDataDetails;
use Neos\Flow\Annotations as Flow;
use Flowpack\ElasticSearch\Annotations as ElasticSearch;

Expand All @@ -12,66 +14,27 @@
trait GeoIndexableTrait {

/**
* Lat and Lon as array for elasticsearch indexing [lat => xx.xxx, lon => xx.xxx]
* Be aware! After DB-Persist lat and lon are strings!
*
* @var array
* @ElasticSearch\Indexable
* @ElasticSearch\Mapping(type="geo_point")
*/
protected $locationGeoPoint = [];

/**
* @var string
* @ElasticSearch\Indexable
*/
protected $locationAddress = '';

/**
* @var float
* @ORM\Column(nullable=true)
* @ElasticSearch\Indexable
*/
protected $locationLatitude = NULL;

/**
* @var float
* @ORM\Column(nullable=true)
* @ElasticSearch\Indexable
* @Flow\Inject
* @var \FormatD\GeoIndexable\Service\GeoIndexService
*/
protected $locationLongitude = NULL;
protected $geoIndexService;

/**
* @var string
* @ElasticSearch\Indexable
* @var LocationData
* @ORM\Column(type="object")
*/
protected $locationLabel = '';
protected $locationData;

/**
* @var string
* @ORM\Column(nullable=true)
* @ElasticSearch\Indexable
*/
protected $locationTimezone = NULL;


/**
* @Flow\Inject
* @var \FormatD\GeoIndexable\Service\GeoIndexService
*/
protected $geoIndexService;
protected $locationAddress = '';

/**
* @return array
* @param array $details
*/
public function getLocationGeoPoint() {
if (isset($this->locationGeoPoint['lat']) && $this->locationGeoPoint['lat']) {
$this->locationGeoPoint['lat'] = (float) $this->locationGeoPoint['lat'];
}
if (isset($this->locationGeoPoint['lon']) && $this->locationGeoPoint['lon']) {
$this->locationGeoPoint['lon'] = (float) $this->locationGeoPoint['lon'];
}
return $this->locationGeoPoint;
public function setLocationDataDetails($details) {
$this->locationData = new LocationData($details);
}

/**
Expand All @@ -83,86 +46,37 @@ public function getLocationAddress() {

/**
* @param string $locationAddress
* @param bool $skipIndexing
*/
public function setLocationAddress($locationAddress) {
$this->onLocationChange($locationAddress);
$this->locationAddress = $locationAddress;
}

/**
* @return float
*/
public function getLocationLatitude() {
return $this->locationLatitude;
}
public function setLocationAddress($locationAddress, $skipIndexing = false) {
$this->setLocationDataDetails([LocationDataDetails::LATITUDE, LocationDataDetails::LONGITUDE]);

/**
* @param float $locationLatitude
*/
public function setLocationLatitude($locationLatitude) {
$this->locationGeoPoint['lat'] = $locationLatitude;
$this->locationLatitude = $locationLatitude;
}

/**
* @return float
*/
public function getLocationLongitude() {
return $this->locationLongitude;
}

/**
* @param float $locationLongitude
*/
public function setLocationLongitude($locationLongitude) {
$this->locationGeoPoint['lon'] = $locationLongitude;
$this->locationLongitude = $locationLongitude;
}

/**
* @return string
*/
public function getLocationLabel() {
return $this->locationLabel;
}

/**
* @param string $locationLabel
*/
public function setLocationLabel($locationLabel) {
$this->locationLabel = $locationLabel;
}

/**
* @return string
*/
public function getLocationTimezone() {
return $this->locationTimezone;
if ($this->locationAddress === $locationAddress) {
$skipIndexing = true;
} else {
$this->locationAddress = $locationAddress;
}
if (!$skipIndexing) {
$this->onLocationChange($locationAddress);
}
}

/**
* @param string $locationTimezone
* @return LocationData
*/
public function setLocationTimezone($locationTimezone) {
$this->locationTimezone = $locationTimezone;
public function getLocationData() {
return $this->locationData;
}


/**
* @param string $locationAddress
*/
protected function onLocationChange($locationAddress){
protected function onLocationChange($locationAddress) {
if (strlen($locationAddress) > 1) {
$this->geoIndexService->indexAddress($locationAddress);
$this->geoIndexService->setLocationDataOnObject($this);
} else {
$this->setLocationLabel('');
$this->setLocationLatitude(NULL);
$this->setLocationLongitude(NULL);
$this->setLocationTimezone(NULL);
$this->geoIndexService->indexByAddress($this->locationData, $locationAddress);
}
}

}

?>
?>
Loading

0 comments on commit a67ed79

Please sign in to comment.