Skip to content

Commit

Permalink
factorize function to update ban id #3350
Browse files Browse the repository at this point in the history
  • Loading branch information
emilschn committed Dec 2, 2024
1 parent 77ce9a8 commit 17bb51b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
13 changes: 3 additions & 10 deletions src/Command/InitIdBanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Command;

use App\Entity\Signalement;
use App\Manager\SignalementManager;
use App\Repository\SignalementRepository;
use App\Service\DataGouv\AddressService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -19,11 +19,10 @@
)]
class InitIdBanCommand extends Command
{
public const float SCORE_IF_ACCEPTED = 0.9;
private const int BATCH_SIZE = 20;

public function __construct(
private readonly AddressService $addressService,
private readonly SignalementManager $signalementManager,
private readonly SignalementRepository $signalementRepository,
private readonly EntityManagerInterface $entityManager,
) {
Expand Down Expand Up @@ -52,13 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/** @var Signalement $signalement */
foreach ($listSignalementBanIdNull as $signalement) {
$bestAddressResult = $this->addressService->getAddress($signalement->getAddressCompleteOccupant());
if ($bestAddressResult->getScore() > self::SCORE_IF_ACCEPTED) {
$signalement->setBanIdOccupant($bestAddressResult->getBanId());
++$nb;
} else {
$signalement->setBanIdOccupant(0);
}
$this->signalementManager->updateBanIdOccupantFromAddressComplete($signalement);
$progressBar->advance();
}
$this->entityManager->flush();
Expand Down
20 changes: 13 additions & 7 deletions src/Manager/SignalementManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Manager;

use App\Command\InitIdBanCommand;
use App\Dto\Request\Signalement\AdresseOccupantRequest;
use App\Dto\Request\Signalement\CompositionLogementRequest;
use App\Dto\Request\Signalement\CoordonneesBailleurRequest;
Expand Down Expand Up @@ -53,6 +52,8 @@

class SignalementManager extends AbstractManager
{
public const float SCORE_IF_BAN_ID_ACCEPTED = 0.9;

public function __construct(
protected ManagerRegistry $managerRegistry,
private Security $security,
Expand Down Expand Up @@ -194,6 +195,16 @@ public function updateAddressOccupantFromAddress(Signalement $signalement, Addre
}
}

public function updateBanIdOccupantFromAddressComplete(Signalement $signalement): void
{
$addressResult = $this->addressService->getAddress($signalement->getAddressCompleteOccupant());
if ($addressResult->getScore() > self::SCORE_IF_BAN_ID_ACCEPTED) {
$signalement->setBanIdOccupant($addressResult->getBanId());
} else {
$signalement->setBanIdOccupant(0);
}
}

public function findAllPartners(Signalement $signalement): array
{
/** @var PartnerRepository $partnerRepository */
Expand Down Expand Up @@ -367,12 +378,7 @@ public function updateFromAdresseOccupantRequest(
}
}

$addressResult = $this->addressService->getAddress($signalement->getAddressCompleteOccupant());
if ($addressResult->getScore() > InitIdBanCommand::SCORE_IF_ACCEPTED) {
$signalement->setBanIdOccupant($addressResult->getBanId());
} else {
$signalement->setBanIdOccupant(0);
}
$this->updateBanIdOccupantFromAddressComplete($signalement);

$this->save($signalement);

Expand Down
12 changes: 4 additions & 8 deletions src/Service/Signalement/SignalementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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 @@ -20,6 +19,7 @@
use App\Factory\Signalement\SituationFoyerFactory;
use App\Factory\Signalement\TypeCompositionLogementFactory;
use App\Manager\DesordreCritereManager;
use App\Manager\SignalementManager;
use App\Repository\BailleurRepository;
use App\Repository\DesordreCritereRepository;
use App\Repository\DesordrePrecisionRepository;
Expand Down Expand Up @@ -57,6 +57,7 @@ public function __construct(
private SignalementQualificationUpdater $signalementQualificationUpdater,
private DesordreCompositionLogementLoader $desordreCompositionLogementLoader,
private AddressService $addressService,
private SignalementManager $signalementManager,
) {
}

Expand Down Expand Up @@ -321,7 +322,6 @@ private function setAddressData(): void
->setAdresseOccupant($this->signalementDraftRequest->getAdresseLogementAdresseDetailNumero())
->setCpOccupant($this->signalementDraftRequest->getAdresseLogementAdresseDetailCodePostal())
->setInseeOccupant($this->signalementDraftRequest->getAdresseLogementAdresseDetailInsee())
->setBanIdOccupant($this->signalementDraftRequest->getAdresseLogementAdresseDetailBanID())
->setGeoloc([
'lat' => $this->signalementDraftRequest->getAdresseLogementAdresseDetailGeolocLat(),
'lng' => $this->signalementDraftRequest->getAdresseLogementAdresseDetailGeolocLng(),
Expand All @@ -335,12 +335,8 @@ private function setAddressData(): void
->setAdresseAutreOccupant($this->signalementDraftRequest->getAdresseLogementComplementAdresseAutre())
->setManualAddressOccupant($this->signalementDraftRequest->getAdresseLogementAdresseDetailManual());

$addressResult = $this->addressService->getAddress($this->signalement->getAddressCompleteOccupant());
if ($addressResult->getScore() > InitIdBanCommand::SCORE_IF_ACCEPTED) {
$this->signalement->setBanIdOccupant($addressResult->getBanId());
} else {
$this->signalement->setBanIdOccupant(0);
}
$this->signalementManager->updateBanIdOccupantFromAddressComplete($this->signalement);

$inseeResult = $this->addressService->getAddress($this->signalement->getCpOccupant().' '.$this->signalement->getVilleOccupant());
$this->signalement->setGeoloc([
'lat' => $inseeResult->getLatitude(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Factory\Signalement\SituationFoyerFactory;
use App\Factory\Signalement\TypeCompositionLogementFactory;
use App\Manager\DesordreCritereManager;
use App\Manager\SignalementManager;
use App\Repository\BailleurRepository;
use App\Repository\DesordreCritereRepository;
use App\Repository\DesordrePrecisionRepository;
Expand Down Expand Up @@ -57,6 +58,7 @@ protected function setUp(): void
$signalementQualificationUpdater = static::getContainer()->get(SignalementQualificationUpdater::class);
$desordreCompositionLogementLoader = static::getContainer()->get(DesordreCompositionLogementLoader::class);
$addressService = static::getContainer()->get(AddressService::class);
$signalementManager = static::getContainer()->get(SignalementManager::class);

$this->signalementBuilder = new SignalementBuilder(
$territoryRepository,
Expand All @@ -75,6 +77,7 @@ protected function setUp(): void
$signalementQualificationUpdater,
$desordreCompositionLogementLoader,
$addressService,
$signalementManager,
);
}

Expand Down

0 comments on commit 17bb51b

Please sign in to comment.