Rename LatLongModel to GeoCoordinates

This commit is contained in:
brabli
2024-08-05 12:16:42 +01:00
parent f30c20ceda
commit 4d936a6279
3 changed files with 12 additions and 10 deletions

View File

@@ -9,7 +9,7 @@ namespace Pcm\GeocodeBundle\Model;
* *
* @package Pcm\GeocodeBundle * @package Pcm\GeocodeBundle
*/ */
final class LatLongModel final class GeoCoordinates
{ {
public function __construct(private float $latitude, private float $longitude) {} public function __construct(private float $latitude, private float $longitude) {}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Service; namespace Pcm\GeocodeBundle\Service;
use Pcm\GeocodeBundle\Model\LatLongModel; use Pcm\GeocodeBundle\Model\GeoCoordinates;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface; use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -18,14 +18,15 @@ final class Geocoder
* Convert a postcode into latitude and longitude. Returns null if conversion failed. * Convert a postcode into latitude and longitude. Returns null if conversion failed.
* *
* @param string $postcode * @param string $postcode
* @return null|LatLongModel * @return null|GeoCoordinates
*/ */
public function geocodePostcode(string $postcode): ?LatLongModel public function geocodePostcode(string $postcode): ?GeoCoordinates
{ {
$client = $this->createClient(); $client = $this->createClient();
$response = $this->makeApiRequest($client, $postcode); $response = $this->makeApiRequest($client, $postcode);
$data = $this->getDataFromResponse($response); $data = $this->getDataFromResponse($response);
// @todo remove check, I don't think it's needed
if (empty($data)) { if (empty($data)) {
return null; return null;
} }
@@ -34,7 +35,7 @@ final class Geocoder
return null; return null;
} }
return $this->createLatLongModel($data); return $this->createGeoCoordinates($data);
} }
private function createClient(): HttpClientInterface private function createClient(): HttpClientInterface
@@ -63,12 +64,12 @@ final class Geocoder
return $response->toArray(false); return $response->toArray(false);
} }
private function createLatLongModel(array $data): LatLongModel private function createGeoCoordinates(array $data): GeoCoordinates
{ {
$lat = $this->getLatitudeFromData($data); $lat = $this->getLatitudeFromData($data);
$long = $this->getLongitudeFromData($data); $long = $this->getLongitudeFromData($data);
return new LatLongModel($lat, $long); return new GeoCoordinates($lat, $long);
} }
private function getLatitudeFromData(array $data): float private function getLatitudeFromData(array $data): float
@@ -81,3 +82,4 @@ final class Geocoder
return (float) $data[0]['lon']; return (float) $data[0]['lon'];
} }
} }

View File

@@ -6,7 +6,7 @@ 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\LatLongModel; use Pcm\GeocodeBundle\Model\GeoCoordinates;
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;
@@ -39,11 +39,11 @@ final class GeocodeTest extends KernelTestCase
$this->assertNull($this->geocoder->geocodePostcode('')); $this->assertNull($this->geocoder->geocodePostcode(''));
} }
public function testGeocodePostcodeReturnsLatLonModel(): void public function testGeocodePostcodeReturnsGeoCoordinates(): void
{ {
sleep(1); sleep(1);
$result = $this->geocoder->geocodePostcode(self::POSTCODE); $result = $this->geocoder->geocodePostcode(self::POSTCODE);
$this->assertInstanceOf(LatLongModel::class, $result); $this->assertInstanceOf(GeoCoordinates::class, $result);
} }
private function getMappableEntity(): MappableInterface private function getMappableEntity(): MappableInterface