-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
390 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
final class Version20240405172355 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Add epci table'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('CREATE TABLE epci (id INT AUTO_INCREMENT NOT NULL, nom VARCHAR(255) NOT NULL, slug VARCHAR(255) NOT NULL, UNIQUE INDEX slug_unique (slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'); | ||
$this->addSql('ALTER TABLE commune ADD epci_id INT DEFAULT NULL'); | ||
$this->addSql('ALTER TABLE commune ADD CONSTRAINT FK_E2E2D1EE4E7C18CB FOREIGN KEY (epci_id) REFERENCES epci (id)'); | ||
$this->addSql('CREATE INDEX IDX_E2E2D1EE4E7C18CB ON commune (epci_id)'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE commune DROP FOREIGN KEY FK_E2E2D1EE4E7C18CB'); | ||
$this->addSql('DROP TABLE epci'); | ||
$this->addSql('DROP INDEX IDX_E2E2D1EE4E7C18CB ON commune'); | ||
$this->addSql('ALTER TABLE commune DROP epci_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace App\Command; | ||
|
||
use App\Entity\Epci; | ||
use App\Repository\CommuneRepository; | ||
use App\Repository\EpciRepository; | ||
use App\Repository\TerritoryRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\String\Slugger\SluggerInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
#[AsCommand( | ||
name: 'app:load-epci', | ||
description: 'Load EPCI to communes', | ||
)] | ||
class LoadEpciCommand extends Command | ||
{ | ||
public const API_EPCI_ALL_URL = 'https://geo.api.gouv.fr/epcis?fields=nom'; | ||
public const API_EPCI_COMMUNE_URL = 'https://geo.api.gouv.fr/epcis/%d/communes'; | ||
|
||
public function __construct( | ||
private HttpClientInterface $httpClient, | ||
private TerritoryRepository $territoryRepository, | ||
private CommuneRepository $communeRepository, | ||
private EpciRepository $epciRepository, | ||
private EntityManagerInterface $entityManager, | ||
private SluggerInterface $slugger, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$response = $this->httpClient->request('GET', self::API_EPCI_ALL_URL); | ||
if (Response::HTTP_OK !== $response->getStatusCode()) { | ||
$io->error('API failed'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$epciList = json_decode($response->getContent(), true); | ||
$progressBar = new ProgressBar($output, \count($epciList)); | ||
$progressBar->start(); | ||
foreach ($epciList as $epciItem) { | ||
$epci = $this->epciRepository->findOneBy(['nom' => $epciNom = $epciItem['nom']]); | ||
if (!$epci) { | ||
$epci = (new Epci()) | ||
->setNom($epciNom) | ||
->setSlug($this->slugger->slug($epciNom)); | ||
} | ||
$response = $this->httpClient->request( | ||
'GET', | ||
$epciCommunesUrl = sprintf(self::API_EPCI_COMMUNE_URL, $epciItem['code']) | ||
); | ||
|
||
if (Response::HTTP_OK === $response->getStatusCode()) { | ||
$communeList = json_decode($response->getContent(), true); | ||
foreach ($communeList as $communeItem) { | ||
$communes = $this->communeRepository->findByCodesPostaux($communeItem['codesPostaux']); | ||
foreach ($communes as $commune) { | ||
$epci->addCommune($commune); | ||
} | ||
} | ||
$this->entityManager->flush(); | ||
} else { | ||
$io->error(sprintf('API failed for: %s', $epciCommunesUrl)); | ||
} | ||
$progressBar->advance(); | ||
} | ||
|
||
$progressBar->finish(); | ||
$this->entityManager->flush(); | ||
$nbCommunesWithECPI = $this->communeRepository->count([]); | ||
$nbCommunesWithoutECPI = $this->communeRepository->count(['epci' => null]); | ||
$io->success(sprintf( | ||
'EPCI loaded with %d communes that belong to EPCI', | ||
$nbCommunesWithECPI - $nbCommunesWithoutECPI | ||
)); | ||
if ($nbCommunesWithoutECPI > 0) { | ||
$io->warning(sprintf( | ||
'%d communes do not belong to EPCI, the code postal might be obsolete.', | ||
$nbCommunesWithoutECPI | ||
)); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use App\Repository\EpciRepository; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Doctrine\ORM\Mapping\UniqueConstraint; | ||
|
||
#[ORM\Entity(repositoryClass: EpciRepository::class)] | ||
#[UniqueConstraint(name: 'slug_unique', columns: ['slug'])] | ||
class Epci | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column] | ||
private ?int $id = null; | ||
|
||
#[ORM\Column(length: 255)] | ||
private ?string $nom = null; | ||
|
||
#[ORM\Column(length: 255)] | ||
private ?string $slug = null; | ||
|
||
#[ORM\OneToMany(mappedBy: 'epci', targetEntity: Commune::class)] | ||
private Collection $communes; | ||
|
||
public function __construct() | ||
{ | ||
$this->communes = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getNom(): ?string | ||
{ | ||
return $this->nom; | ||
} | ||
|
||
public function setNom(string $nom): static | ||
{ | ||
$this->nom = $nom; | ||
|
||
return $this; | ||
} | ||
|
||
public function getSlug(): ?string | ||
{ | ||
return $this->slug; | ||
} | ||
|
||
public function setSlug(string $slug): static | ||
{ | ||
$this->slug = strtolower($slug); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return Collection<int, Commune> | ||
*/ | ||
public function getCommunes(): Collection | ||
{ | ||
return $this->communes; | ||
} | ||
|
||
public function addCommune(Commune $commune): static | ||
{ | ||
if (!$this->communes->contains($commune)) { | ||
$this->communes->add($commune); | ||
$commune->setEpci($this); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function removeCommune(Commune $commune): static | ||
{ | ||
if ($this->communes->removeElement($commune)) { | ||
// set the owning side to null (unless already changed) | ||
if ($commune->getEpci() === $this) { | ||
$commune->setEpci(null); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Repository; | ||
|
||
use App\Entity\Epci; | ||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
|
||
/** | ||
* @extends ServiceEntityRepository<Epci> | ||
* | ||
* @method Epci|null find($id, $lockMode = null, $lockVersion = null) | ||
* @method Epci|null findOneBy(array $criteria, array $orderBy = null) | ||
* @method Epci[] findAll() | ||
* @method Epci[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | ||
*/ | ||
class EpciRepository extends ServiceEntityRepository | ||
{ | ||
public function __construct(ManagerRegistry $registry) | ||
{ | ||
parent::__construct($registry, Epci::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.