Skip to content

Commit

Permalink
Implement PR+abs (#693)
Browse files Browse the repository at this point in the history
* Implement PR+abs form

* Rename DepartmentalRoad To Road

* retours

* add side

* fix typo

* retours

* catch abscissa exceptions
  • Loading branch information
mmarchois authored Apr 11, 2024
1 parent 12b28fb commit 98c9637
Show file tree
Hide file tree
Showing 46 changed files with 1,473 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,42 @@
</option>
</constraint>
</property>
<property name="fromPointNumber">
<constraint name="When">
<option name="expression"> this.roadType === 'departmentalRoad' </option>
<option name="constraints">
<constraint name="NotBlank"/>
</option>
</constraint>
</property>
<property name="toPointNumber">
<constraint name="When">
<option name="expression"> this.roadType === 'departmentalRoad' </option>
<option name="constraints">
<constraint name="NotBlank"/>
</option>
</constraint>
</property>
<property name="fromAbscissa">
<constraint name="When">
<option name="expression"> this.roadType === 'departmentalRoad' </option>
<option name="constraints">
<constraint name="Type">
<option name="type">integer</option>
</constraint>
</option>
</constraint>
</property>
<property name="toAbscissa">
<constraint name="When">
<option name="expression"> this.roadType === 'departmentalRoad' </option>
<option name="constraints">
<constraint name="Type">
<option name="type">integer</option>
</constraint>
</option>
</constraint>
</property>
<getter property="isEntireStreet">
<constraint name="Type">
<option name="type">boolean</option>
Expand Down
9 changes: 9 additions & 0 deletions src/Application/Exception/AbscissaOutOfRangeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Application\Exception;

class AbscissaOutOfRangeException extends GeocodingFailureException
{
}
9 changes: 9 additions & 0 deletions src/Application/Exception/EndAbscissaOutOfRangeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Application\Exception;

final class EndAbscissaOutOfRangeException extends AbscissaOutOfRangeException
{
}
7 changes: 6 additions & 1 deletion src/Application/Exception/GeocodingFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Application\Exception;

final class GeocodingFailureException extends \Exception
class GeocodingFailureException extends \Exception
{
public function __construct(
string $message = '',
Expand All @@ -18,4 +18,9 @@ public function getLocationIndex(): ?int
{
return $this->locationIndex;
}

public function setLocationIndex(int $index): void
{
$this->locationIndex = $index;
}
}
9 changes: 9 additions & 0 deletions src/Application/Exception/RoadGeocodingFailureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Application\Exception;

final class RoadGeocodingFailureException extends GeocodingFailureException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Application\Exception;

final class StartAbscissaOutOfRangeException extends AbscissaOutOfRangeException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ private function duplicateRegulationMeasures(
$cmd->fromHouseNumber = $location->getFromHouseNumber();
$cmd->toHouseNumber = $location->getToHouseNumber();
$cmd->geometry = $location->getGeometry();
$cmd->fromPointNumber = $location->getFromPointNumber();
$cmd->toPointNumber = $location->getToPointNumber();
$cmd->toAbscissa = $location->getToAbscissa();
$cmd->fromAbscissa = $location->getFromAbscissa();
$cmd->fromSide = $location->getFromSide();
$cmd->toSide = $location->getToSide();
$cmd->measure = $measure;

$locationCommands[] = $cmd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
final class SaveLocationCommand implements CommandInterface
{
public ?string $roadType = null;
public ?string $administrator = null;
public ?string $roadNumber = null;
public ?string $cityCode = null;
public ?string $cityLabel = null;
public ?string $roadName = null;
Expand All @@ -26,8 +24,15 @@ final class SaveLocationCommand implements CommandInterface
public ?Coordinates $toCoords = null;
public ?string $geometry;
public ?Measure $measure;
public ?string $departmentalRoadGeometry = null;
private ?bool $isEntireStreetFormValue = null;
public ?string $administrator = null;
public ?string $roadNumber = null;
public ?string $fromPointNumber = null;
public ?int $fromAbscissa = null;
public ?string $fromSide = null;
public ?string $toPointNumber = null;
public ?int $toAbscissa = null;
public ?string $toSide = null;

public function __construct(
public readonly ?Location $location = null,
Expand All @@ -42,6 +47,12 @@ public function __construct(
$this->toHouseNumber = $location?->getToHouseNumber();
$this->geometry = $location?->getGeometry();
$this->isEntireStreetFormValue = $location ? (!$this->fromHouseNumber && !$this->toHouseNumber) : null;
$this->fromPointNumber = $location?->getFromPointNumber();
$this->fromSide = $location?->getFromSide();
$this->fromAbscissa = $location?->getFromAbscissa();
$this->toPointNumber = $location?->getToPointNumber();
$this->toAbscissa = $location?->getToAbscissa();
$this->toSide = $location?->getToSide();
}

public function clean(): void
Expand All @@ -57,7 +68,12 @@ public function clean(): void
if ($this->roadType === RoadTypeEnum::LANE->value || $this->roadType === null) {
$this->administrator = null;
$this->roadNumber = null;
$this->departmentalRoadGeometry = null;
$this->fromPointNumber = null;
$this->toPointNumber = null;
$this->fromAbscissa = null;
$this->toAbscissa = null;
$this->fromSide = null;
$this->toSide = null;
}

if ($this->roadType === RoadTypeEnum::LANE->value && $this->isEntireStreetFormValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Application\Regulation\Command\Location;

use App\Application\Exception\GeocodingFailureException;
use App\Application\IdFactoryInterface;
use App\Application\LaneSectionMakerInterface;
use App\Application\RoadGeocoderInterface;
use App\Application\RoadSectionMakerInterface;
use App\Domain\Regulation\Enum\RoadTypeEnum;
use App\Domain\Regulation\Location;
use App\Domain\Regulation\Repository\LocationRepositoryInterface;
Expand All @@ -19,6 +19,7 @@ public function __construct(
private LocationRepositoryInterface $locationRepository,
private RoadGeocoderInterface $roadGeocoder,
private LaneSectionMakerInterface $laneSectionMaker,
private RoadSectionMakerInterface $roadSectionMaker,
) {
}

Expand All @@ -31,21 +32,27 @@ public function __invoke(SaveLocationCommand $command): Location
if ($command->roadType === RoadTypeEnum::LANE->value) {
$geometry = empty($command->geometry) ? $this->computeLaneGeometry($command) : $command->geometry;
} else {
$geometry = empty($command->departmentalRoadGeometry) ? $this->computeDepartmentalRoadGeometry($command) : $command->departmentalRoadGeometry;
$geometry = $this->computeRoadSectionGeometry($command);
}

$location = $this->locationRepository->add(
new Location(
uuid: $this->idFactory->make(),
measure: $command->measure,
roadType: $command->roadType,
administrator: $command->administrator,
roadNumber: $command->roadNumber,
cityLabel: $command->cityLabel,
cityCode: $command->cityCode,
roadName: $command->roadName,
fromHouseNumber: $command->fromHouseNumber,
toHouseNumber: $command->toHouseNumber,
administrator: $command->administrator,
fromPointNumber: $command->fromPointNumber,
fromAbscissa: $command->fromAbscissa,
fromSide: $command->fromSide,
toPointNumber: $command->toPointNumber,
toAbscissa: $command->toAbscissa,
toSide: $command->toSide,
geometry: $geometry,
),
);
Expand All @@ -60,20 +67,26 @@ public function __invoke(SaveLocationCommand $command): Location
? $this->computeLaneGeometry($command)
: $command->location->getGeometry();
} else {
$geometry = $this->shouldRecomputeDepartmentalRoadGeometry($command)
? $this->computeDepartmentalRoadGeometry($command)
$geometry = $this->shouldRecomputeRoadSectionGeometry($command)
? $this->computeRoadSectionGeometry($command)
: $command->location->getGeometry();
}

$command->location->update(
roadType: $command->roadType,
administrator: $command->administrator,
roadNumber: $command->roadNumber,
cityCode: $command->cityCode,
cityLabel: $command->cityLabel,
roadName: $command->roadName,
fromHouseNumber: $command->fromHouseNumber,
toHouseNumber: $command->toHouseNumber,
administrator: $command->administrator,
roadNumber: $command->roadNumber,
fromPointNumber: $command->fromPointNumber,
fromAbscissa: $command->fromAbscissa,
fromSide: $command->fromSide,
toPointNumber: $command->toPointNumber,
toAbscissa: $command->toAbscissa,
toSide: $command->toSide,
geometry: $geometry,
);

Expand Down Expand Up @@ -109,18 +122,21 @@ private function computeLaneGeometry(SaveLocationCommand $command): ?string
);
}

private function computeDepartmentalRoadGeometry(SaveLocationCommand $command): ?string
private function computeRoadSectionGeometry(SaveLocationCommand $command): string
{
if ($command->departmentalRoadGeometry) {
return $command->departmentalRoadGeometry;
}

$departmentalRoadNumbers = $this->roadGeocoder->findDepartmentalRoads($command->roadNumber, $command->administrator);
if (!$departmentalRoadNumbers) {
throw new GeocodingFailureException(sprintf('could not retrieve geometry for roadNumber="%s", administrator="%s"', $command->roadNumber, $command->administrator));
}

return $departmentalRoadNumbers[0]['geometry'];
$fullDepartmentalRoadGeometry = $this->roadGeocoder->computeRoad($command->roadNumber, $command->administrator);

return $this->roadSectionMaker->computeSection(
$fullDepartmentalRoadGeometry,
$command->administrator,
$command->roadNumber,
$command->fromPointNumber,
$command->fromSide,
$command->fromAbscissa ?? 0,
$command->toPointNumber,
$command->toSide,
$command->toAbscissa ?? 0,
);
}

private function shouldRecomputeLaneGeometry(SaveLocationCommand $command): bool
Expand All @@ -131,9 +147,15 @@ private function shouldRecomputeLaneGeometry(SaveLocationCommand $command): bool
|| ($command->toHouseNumber !== $command->location->getToHouseNumber());
}

private function shouldRecomputeDepartmentalRoadGeometry(SaveLocationCommand $command): bool
private function shouldRecomputeRoadSectionGeometry(SaveLocationCommand $command): bool
{
return $command->roadNumber !== $command->location->getRoadNumber()
|| $command->administrator !== $command->location->getAdministrator();
|| $command->administrator !== $command->location->getAdministrator()
|| $command->fromPointNumber !== $command->location->getFromPointNumber()
|| $command->toPointNumber !== $command->location->getToPointNumber()
|| $command->fromAbscissa !== $command->location->getFromAbscissa()
|| $command->toAbscissa !== $command->location->getToAbscissa()
|| $command->fromSide !== $command->location->getFromSide()
|| $command->toSide !== $command->location->getToSide();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public function __invoke(SaveMeasureCommand $command): Measure
$location = $this->commandBus->handle($locationCommand);
$locationsStillPresentUuids[] = $location->getUuid();
} catch (GeocodingFailureException $e) {
throw new GeocodingFailureException(sprintf('Geocoding of location #%d has failed', $index), locationIndex: $index, previous: $e);
$e->setLocationIndex($index);
throw $e;
}
}

Expand Down Expand Up @@ -116,7 +117,8 @@ public function __invoke(SaveMeasureCommand $command): Measure
$location = $this->commandBus->handle($locationCommand);
$measure->addLocation($location);
} catch (GeocodingFailureException $e) {
throw new GeocodingFailureException(sprintf('Geocoding of location #%d has failed', $index), locationIndex: $index, previous: $e);
$e->setLocationIndex($index);
throw $e;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/Application/Regulation/View/LocationView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace App\Application\Regulation\View;

final class LocationView
final readonly class LocationView
{
public function __construct(
public readonly ?string $cityCode = null,
public readonly ?string $cityLabel = null,
public readonly ?string $roadName = null,
public readonly ?string $roadNumber = null,
public readonly ?string $administrator = null,
public ?string $cityCode = null,
public ?string $cityLabel = null,
public ?string $roadName = null,
public ?string $roadNumber = null,
public ?string $administrator = null,
) {
}
}
6 changes: 6 additions & 0 deletions src/Application/Regulation/View/Measure/LocationView.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public function __construct(
public ?string $toHouseNumber,
public ?string $administrator,
public ?string $roadNumber,
public ?string $fromPointNumber,
public ?int $fromAbscissa,
public ?string $fromSide,
public ?string $toPointNumber,
public ?int $toAbscissa,
public ?string $toSide,
) {
}
}
6 changes: 6 additions & 0 deletions src/Application/Regulation/View/Measure/MeasureView.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public static function fromEntity(Measure $measure): self
toHouseNumber: $location->getToHouseNumber(),
administrator: $location->getAdministrator(),
roadNumber: $location->getRoadNumber(),
fromPointNumber: $location->getFromPointNumber(),
fromAbscissa: $location->getFromAbscissa() ?? 0,
fromSide: $location->getFromSide(),
toPointNumber: $location->getToPointNumber(),
toAbscissa: $location->getToAbscissa() ?? 0,
toSide: $location->getToSide(),
);
}

Expand Down
15 changes: 14 additions & 1 deletion src/Application/RoadGeocoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@

namespace App\Application;

use App\Domain\Geography\Coordinates;

interface RoadGeocoderInterface
{
public function computeRoadLine(string $roadName, string $inseeCode): string;

public function findDepartmentalRoads(string $search, string $administrator): array;
public function findRoads(string $search, string $administrator): array;

public function computeRoad(string $roadNumber, string $administrator): string;

public function computeReferencePoint(
string $lineGeometry,
string $administrator,
string $roadNumber,
string $pointNumber,
string $side,
int $abscissa,
): Coordinates;
}
Loading

0 comments on commit 98c9637

Please sign in to comment.