Skip to content

Commit

Permalink
fix commande update communes #2107
Browse files Browse the repository at this point in the history
  • Loading branch information
numew committed Jan 12, 2024
1 parent fc02a9f commit 60ae812
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src/Command/UpdateCommunesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Command;

use App\DataFixtures\Loader\LoadCommuneData;
use App\Entity\Commune;
use App\Entity\Territory;
use App\Factory\CommuneFactory;
use App\Service\Import\CsvParser;
use App\Utils\ImportCommune;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -48,7 +48,7 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
foreach ($list as $territory) {
$this->territories[$territory->getZip()] = $territory;
}
$this->csvData = $this->csvParser->parse($this->params->get('kernel.project_dir').LoadCommuneData::COMMUNE_LIST_CSV_PATH);
$this->csvData = $this->csvParser->parse($this->params->get('kernel.project_dir').ImportCommune::COMMUNE_LIST_CSV_PATH);
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -58,9 +58,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$nbDelete = 0;

foreach ($this->csvData as $rowData) {
$itemCodeCommune = $rowData[LoadCommuneData::INDEX_CSV_CODE_COMMUNE];
$itemCodePostal = $rowData[LoadCommuneData::INDEX_CSV_CODE_POSTAL];
$itemNomCommune = $rowData[LoadCommuneData::INDEX_CSV_NOM_COMMUNE];
$itemCodeCommune = $rowData[ImportCommune::INDEX_CSV_CODE_COMMUNE];
$itemCodePostal = $rowData[ImportCommune::INDEX_CSV_CODE_POSTAL];
$itemNomCommune = $rowData[ImportCommune::INDEX_CSV_NOM_COMMUNE];

$keyCommune = $itemCodePostal.'-'.$itemCodeCommune;
if (isset($this->communes[$keyCommune])) {
Expand All @@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

$zipCode = LoadCommuneData::getZipCodeByCodeCommune($itemCodeCommune);
$zipCode = ImportCommune::getZipCodeByCodeCommune($itemCodeCommune);
$new = $this->communeFactory->createInstanceFrom(
territory: $this->territories[$zipCode],
nom: $itemNomCommune,
Expand Down
30 changes: 6 additions & 24 deletions src/DataFixtures/Loader/LoadCommuneData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Manager\CommuneManager;
use App\Manager\TerritoryManager;
use App\Service\Import\CsvParser;
use App\Utils\ImportCommune;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -14,13 +15,6 @@

class LoadCommuneData extends Fixture implements OrderedFixtureInterface
{
// File found here: https://www.data.gouv.fr/fr/datasets/codes-postaux/
public const COMMUNE_LIST_CSV_PATH = '/src/DataFixtures/Files/codespostaux.csv';

public const INDEX_CSV_CODE_POSTAL = 0;
public const INDEX_CSV_CODE_COMMUNE = 1;
public const INDEX_CSV_NOM_COMMUNE = 2;

private const FLUSH_COUNT = 1000;

public function __construct(
Expand All @@ -39,7 +33,7 @@ public function load(ObjectManager $manager): void
$existingCpAndInseeCode = [];
$territory = null;

$csvData = $this->csvParser->parse($this->params->get('kernel.project_dir').self::COMMUNE_LIST_CSV_PATH);
$csvData = $this->csvParser->parse($this->params->get('kernel.project_dir').ImportCommune::COMMUNE_LIST_CSV_PATH);

// Start reading
foreach ($csvData as $rowData) {
Expand All @@ -50,16 +44,16 @@ public function load(ObjectManager $manager): void
continue;
}

$itemCodeCommune = $rowData[self::INDEX_CSV_CODE_COMMUNE];
$itemCodePostal = $rowData[self::INDEX_CSV_CODE_POSTAL];
$itemNomCommune = $rowData[self::INDEX_CSV_NOM_COMMUNE];
$itemCodeCommune = $rowData[ImportCommune::INDEX_CSV_CODE_COMMUNE];
$itemCodePostal = $rowData[ImportCommune::INDEX_CSV_CODE_POSTAL];
$itemNomCommune = $rowData[ImportCommune::INDEX_CSV_NOM_COMMUNE];

$keyCommune = $itemCodePostal.'-'.$itemCodeCommune;
if (!empty($existingCpAndInseeCode[$keyCommune])) {
continue;
}

$zipCode = self::getZipCodeByCodeCommune($itemCodeCommune);
$zipCode = ImportCommune::getZipCodeByCodeCommune($itemCodeCommune);

if (null === $territory || $zipCode != $territory->getZip()) {
$territory = $this->territoryManager->findOneBy(['zip' => $zipCode]);
Expand All @@ -79,18 +73,6 @@ public function load(ObjectManager $manager): void
$this->communeManager->flush();
}

public static function getZipCodeByCodeCommune($itemCodeCommune)
{
$codeCommune = $itemCodeCommune;
$codeCommune = str_pad($codeCommune, 5, '0', \STR_PAD_LEFT);
$zipCode = substr($codeCommune, 0, 2);
if ('97' == $zipCode) {
$zipCode = substr($codeCommune, 0, 3);
}

return $zipCode;
}

public function getOrder(): int
{
return 16;
Expand Down
25 changes: 25 additions & 0 deletions src/Utils/ImportCommune.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Utils;

class ImportCommune
{
// File found here: https://www.data.gouv.fr/fr/datasets/codes-postaux/
public const COMMUNE_LIST_CSV_PATH = '/src/DataFixtures/Files/codespostaux.csv';

public const INDEX_CSV_CODE_POSTAL = 0;
public const INDEX_CSV_CODE_COMMUNE = 1;
public const INDEX_CSV_NOM_COMMUNE = 2;

public static function getZipCodeByCodeCommune($itemCodeCommune)
{
$codeCommune = $itemCodeCommune;
$codeCommune = str_pad($codeCommune, 5, '0', \STR_PAD_LEFT);
$zipCode = substr($codeCommune, 0, 2);
if ('97' == $zipCode) {
$zipCode = substr($codeCommune, 0, 3);
}

return $zipCode;
}
}

0 comments on commit 60ae812

Please sign in to comment.