diff --git a/src/Command/UpdateCommunesCommand.php b/src/Command/UpdateCommunesCommand.php index 4e73e5aaf..22d30fdea 100644 --- a/src/Command/UpdateCommunesCommand.php +++ b/src/Command/UpdateCommunesCommand.php @@ -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; @@ -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 @@ -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])) { @@ -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, diff --git a/src/DataFixtures/Loader/LoadCommuneData.php b/src/DataFixtures/Loader/LoadCommuneData.php index bc567d959..8c67eea22 100644 --- a/src/DataFixtures/Loader/LoadCommuneData.php +++ b/src/DataFixtures/Loader/LoadCommuneData.php @@ -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; @@ -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( @@ -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) { @@ -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]); @@ -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; diff --git a/src/Utils/ImportCommune.php b/src/Utils/ImportCommune.php new file mode 100644 index 000000000..0a728b51b --- /dev/null +++ b/src/Utils/ImportCommune.php @@ -0,0 +1,25 @@ +