Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57796a6fdb | ||
|
|
31d29f2460 |
@@ -15,11 +15,11 @@ class LatLongModel
|
||||
|
||||
public function getLatitude(): float
|
||||
{
|
||||
return $this->getLatitude();
|
||||
return $this->latitude;
|
||||
}
|
||||
|
||||
public function getLongitude(): float
|
||||
{
|
||||
return $this->getLongitude();
|
||||
return $this->longitude;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,17 +15,20 @@ class Geocoder
|
||||
public function __construct(private HttpClientInterface $client) {}
|
||||
|
||||
/**
|
||||
* Convert a postcode into latitude and longitude
|
||||
* Convert a postcode into latitude and longitude. Returns null if conversion failed.
|
||||
*
|
||||
* @param string $postcode
|
||||
* @return LatLongModel
|
||||
* @return null|LatLongModel
|
||||
*/
|
||||
public function geocodePostcode(string $postcode): LatLongModel
|
||||
public function geocodePostcode(string $postcode): ?LatLongModel
|
||||
{
|
||||
$client = $this->createClient();
|
||||
$response = $this->makeApiRequest($client, $postcode);
|
||||
$data = $this->getDataFromResponse($response);
|
||||
$this->throwIfDataIsEmpty($data);
|
||||
|
||||
if (empty($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->createLatLongModel($data);
|
||||
}
|
||||
@@ -56,12 +59,6 @@ class Geocoder
|
||||
return $response->toArray(false);
|
||||
}
|
||||
|
||||
private function throwIfDataIsEmpty(array $data): void
|
||||
{
|
||||
if (empty($data))
|
||||
throw new \Exception("No data was received from API response! Were the arguments valid?");
|
||||
}
|
||||
|
||||
private function createLatLongModel(array $data): LatLongModel
|
||||
{
|
||||
$lat = $this->getLatitudeFromData($data);
|
||||
|
||||
@@ -33,11 +33,10 @@ class GeocodeTest extends KernelTestCase
|
||||
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
||||
}
|
||||
|
||||
public function testGeocodePostcodeThrowsOnInvalidInput(): void
|
||||
public function testGeocodePostcodeReturnsNullOnInvalidInput(): void
|
||||
{
|
||||
sleep(1);
|
||||
$this->expectException(\Exception::class);
|
||||
$this->geocoder->geocodePostcode('aaaaaaaa');
|
||||
$this->assertNull($this->geocoder->geocodePostcode(''));
|
||||
}
|
||||
|
||||
public function testGeocodePostcodeReturnsLatLonModel(): void
|
||||
|
||||
Reference in New Issue
Block a user