Compare commits
9 Commits
0.0.3
...
issue/2-up
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84ba22f287 | ||
|
|
eaa1c0a173 | ||
|
|
af13393765 | ||
|
|
7e0c231d88 | ||
|
|
57796a6fdb | ||
|
|
31d29f2460 | ||
|
|
6edbed9dfe | ||
|
|
3c23d5e88d | ||
|
|
ee418a4844 |
@@ -1,4 +1,4 @@
|
|||||||
FROM php:8.1-alpine
|
FROM php:8.3-alpine
|
||||||
|
|
||||||
WORKDIR /code
|
WORKDIR /code
|
||||||
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
|
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
|
||||||
|
|||||||
@@ -14,16 +14,17 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.1.0",
|
"php": "^8.3.0",
|
||||||
"symfony/http-client": "^6.1",
|
"symfony/http-client": "^7.1",
|
||||||
"symfony/dependency-injection": "^6.1",
|
"symfony/dependency-injection": "^7.1",
|
||||||
"symfony/framework-bundle": "^6.1",
|
"symfony/framework-bundle": "^7.1",
|
||||||
"symfony/yaml": "^6.1"
|
"symfony/yaml": "^7.1",
|
||||||
|
"doctrine/orm": "^2.13"
|
||||||
},
|
},
|
||||||
|
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9.5",
|
"phpunit/phpunit": "^9.5",
|
||||||
"symfony/phpunit-bridge": "^6.1"
|
"symfony/phpunit-bridge": "^7.1"
|
||||||
},
|
},
|
||||||
|
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Entity\Trait;
|
namespace Pcm\GeocodeBundle\Entity\Trait;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows an entity to be mapped via latitude and longitude coordinates
|
* Allows an entity to be mapped via latitude and longitude coordinates
|
||||||
*
|
*
|
||||||
@@ -13,8 +15,10 @@ namespace Pcm\GeocodeBundle\Entity\Trait;
|
|||||||
*/
|
*/
|
||||||
trait MappableTrait
|
trait MappableTrait
|
||||||
{
|
{
|
||||||
|
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
|
||||||
private ?float $latitude = null;
|
private ?float $latitude = null;
|
||||||
|
|
||||||
|
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
|
||||||
private ?float $longitude = null;
|
private ?float $longitude = null;
|
||||||
|
|
||||||
public function getLatitude(): ?float
|
public function getLatitude(): ?float
|
||||||
|
|||||||
@@ -9,17 +9,18 @@ namespace Pcm\GeocodeBundle\Model;
|
|||||||
*
|
*
|
||||||
* @package Pcm\GeocodeBundle
|
* @package Pcm\GeocodeBundle
|
||||||
*/
|
*/
|
||||||
class LatLonModel
|
class LatLongModel
|
||||||
{
|
{
|
||||||
public function __construct(private float $latitude, private float $longitude) {}
|
public function __construct(private float $latitude, private float $longitude) {}
|
||||||
|
|
||||||
public function getLatitude(): float
|
public function getLatitude(): float
|
||||||
{
|
{
|
||||||
return $this->getLatitude();
|
return $this->latitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLongitude(): float
|
public function getLongitude(): float
|
||||||
{
|
{
|
||||||
return $this->getLongitude();
|
return $this->longitude;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4,8 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Service;
|
namespace Pcm\GeocodeBundle\Service;
|
||||||
|
|
||||||
use Pcm\GeocodeBundle\Entity\GeocodeData;
|
use Pcm\GeocodeBundle\Model\LatLongModel;
|
||||||
use Pcm\GeocodeBundle\Model\LatLonModel;
|
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
|
|
||||||
@@ -16,19 +15,26 @@ class Geocoder
|
|||||||
public function __construct(private HttpClientInterface $client) {}
|
public function __construct(private HttpClientInterface $client) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a LatLonModel object
|
* Convert a postcode into latitude and longitude. Returns null if conversion failed.
|
||||||
*
|
*
|
||||||
* @param string $postcode
|
* @param string $postcode
|
||||||
* @return GeocodeData
|
* @return null|LatLongModel
|
||||||
*/
|
*/
|
||||||
public function geocodePostcode(string $postcode): LatLonModel
|
public function geocodePostcode(string $postcode): ?LatLongModel
|
||||||
{
|
{
|
||||||
$client = $this->createClient();
|
$client = $this->createClient();
|
||||||
$response = $this->requestGeocodeData($client, $postcode);
|
$response = $this->makeApiRequest($client, $postcode);
|
||||||
$data = $response->toArray();
|
$data = $this->getDataFromResponse($response);
|
||||||
$this->throwIfNoResponseData($data);
|
|
||||||
|
|
||||||
return $this->createLatLonObject($data);
|
if (empty($data)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('error', $data)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->createLatLongModel($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createClient(): HttpClientInterface
|
private function createClient(): HttpClientInterface
|
||||||
@@ -38,7 +44,7 @@ class Geocoder
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function requestGeocodeData(HttpClientInterface $client, string $postcode): ResponseInterface
|
private function makeApiRequest(HttpClientInterface $client, string $postcode): ResponseInterface
|
||||||
{
|
{
|
||||||
return $client->request(
|
return $client->request(
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
@@ -52,14 +58,26 @@ class Geocoder
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function throwIfNoResponseData(array $data): void
|
private function getDataFromResponse(ResponseInterface $response): array
|
||||||
{
|
{
|
||||||
if (empty($data))
|
return $response->toArray(false);
|
||||||
throw new \Exception("No data was received from API response! Were the arguments valid?");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createLatLonObject(array $data): LatLonModel
|
private function createLatLongModel(array $data): LatLongModel
|
||||||
{
|
{
|
||||||
return new LatLonModel((float) $data[0]['lat'], (float) $data[0]['lon']);
|
$lat = $this->getLatitudeFromData($data);
|
||||||
|
$long = $this->getLongitudeFromData($data);
|
||||||
|
|
||||||
|
return new LatLongModel($lat, $long);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getLatitudeFromData(array $data): float
|
||||||
|
{
|
||||||
|
return (float) $data[0]['lat'];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getLongitudeFromData(array $data): float
|
||||||
|
{
|
||||||
|
return (float) $data[0]['lon'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,14 @@ 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\LatLonModel;
|
use Pcm\GeocodeBundle\Model\LatLongModel;
|
||||||
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Note we sleep 1s after requests to prevent breaking the API T&Cs
|
||||||
|
*/
|
||||||
class GeocodeTest extends KernelTestCase
|
class GeocodeTest extends KernelTestCase
|
||||||
{
|
{
|
||||||
// Buckingham Palace
|
// Buckingham Palace
|
||||||
@@ -30,18 +33,17 @@ class GeocodeTest extends KernelTestCase
|
|||||||
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeThrowsOnInvalidInput(): void
|
public function testGeocodePostcodeReturnsNullOnInvalidInput(): void
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$this->expectException(\Exception::class);
|
$this->assertNull($this->geocoder->geocodePostcode(''));
|
||||||
$this->geocoder->geocodePostcode('aaaaaaaa');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeReturnsLatLonModel(): void
|
public function testGeocodePostcodeReturnsLatLonModel(): void
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$result = $this->geocoder->geocodePostcode(self::POSTCODE);
|
$result = $this->geocoder->geocodePostcode(self::POSTCODE);
|
||||||
$this->assertInstanceOf(LatLonModel::class, $result);
|
$this->assertInstanceOf(LatLongModel::class, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getMappableEntity(): MappableInterface
|
private function getMappableEntity(): MappableInterface
|
||||||
|
|||||||
Reference in New Issue
Block a user