-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC: Lien vers la localisation dans Waze
- Loading branch information
1 parent
cafbb7f
commit 8791495
Showing
6 changed files
with
87 additions
and
3 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
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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Adapter; | ||
|
||
use App\Application\Regulation\View\Measure\MeasureView; | ||
use Doctrine\DBAL\ArrayParameterType; | ||
use Doctrine\DBAL\Connection; | ||
|
||
final class WazeUrlMaker | ||
{ | ||
public function __construct( | ||
private Connection $connection, | ||
) { | ||
} | ||
|
||
/** | ||
* @param MeasureView[] $measures | ||
*/ | ||
public function makeAll(array $measures): array | ||
{ | ||
$urlsByMeasureAndLocation = []; | ||
$hashes = []; | ||
$geometries = []; | ||
|
||
foreach ($measures as $measure) { | ||
$urlsByMeasureAndLocation[$measure->uuid] = []; | ||
|
||
foreach ($measure->locations as $location) { | ||
$hashes[] = implode('#', [$measure->uuid, $location->uuid]); | ||
$geometries[] = $location->geometry; | ||
} | ||
} | ||
|
||
// Use a single SQL query | ||
$urls = $this->bulkBuildUrls($geometries); | ||
|
||
foreach ($urls as $index => $url) { | ||
$hash = $hashes[$index]; | ||
[$measureUuid, $locationUuid] = explode('#', $hash); | ||
$urlsByMeasureAndLocation[$measureUuid][$locationUuid] = $url; | ||
} | ||
|
||
return $urlsByMeasureAndLocation; | ||
} | ||
|
||
private function bulkBuildUrls(array $geometries): array | ||
{ | ||
$rows = $this->connection->fetchAllAssociative( | ||
'SELECT ST_AsGeoJSON(ST_Centroid(g)) AS geom | ||
FROM unnest(ARRAY[:geometries]) AS g', | ||
['geometries' => $geometries], | ||
['geometries' => ArrayParameterType::STRING], | ||
); | ||
|
||
$urls = []; | ||
|
||
foreach ($rows as $row) { | ||
$lonLat = json_decode($row['geom'], true)['coordinates']; | ||
|
||
$urls[] = 'https://www.waze.com/en/live-map/directions?' . http_build_query([ | ||
'latlng' => \sprintf('%.6f,%.6f', $lonLat[1], $lonLat[0]), | ||
]); | ||
} | ||
|
||
return $urls; | ||
} | ||
} |
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