Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display locations on conference list #532

Merged
merged 11 commits into from
Jan 21, 2025
Next Next commit
Separate getCoordinates and geocode methods
andrewmile committed Dec 20, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
xatier xatier
commit 58624d4a18ba2349e0adb043349e4f417016f9cb
3 changes: 2 additions & 1 deletion app/CallingAllPapers/ConferenceImporter.php
Original file line number Diff line number Diff line change
@@ -115,7 +115,8 @@ private function nullifyInvalidLatLong($primary, $secondary)
private function geocodeLatLongFromLocation(Conference $conference): Conference
{
try {
$conference->coordinates = $this->geocoder->geocode($conference->location);
$result = $this->geocoder->geocode($conference->location);
$conference->coordinates = $result->getCoordinates();
} catch (InvalidAddressGeocodingException $e) {
}

18 changes: 13 additions & 5 deletions app/Services/Geocoder.php
Original file line number Diff line number Diff line change
@@ -8,25 +8,33 @@

class Geocoder
{
public function geocode(string $address): Coordinates
private $response;

public function geocode(string $address): self
{
if ($this->isInvalidAddress($address)) {
throw new InvalidAddressGeocodingException;
}

$response = $this->requestGeocoding($address);
$this->response = $this->requestGeocoding($address);

if (! count($response['results'])) {
if (! count($this->response['results'])) {
cache()->set('invalid-address::' . md5($address), true);
throw new InvalidAddressGeocodingException;
}

return $this;
}

public function getCoordinates(): Coordinates
{
return new Coordinates(
$this->getCoordinate('lat', $response),
$this->getCoordinate('lng', $response),
$this->getCoordinate('lat', $this->response),
$this->getCoordinate('lng', $this->response),
);
}


private function requestGeocoding($address)
{
return Http::acceptJson()
2 changes: 2 additions & 0 deletions tests/Feature/CallingAllPapersConferenceImporterTest.php
Original file line number Diff line number Diff line change
@@ -236,6 +236,8 @@ public function it_fills_latitude_and_longitude_from_location_if_lat_long_are_nu
$this->mockClient($event);
$this->mock(Geocoder::class, function ($mock) {
$mock->shouldReceive('geocode')
->andReturn($mock)
->shouldReceive('getCoordinates')
->andReturn(new Coordinates('38.8921062', '-77.0259036'));
});

3 changes: 2 additions & 1 deletion tests/Integration/GeocoderTest.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,8 @@ public function geocoding_an_address(): void

$geocoder = app(Geocoder::class);

$coordinates = $geocoder->geocode('1600 Pennsylvania Ave Washington, DC');
$result = $geocoder->geocode('1600 Pennsylvania Ave Washington, DC');
$coordinates = $result->getCoordinates();

$this->assertEquals('38.8976801', $coordinates->getLatitude());
$this->assertEquals('-77.0363304', $coordinates->getLongitude());