Compare commits

..

5 Commits

Author SHA1 Message Date
brabli
a6c8c4cc0c Throw exceptions when something goes wrong instead of returning null 2024-08-05 13:07:04 +01:00
brabli
fcda1e931c Add exceptions 2024-08-05 13:06:42 +01:00
brabli
e4222229c4 Add test 2024-08-05 12:28:25 +01:00
brabli
df96a8c1ed Update geo coordinates to use readonly properties rather than getters 2024-08-05 12:28:22 +01:00
brabli
4d936a6279 Rename LatLongModel to GeoCoordinates 2024-08-05 12:16:42 +01:00
7 changed files with 101 additions and 43 deletions

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Pcm\GeocodeBundle\Exception;
class ApiErrorException extends GeocodeException
{
}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Pcm\GeocodeBundle\Exception;
class GeocodeException extends \RuntimeException
{
public function __construct(string $postcode, string $message)
{
$formattedMessage = sprintf("Failed to geocode a postcode \"%s\": %s", $postcode, $message);
parent::__construct($formattedMessage);
}
}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Pcm\GeocodeBundle\Exception;
class NoResultsFoundException extends GeocodeException
{
private const string MESSAGE = "No results found.";
public function __construct(string $postcode)
{
parent::__construct($postcode, self::MESSAGE);
}
}

View 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) {}
}

View File

@@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
namespace Pcm\GeocodeBundle\Model;
/**
* A model for accessing latitude and longitude values
*
* @package Pcm\GeocodeBundle
*/
final 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;
}
}

View File

@@ -4,7 +4,9 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Service;
use Pcm\GeocodeBundle\Model\LatLongModel;
use Pcm\GeocodeBundle\Exception\ApiErrorException;
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
use Pcm\GeocodeBundle\Model\GeoCoordinates;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -18,23 +20,23 @@ final class Geocoder
* Convert a postcode into latitude and longitude. Returns null if conversion failed.
*
* @param string $postcode
* @return null|LatLongModel
* @return null|GeoCoordinates
*/
public function geocodePostcode(string $postcode): ?LatLongModel
public function geocodePostcode(string $postcode): ?GeoCoordinates
{
$client = $this->createClient();
$response = $this->makeApiRequest($client, $postcode);
$data = $this->getDataFromResponse($response);
if (empty($data)) {
return null;
}
if (array_key_exists('error', $data)) {
return null;
throw new ApiErrorException($postcode, $data['error']['message']);
}
return $this->createLatLongModel($data);
if (empty($data)) {
throw new NoResultsFoundException($postcode, "No results found.");
}
return $this->createGeoCoordinates($data);
}
private function createClient(): HttpClientInterface
@@ -63,12 +65,12 @@ final class Geocoder
return $response->toArray(false);
}
private function createLatLongModel(array $data): LatLongModel
private function createGeoCoordinates(array $data): GeoCoordinates
{
$lat = $this->getLatitudeFromData($data);
$long = $this->getLongitudeFromData($data);
return new LatLongModel($lat, $long);
return new GeoCoordinates($lat, $long);
}
private function getLatitudeFromData(array $data): float
@@ -81,3 +83,4 @@ final class Geocoder
return (float) $data[0]['lon'];
}
}

View File

@@ -6,7 +6,9 @@ namespace Pcm\GeocodeBundle\Tests;
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
use Pcm\GeocodeBundle\Model\LatLongModel;
use Pcm\GeocodeBundle\Exception\ApiErrorException;
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
use Pcm\GeocodeBundle\Model\GeoCoordinates;
use Pcm\GeocodeBundle\Service\Geocoder;
use Pcm\GeocodeBundle\Tests\AppKernel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@@ -33,17 +35,38 @@ final class GeocodeTest extends KernelTestCase
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
}
public function testGeocodePostcodeReturnsNullOnInvalidInput(): void
public function testGeocodePostcodeThrowsOnEmptyInput(): void
{
sleep(1);
$this->assertNull($this->geocoder->geocodePostcode(''));
$this->expectException(ApiErrorException::class);
$this->expectExceptionMessageMatches("/Nothing to search for.$/");
$this->geocoder->geocodePostcode('');
}
public function testGeocodePostcodeReturnsLatLonModel(): void
public function testGeocodePostcodeThrowsOnInvalidPostcode(): void
{
sleep(1);
$result = $this->geocoder->geocodePostcode(self::POSTCODE);
$this->assertInstanceOf(LatLongModel::class, $result);
$this->expectException(NoResultsFoundException::class);
$this->expectExceptionMessageMatches("/No results found.$/");
$this->geocoder->geocodePostcode('ZZZZZZZZZZZZ');
}
public function testGeocodePostcodeReturnsGeoCoordinates(): GeoCoordinates
{
sleep(1);
$geoCoords = $this->geocoder->geocodePostcode(self::POSTCODE);
$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