Compare commits
4 Commits
issue/34-g
...
issue/6-re
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e4222229c4 | ||
|
|
df96a8c1ed | ||
|
|
4d936a6279 | ||
|
|
f30c20ceda |
16
src/Model/GeoCoordinates.php
Normal file
16
src/Model/GeoCoordinates.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pcm\GeocodeBundle\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A model for accessing latitude and longitude values.
|
||||||
|
*
|
||||||
|
* @package Pcm\GeocodeBundle
|
||||||
|
*/
|
||||||
|
final class GeoCoordinates
|
||||||
|
{
|
||||||
|
public function __construct(readonly public float $latitude, readonly public float $longitude) {}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A model for accessing latitude and longitude values
|
|
||||||
*
|
|
||||||
* @package Pcm\GeocodeBundle
|
|
||||||
*/
|
|
||||||
class LatLongModel
|
|
||||||
{
|
|
||||||
public function __construct(private float $latitude, private float $longitude) {}
|
|
||||||
|
|
||||||
public function getLatitude(): float
|
|
||||||
{
|
|
||||||
return $this->latitude;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLongitude(): float
|
|
||||||
{
|
|
||||||
return $this->longitude;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
|
|||||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||||
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
|
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
|
||||||
|
|
||||||
class PcmGeocodeBundle extends AbstractBundle {
|
final class PcmGeocodeBundle extends AbstractBundle {
|
||||||
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
|
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
|
||||||
{
|
{
|
||||||
$loader = new YamlFileLoader($builder, new FileLocator(__DIR__.'/../config/'));
|
$loader = new YamlFileLoader($builder, new FileLocator(__DIR__.'/../config/'));
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Service;
|
namespace Pcm\GeocodeBundle\Service;
|
||||||
|
|
||||||
use Pcm\GeocodeBundle\Model\LatLongModel;
|
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
|
|
||||||
class Geocoder
|
final class Geocoder
|
||||||
{
|
{
|
||||||
private const string API_URL = "https://nominatim.openstreetmap.org/search";
|
private const string API_URL = "https://nominatim.openstreetmap.org/search";
|
||||||
|
|
||||||
@@ -18,14 +18,15 @@ class Geocoder
|
|||||||
* Convert a postcode into latitude and longitude. Returns null if conversion failed.
|
* Convert a postcode into latitude and longitude. Returns null if conversion failed.
|
||||||
*
|
*
|
||||||
* @param string $postcode
|
* @param string $postcode
|
||||||
* @return null|LatLongModel
|
* @return null|GeoCoordinates
|
||||||
*/
|
*/
|
||||||
public function geocodePostcode(string $postcode): ?LatLongModel
|
public function geocodePostcode(string $postcode): ?GeoCoordinates
|
||||||
{
|
{
|
||||||
$client = $this->createClient();
|
$client = $this->createClient();
|
||||||
$response = $this->makeApiRequest($client, $postcode);
|
$response = $this->makeApiRequest($client, $postcode);
|
||||||
$data = $this->getDataFromResponse($response);
|
$data = $this->getDataFromResponse($response);
|
||||||
|
|
||||||
|
// @todo remove check, I don't think it's needed
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -34,7 +35,7 @@ class Geocoder
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->createLatLongModel($data);
|
return $this->createGeoCoordinates($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createClient(): HttpClientInterface
|
private function createClient(): HttpClientInterface
|
||||||
@@ -63,12 +64,12 @@ class Geocoder
|
|||||||
return $response->toArray(false);
|
return $response->toArray(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createLatLongModel(array $data): LatLongModel
|
private function createGeoCoordinates(array $data): GeoCoordinates
|
||||||
{
|
{
|
||||||
$lat = $this->getLatitudeFromData($data);
|
$lat = $this->getLatitudeFromData($data);
|
||||||
$long = $this->getLongitudeFromData($data);
|
$long = $this->getLongitudeFromData($data);
|
||||||
|
|
||||||
return new LatLongModel($lat, $long);
|
return new GeoCoordinates($lat, $long);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getLatitudeFromData(array $data): float
|
private function getLatitudeFromData(array $data): float
|
||||||
@@ -81,3 +82,4 @@ class Geocoder
|
|||||||
return (float) $data[0]['lon'];
|
return (float) $data[0]['lon'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
|
|||||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
use Symfony\Component\HttpKernel\Kernel;
|
use Symfony\Component\HttpKernel\Kernel;
|
||||||
|
|
||||||
class AppKernel extends Kernel
|
final class AppKernel extends Kernel
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
@@ -30,3 +30,4 @@ class AppKernel extends Kernel
|
|||||||
$loader->load(__DIR__.'/config_'.$this->getEnvironment().'.yml');
|
$loader->load(__DIR__.'/config_'.$this->getEnvironment().'.yml');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Pcm\GeocodeBundle\Tests;
|
|||||||
|
|
||||||
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
|
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
|
||||||
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
|
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
|
||||||
use Pcm\GeocodeBundle\Model\LatLongModel;
|
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||||
use Pcm\GeocodeBundle\Service\Geocoder;
|
use Pcm\GeocodeBundle\Service\Geocoder;
|
||||||
use Pcm\GeocodeBundle\Tests\AppKernel;
|
use Pcm\GeocodeBundle\Tests\AppKernel;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
@@ -14,7 +14,7 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|||||||
/**
|
/**
|
||||||
* We sleep 1 second after API calls to prevent breaking the API T&Cs.
|
* We sleep 1 second after API calls to prevent breaking the API T&Cs.
|
||||||
*/
|
*/
|
||||||
class GeocodeTest extends KernelTestCase
|
final class GeocodeTest extends KernelTestCase
|
||||||
{
|
{
|
||||||
// Buckingham Palace
|
// Buckingham Palace
|
||||||
private const string POSTCODE = 'SW1A 1AA';
|
private const string POSTCODE = 'SW1A 1AA';
|
||||||
@@ -39,11 +39,22 @@ class GeocodeTest extends KernelTestCase
|
|||||||
$this->assertNull($this->geocoder->geocodePostcode(''));
|
$this->assertNull($this->geocoder->geocodePostcode(''));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeReturnsLatLonModel(): void
|
public function testGeocodePostcodeReturnsGeoCoordinates(): GeoCoordinates
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$result = $this->geocoder->geocodePostcode(self::POSTCODE);
|
$geoCoords = $this->geocoder->geocodePostcode(self::POSTCODE);
|
||||||
$this->assertInstanceOf(LatLongModel::class, $result);
|
$this->assertNotNull($geoCoords);
|
||||||
|
|
||||||
|
return $geoCoords;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testGeocodePostcodeReturnsGeoCoordinates
|
||||||
|
*/
|
||||||
|
public function testCoordinatesAreSet(GeoCoordinates $geoCoordinates): void
|
||||||
|
{
|
||||||
|
$this->assertIsFloat($geoCoordinates->latitude);
|
||||||
|
$this->assertIsFloat($geoCoordinates->longitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getMappableEntity(): MappableInterface
|
private function getMappableEntity(): MappableInterface
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
|
|||||||
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
|
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class MappableTraitTest extends TestCase
|
final class MappableTraitTest extends TestCase
|
||||||
{
|
{
|
||||||
private const float COORD = 123.456;
|
private const float COORD = 123.456;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user