Improvements

This commit is contained in:
Brabli
2022-09-26 11:50:04 +01:00
parent 794d35e6e0
commit ee418a4844
3 changed files with 36 additions and 16 deletions

View File

@@ -9,7 +9,7 @@ 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) {}

View File

@@ -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,19 @@ 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
* *
* @param string $postcode * @param string $postcode
* @return GeocodeData * @return 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); $this->throwIfDataIsEmpty($data);
return $this->createLatLonObject($data); return $this->createLatLongModel($data);
} }
private function createClient(): HttpClientInterface private function createClient(): HttpClientInterface
@@ -38,7 +37,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 +51,32 @@ class Geocoder
); );
} }
private function throwIfNoResponseData(array $data): void private function getDataFromResponse(ResponseInterface $response): array
{
return $response->toArray(false);
}
private function throwIfDataIsEmpty(array $data): void
{ {
if (empty($data)) if (empty($data))
throw new \Exception("No data was received from API response! Were the arguments valid?"); 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'];
} }
} }

View File

@@ -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
@@ -41,7 +44,7 @@ class GeocodeTest extends KernelTestCase
{ {
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