Skip to content

Commit

Permalink
POC: Lien vers la localisation dans Waze
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Dec 18, 2024
1 parent cafbb7f commit 8791495
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Application/Regulation/View/Measure/LocationView.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public function __construct(
public string $uuid,
public string $roadType,
public string $geometry,
public ?NamedStreetView $namedStreet = null,
public ?NumberedRoadView $numberedRoad = null,
public ?RawGeoJSONView $rawGeoJSON = null,
Expand Down
3 changes: 3 additions & 0 deletions src/Application/Regulation/View/Measure/MeasureView.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static function fromEntity(Measure $measure): self
$locations[] = new LocationView(
uuid: $location->getUuid(),
roadType: $location->getRoadType(),
geometry: $location->getGeometry(),
namedStreet: new NamedStreetView(
cityLabel: $namedStreet->getCityLabel(),
roadName: $namedStreet->getRoadName(),
Expand All @@ -66,6 +67,7 @@ public static function fromEntity(Measure $measure): self
$locations[] = new LocationView(
uuid: $location->getUuid(),
roadType: $location->getRoadType(),
geometry: $location->getGeometry(),
numberedRoad: new NumberedRoadView(
administrator: $numberedRoad->getAdministrator(),
roadNumber: $numberedRoad->getRoadNumber(),
Expand All @@ -81,6 +83,7 @@ public static function fromEntity(Measure $measure): self
$locations[] = new LocationView(
uuid: $location->getUuid(),
roadType: $location->getRoadType(),
geometry: $location->getGeometry(),
rawGeoJSON: new RawGeoJSONView(
label: $rawGeoJSON->getLabel(),
),
Expand Down
69 changes: 69 additions & 0 deletions src/Infrastructure/Adapter/WazeUrlMaker.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Domain\Regulation\Specification\CanOrganizationAccessToRegulation;
use App\Domain\Regulation\Specification\CanRegulationOrderRecordBePublished;
use App\Domain\Regulation\Specification\CanViewRegulationDetail;
use App\Infrastructure\Adapter\WazeUrlMaker;
use App\Infrastructure\Security\SymfonyUser;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -28,6 +29,7 @@ public function __construct(
private CanViewRegulationDetail $canViewRegulationDetail,
private CanDeleteMeasures $canDeleteMeasures,
private CanRegulationOrderRecordBePublished $canRegulationOrderRecordBePublished,
private WazeUrlMaker $wazeUrlMaker,
CanOrganizationAccessToRegulation $canOrganizationAccessToRegulation,
Security $security,
) {
Expand Down Expand Up @@ -58,6 +60,7 @@ public function __invoke(string $uuid): Response
$organizationUuid = $regulationOrderRecord->getOrganizationUuid();
$measures = $this->queryBus->handle(new GetMeasuresQuery($uuid));
$isReadOnly = !($currentUser && $this->canOrganizationAccessToRegulation->isSatisfiedBy($organizationUuid, $currentUser->getUserOrganizationUuids()));
$wazeUrls = $this->wazeUrlMaker->makeAll($measures);

$context = [
'uuid' => $uuid,
Expand All @@ -69,6 +72,7 @@ public function __invoke(string $uuid): Response
'isPermanent' => $regulationOrderRecord->getRegulationOrder()->isPermanent(),
'measures' => $measures,
'regulationOrderRecord' => $regulationOrderRecord,
'wazeUrls' => $wazeUrls,
];

return new Response(
Expand Down
2 changes: 1 addition & 1 deletion templates/regulation/detail.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<ul id="measure_list" class="fr-raw-list">
{% for measure in measures %}
<li class="fr-mx-n4v fr-mx-md-0">
{% include "regulation/fragments/_measure.html.twig" with { isReadOnly, measure, generalInfo, canDelete } only %}
{% include "regulation/fragments/_measure.html.twig" with { isReadOnly, measure, generalInfo, canDelete, wazeUrls: wazeUrls[measure.uuid] } only %}
</li>
{% endfor %}
</ul>
Expand Down
11 changes: 9 additions & 2 deletions templates/regulation/fragments/_measure.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@
</ul>
<ul class="fr-raw-list d-stack" style="--stack-gap: var(--1w)">
{% for location in measure.locations %}
<li data-location-uuid="{{ location.uuid }}">
<li data-location-uuid="{{ location.uuid }}" class="fr-grid-row">
<span class="app-card__img fr-icon-map-pin-2-line fr-x-icon--dark-border fr-pr-1w" aria-hidden="true"></span>
{% include 'regulation/_location_line.html.twig' with { location } only %}
<span class="fr-grid-row">
<span class="fr-pr-1w">
{% include 'regulation/_location_line.html.twig' with { location } only %}
</span>
<a href="{{ wazeUrls[location.uuid] }}" target="_blank">
<small>Voir dans Waze</small>
</a>
</span>
</li>
{% endfor %}
</ul>
Expand Down

0 comments on commit 8791495

Please sign in to comment.