Skip to content

Commit

Permalink
fixes based on comments #3322
Browse files Browse the repository at this point in the history
  • Loading branch information
emilschn committed Nov 28, 2024
1 parent 1c2f1cb commit 7767758
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/Command/InitIdBanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)]
class InitIdBanCommand extends Command
{
private const float SCORE_IF_ACCEPTED = 0.9;
public const float SCORE_IF_ACCEPTED = 0.9;
private const int BATCH_SIZE = 20;

public function __construct(
Expand Down Expand Up @@ -52,19 +52,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/** @var Signalement $signalement */
foreach ($listSignalementBanIdNull as $signalement) {
$bestAddressResult = $this->addressService->getAddress($signalement->getAddressCompleteOccupant());
$bestAddressResult = $this->addressService->getAddress($signalement->getAddressCompleteOccupant(), 1);
if ($bestAddressResult->getScore() > self::SCORE_IF_ACCEPTED) {
$signalement->setBanIdOccupant($bestAddressResult->getBanId());
++$nb;
} else {
$signalement->setBanIdOccupant(0);
}
$this->entityManager->persist($signalement);
$progressBar->advance();
}
$this->entityManager->flush();
}

$progressBar->finish();
$nbSignalementWithoutBanId = $this->signalementRepository->count(['banIdOccupant' => null]);
$nbSignalementWithoutBanId = $this->signalementRepository->count(['banIdOccupant' => '0']);
$io->success(\sprintf(
'%s BAN IDs have been initialized, but %s signalements remain with no BAN ID',
$nb,
Expand Down
13 changes: 9 additions & 4 deletions src/Service/DataGouv/AddressService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@
class AddressService
{
private const string API_URL = 'https://api-adresse.data.gouv.fr/search/?q=';
private const string API_PARAM_LIMIT = '&limit=';

public function __construct(
private readonly HttpClientInterface $httpClient,
private readonly LoggerInterface $logger,
) {
}

public function searchAddress(string $query): ?array
public function searchAddress(string $query, ?int $limit = null): ?array
{
try {
$response = $this->httpClient->request('GET', self::API_URL.urlencode($query));
$url = self::API_URL.urlencode($query);
if (!empty($limit)) {
$url .= self::API_PARAM_LIMIT.$limit;
}
$response = $this->httpClient->request('GET', $url);

if (Response::HTTP_OK === $response->getStatusCode()) {
return $response->toArray();
Expand All @@ -32,8 +37,8 @@ public function searchAddress(string $query): ?array
return null;
}

public function getAddress(string $address): Address
public function getAddress(string $address, ?int $limit = null): Address
{
return new Address($this->searchAddress($address));
return new Address($this->searchAddress($address, $limit));
}
}
18 changes: 18 additions & 0 deletions src/Service/Signalement/SignalementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Service\Signalement;

use App\Command\InitIdBanCommand;
use App\Dto\Request\Signalement\SignalementDraftRequest;
use App\Entity\Enum\ChauffageType;
use App\Entity\Enum\OccupantLink;
Expand All @@ -24,6 +25,7 @@
use App\Repository\DesordrePrecisionRepository;
use App\Repository\TerritoryRepository;
use App\Serializer\SignalementDraftRequestSerializer;
use App\Service\DataGouv\AddressService;
use App\Service\Signalement\DesordreTraitement\DesordreCompositionLogementLoader;
use App\Service\Signalement\DesordreTraitement\DesordreTraitementProcessor;
use App\Service\Signalement\Qualification\SignalementQualificationUpdater;
Expand Down Expand Up @@ -54,6 +56,7 @@ public function __construct(
private CriticiteCalculator $criticiteCalculator,
private SignalementQualificationUpdater $signalementQualificationUpdater,
private DesordreCompositionLogementLoader $desordreCompositionLogementLoader,
private AddressService $addressService,
) {
}

Expand Down Expand Up @@ -331,6 +334,21 @@ private function setAddressData(): void
)
->setAdresseAutreOccupant($this->signalementDraftRequest->getAdresseLogementComplementAdresseAutre())
->setManualAddressOccupant($this->signalementDraftRequest->getAdresseLogementAdresseDetailManual());

// Update geoloc and BAN ID if manual edit
if ($this->signalement->getManualAddressOccupant()) {
$addressResult = $this->addressService->getAddress($this->signalement->getAddressCompleteOccupant(), 1);
if ($addressResult->getScore() > InitIdBanCommand::SCORE_IF_ACCEPTED) {
$this->signalement->setBanIdOccupant($addressResult->getBanId());
} else {
$this->signalement->setBanIdOccupant(0);
}
$inseeResult = $this->addressService->getAddress($this->signalement->getCpOccupant().' '.$this->signalement->getVilleOccupant(), 1);
$this->signalement->setGeoloc([
'lat' => $inseeResult->getLatitude(),
'lng' => $inseeResult->getLongitude(),
]);
}
}

private function setOccupantDeclarantData(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Repository\DesordrePrecisionRepository;
use App\Repository\TerritoryRepository;
use App\Serializer\SignalementDraftRequestSerializer;
use App\Service\DataGouv\AddressService;
use App\Service\Signalement\CriticiteCalculator;
use App\Service\Signalement\DesordreTraitement\DesordreCompositionLogementLoader;
use App\Service\Signalement\DesordreTraitement\DesordreTraitementProcessor;
Expand Down Expand Up @@ -55,6 +56,7 @@ protected function setUp(): void
$criticiteCalculator = static::getContainer()->get(CriticiteCalculator::class);
$signalementQualificationUpdater = static::getContainer()->get(SignalementQualificationUpdater::class);
$desordreCompositionLogementLoader = static::getContainer()->get(DesordreCompositionLogementLoader::class);
$addressService = static::getContainer()->get(AddressService::class);

$this->signalementBuilder = new SignalementBuilder(
$territoryRepository,
Expand All @@ -71,7 +73,8 @@ protected function setUp(): void
$desordreCritereManager,
$criticiteCalculator,
$signalementQualificationUpdater,
$desordreCompositionLogementLoader
$desordreCompositionLogementLoader,
$addressService,
);
}

Expand Down

0 comments on commit 7767758

Please sign in to comment.