-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #532 from tighten/ajm/display-location
Display locations on conference list
- Loading branch information
Showing
12 changed files
with
249 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Exceptions\InvalidAddressGeocodingException; | ||
use App\Models\Conference; | ||
use App\Services\Geocoder\Geocoder; | ||
use Illuminate\Console\Command; | ||
|
||
class BackfillConferenceLocationNames extends Command | ||
{ | ||
protected $signature = 'app:backfill-conference-location-names'; | ||
|
||
protected $description = 'Backfill location names for future conferences'; | ||
|
||
public function handle() | ||
{ | ||
$conferences = Conference::query() | ||
->whereAfter(now()) | ||
->whereNotNull(['latitude', 'longitude']) | ||
->whereNull('location_name') | ||
->get(); | ||
|
||
$conferences->each(function ($conference) { | ||
try { | ||
$result = app(Geocoder::class)->geocode($conference->location); | ||
} catch (InvalidAddressGeocodingException $e) { | ||
return; | ||
} | ||
|
||
$conference->location_name = $result->getLocationName(); | ||
$conference->save(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Services\Geocoder; | ||
|
||
use App\Casts\Coordinates; | ||
|
||
class GeocoderResponse | ||
{ | ||
public function __construct(protected $response) {} | ||
|
||
public function getCoordinates(): Coordinates | ||
{ | ||
return new Coordinates( | ||
$this->getCoordinate('lat'), | ||
$this->getCoordinate('lng'), | ||
); | ||
} | ||
|
||
public function getLocationName(): string | ||
{ | ||
$country = $this->getCountry(); | ||
$city = $this->getCity(); | ||
|
||
$values = $country === 'United States' | ||
? [$city, $this->getState(), $country] | ||
: [$city, $country]; | ||
|
||
return collect($values)->filter()->implode(', '); | ||
} | ||
|
||
private function getCoordinate($type) | ||
{ | ||
return data_get($this->response, "results.0.geometry.location.{$type}"); | ||
} | ||
|
||
private function getCity() | ||
{ | ||
return $this->getAddressComponent(['locality', 'postal_town'])['long_name'] ?? null; | ||
} | ||
|
||
private function getState() | ||
{ | ||
return $this->getAddressComponent(['administrative_area_level_1'])['short_name'] ?? null; | ||
} | ||
|
||
private function getCountry() | ||
{ | ||
return $this->getAddressComponent(['country'])['long_name'] ?? null; | ||
} | ||
|
||
private function getAddressComponent(array $types) | ||
{ | ||
return collect(data_get($this->response, 'results.0.address_components', [])) | ||
->firstWhere(fn ($component) => collect($component['types'])->intersect($types)->isNotEmpty()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
database/migrations/2024_12_20_115249_add_location_name_to_conferences_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('conferences', function (Blueprint $table) { | ||
$table->string('location_name')->nullable()->after('location'); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('conferences', function (Blueprint $table) { | ||
$table->dropColumn('location_name'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.