Compare commits

...

5 Commits

Author SHA1 Message Date
brabli
84ba22f287 Update php version to 8.3 2024-08-05 11:56:05 +01:00
brabli
eaa1c0a173 Update packages to symfony 7 2024-08-05 11:55:32 +01:00
brabli
af13393765 Add check to make test pass 2024-08-05 11:17:22 +01:00
brabli
7e0c231d88 Add newline 2024-08-05 11:17:09 +01:00
Brabli
57796a6fdb Return null instead of throwing 2022-10-17 15:46:19 +01:00
5 changed files with 21 additions and 20 deletions

View File

@@ -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

View File

@@ -14,17 +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" "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": {

View File

@@ -23,3 +23,4 @@ class LatLongModel
return $this->longitude; return $this->longitude;
} }
} }

View File

@@ -15,17 +15,24 @@ class Geocoder
public function __construct(private HttpClientInterface $client) {} 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 * @param string $postcode
* @return LatLongModel * @return null|LatLongModel
*/ */
public function geocodePostcode(string $postcode): LatLongModel public function geocodePostcode(string $postcode): ?LatLongModel
{ {
$client = $this->createClient(); $client = $this->createClient();
$response = $this->makeApiRequest($client, $postcode); $response = $this->makeApiRequest($client, $postcode);
$data = $this->getDataFromResponse($response); $data = $this->getDataFromResponse($response);
$this->throwIfDataIsEmpty($data);
if (empty($data)) {
return null;
}
if (array_key_exists('error', $data)) {
return null;
}
return $this->createLatLongModel($data); return $this->createLatLongModel($data);
} }
@@ -56,12 +63,6 @@ class Geocoder
return $response->toArray(false); 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 private function createLatLongModel(array $data): LatLongModel
{ {
$lat = $this->getLatitudeFromData($data); $lat = $this->getLatitudeFromData($data);

View File

@@ -33,11 +33,10 @@ 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