feat(sites) : brique fondatrice du module Sites (ticket 1/4)
Module Sites optionnel et desactivable via config/modules.php. Entite Site (nom unique, ville, CP FR, couleur hex, adresse), repository + impl Doctrine, migration racine (namespace DoctrineMigrations conforme exception CLAUDE.md), fixtures idempotentes (Chatellerault, Saint-Jean, Pommevic), permissions RBAC sites.view/sites.manage. Tests unitaires + validation via KernelTestCase (UniqueEntity, regex hex et CP, NotBlank, Length). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
185
src/Module/Sites/Domain/Entity/Site.php
Normal file
185
src/Module/Sites/Domain/Entity/Site.php
Normal file
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Sites\Domain\Entity;
|
||||
|
||||
use App\Module\Sites\Infrastructure\Doctrine\DoctrineSiteRepository;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
* Site physique (usine / etablissement) appartenant a l'instance Coltura.
|
||||
*
|
||||
* Brique fondatrice du module Sites : cette entite n'est pas exposee par
|
||||
* une ressource API Platform dans ce ticket (ticket 1/4). Elle sert de socle
|
||||
* de donnees aux tickets suivants (rattachement utilisateurs, affichage
|
||||
* navbar, etc.). Aucune dependance dure depuis le module Core : la table
|
||||
* est creee meme si le module est desactive (voir migration dediee).
|
||||
*/
|
||||
#[ORM\Entity(repositoryClass: DoctrineSiteRepository::class)]
|
||||
#[ORM\Table(name: 'site')]
|
||||
#[ORM\UniqueConstraint(name: 'uniq_site_name', columns: ['name'])]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
#[UniqueEntity(fields: ['name'], message: 'Un site avec ce nom existe deja.')]
|
||||
class Site
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 100)]
|
||||
#[Assert\NotBlank(message: 'Le nom du site est requis.')]
|
||||
#[Assert\Length(max: 100, maxMessage: 'Le nom du site ne peut pas depasser {{ limit }} caracteres.')]
|
||||
private string $name;
|
||||
|
||||
#[ORM\Column(length: 100)]
|
||||
#[Assert\NotBlank(message: 'La ville du site est requise.')]
|
||||
#[Assert\Length(max: 100, maxMessage: 'La ville ne peut pas depasser {{ limit }} caracteres.')]
|
||||
private string $city;
|
||||
|
||||
// Colonne mappee sur le snake_case PostgreSQL (convention projet : noms de
|
||||
// colonnes en minuscules dans le SQL brut). Le format est contraint au
|
||||
// code postal francais strict : 5 chiffres numeriques.
|
||||
#[ORM\Column(name: 'postal_code', length: 10)]
|
||||
#[Assert\NotBlank(message: 'Le code postal est requis.')]
|
||||
#[Assert\Length(max: 10, maxMessage: 'Le code postal ne peut pas depasser {{ limit }} caracteres.')]
|
||||
#[Assert\Regex(
|
||||
pattern: '/^\d{5}$/',
|
||||
message: 'Le code postal doit etre compose de 5 chiffres (format FR).',
|
||||
)]
|
||||
private string $postalCode;
|
||||
|
||||
// Couleur d'identification visuelle du site au format hex #RRGGBB (7 chars
|
||||
// incluant le diese). Utilisee plus tard par la navbar (ticket 3) pour
|
||||
// distinguer les sites d'un coup d'oeil.
|
||||
#[ORM\Column(length: 7)]
|
||||
#[Assert\NotBlank(message: 'La couleur est requise.')]
|
||||
#[Assert\Regex(
|
||||
pattern: '/^#[0-9A-Fa-f]{6}$/',
|
||||
message: 'La couleur doit etre un code hex de 7 caracteres au format #RRGGBB.',
|
||||
)]
|
||||
private string $color;
|
||||
|
||||
// Champ TEXT volontaire : l'adresse complete peut courir sur plusieurs
|
||||
// lignes (voie + complement + mention particuliere). Borne haute a 500
|
||||
// caracteres : une adresse francaise complete tient tres largement dans
|
||||
// cette enveloppe, et la limite applicative protege contre les payloads
|
||||
// anormalement volumineux envoyes par un client (garde DoS basique).
|
||||
#[ORM\Column(name: 'full_address', type: Types::TEXT)]
|
||||
#[Assert\NotBlank(message: 'L\'adresse complete est requise.')]
|
||||
#[Assert\Length(max: 500, maxMessage: 'L\'adresse complete ne peut pas depasser {{ limit }} caracteres.')]
|
||||
private string $fullAddress;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: Types::DATETIME_IMMUTABLE)]
|
||||
private DateTimeImmutable $createdAt;
|
||||
|
||||
#[ORM\Column(name: 'updated_at', type: Types::DATETIME_IMMUTABLE)]
|
||||
private DateTimeImmutable $updatedAt;
|
||||
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $city,
|
||||
string $postalCode,
|
||||
string $color,
|
||||
string $fullAddress,
|
||||
) {
|
||||
$this->name = $name;
|
||||
$this->city = $city;
|
||||
$this->postalCode = $postalCode;
|
||||
$this->color = $color;
|
||||
$this->fullAddress = $fullAddress;
|
||||
$now = new DateTimeImmutable();
|
||||
$this->createdAt = $now;
|
||||
$this->updatedAt = $now;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback Doctrine : a chaque update en base on rafraichit updatedAt.
|
||||
* Ne pas toucher a createdAt ici (immutable apres creation).
|
||||
*/
|
||||
#[ORM\PreUpdate]
|
||||
public function onPreUpdate(): void
|
||||
{
|
||||
$this->updatedAt = new DateTimeImmutable();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCity(): string
|
||||
{
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
public function setCity(string $city): static
|
||||
{
|
||||
$this->city = $city;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPostalCode(): string
|
||||
{
|
||||
return $this->postalCode;
|
||||
}
|
||||
|
||||
public function setPostalCode(string $postalCode): static
|
||||
{
|
||||
$this->postalCode = $postalCode;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getColor(): string
|
||||
{
|
||||
return $this->color;
|
||||
}
|
||||
|
||||
public function setColor(string $color): static
|
||||
{
|
||||
$this->color = $color;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFullAddress(): string
|
||||
{
|
||||
return $this->fullAddress;
|
||||
}
|
||||
|
||||
public function setFullAddress(string $fullAddress): static
|
||||
{
|
||||
$this->fullAddress = $fullAddress;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function getUpdatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->updatedAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Sites\Domain\Repository;
|
||||
|
||||
use App\Module\Sites\Domain\Entity\Site;
|
||||
|
||||
interface SiteRepositoryInterface
|
||||
{
|
||||
public function findById(int $id): ?Site;
|
||||
|
||||
public function findByName(string $name): ?Site;
|
||||
|
||||
/**
|
||||
* @return list<Site>
|
||||
*/
|
||||
public function findAllOrderedByName(): array;
|
||||
|
||||
public function save(Site $site): void;
|
||||
|
||||
public function remove(Site $site): void;
|
||||
}
|
||||
105
src/Module/Sites/Infrastructure/DataFixtures/SitesFixtures.php
Normal file
105
src/Module/Sites/Infrastructure/DataFixtures/SitesFixtures.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Sites\Infrastructure\DataFixtures;
|
||||
|
||||
use App\Module\Sites\Domain\Entity\Site;
|
||||
use App\Module\Sites\Domain\Repository\SiteRepositoryInterface;
|
||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
|
||||
/**
|
||||
* Fixtures du module Sites : 3 etablissements de demonstration utilises par
|
||||
* les tickets suivants (rattachement utilisateurs, navbar, etc.).
|
||||
*
|
||||
* Idempotence supportee : le purger Doctrine (ORMPurger) vide la table
|
||||
* `site` avant chaque `doctrine:fixtures:load`. Si le purger est
|
||||
* desactive et la fixture rejouee telle quelle sur une base deja seedee,
|
||||
* le lookup par nom evite le doublon et re-aligne les autres champs.
|
||||
*
|
||||
* Idempotence NON supportee :
|
||||
* - chargement cumulatif apres qu'une autre fixture ait persiste (sans
|
||||
* flush) des Site dans la meme session : `findByName()` s'appuie sur
|
||||
* `findOneBy`, qui n'inspecte pas les entites en attente dans l'unit-of-work
|
||||
* et peut renvoyer null alors qu'un homonyme est deja manage ;
|
||||
* - renommage d'un site : le nom etant la cle de lookup, modifier
|
||||
* `name` dans cette fixture cree un nouveau site et laisse l'ancien
|
||||
* en base (purger desactive). Les autres champs (city, color, etc.)
|
||||
* sont en revanche bien re-synchronises pour un site retrouve.
|
||||
*/
|
||||
class SitesFixtures extends Fixture
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SiteRepositoryInterface $siteRepository,
|
||||
) {}
|
||||
|
||||
public function load(ObjectManager $manager): void
|
||||
{
|
||||
// Chatellerault : couleur imposee par le ticket (bleu Coltura).
|
||||
$this->ensureSite(
|
||||
$manager,
|
||||
name: 'Chatellerault',
|
||||
city: 'Chatellerault',
|
||||
postalCode: '86100',
|
||||
color: '#056CF2',
|
||||
fullAddress: "1 avenue de l'Europe\n86100 Chatellerault",
|
||||
);
|
||||
|
||||
// Saint-Jean : vert emeraude pour contraster avec le bleu Chatellerault.
|
||||
$this->ensureSite(
|
||||
$manager,
|
||||
name: 'Saint-Jean',
|
||||
city: 'Saint-Jean-de-Sauves',
|
||||
postalCode: '86330',
|
||||
color: '#10B981',
|
||||
fullAddress: "12 route de Poitiers\n86330 Saint-Jean-de-Sauves",
|
||||
);
|
||||
|
||||
// Pommevic : ambre pour une troisieme teinte nettement distincte.
|
||||
$this->ensureSite(
|
||||
$manager,
|
||||
name: 'Pommevic',
|
||||
city: 'Pommevic',
|
||||
postalCode: '82400',
|
||||
color: '#F59E0B',
|
||||
fullAddress: "5 chemin des Peupliers\n82400 Pommevic",
|
||||
);
|
||||
|
||||
$manager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cree le site s'il n'existe pas encore, sinon re-aligne ville, code
|
||||
* postal, couleur et adresse sur les valeurs de reference.
|
||||
*
|
||||
* Note : le nom sert de cle de lookup (il est unique en base) et n'est
|
||||
* donc pas resynchronise. Consequence : renommer un site dans la
|
||||
* fixture cree un nouveau site sans supprimer l'ancien, sauf si le
|
||||
* purger Doctrine est actif (cas nominal de `doctrine:fixtures:load`).
|
||||
*/
|
||||
private function ensureSite(
|
||||
ObjectManager $manager,
|
||||
string $name,
|
||||
string $city,
|
||||
string $postalCode,
|
||||
string $color,
|
||||
string $fullAddress,
|
||||
): Site {
|
||||
$site = $this->siteRepository->findByName($name);
|
||||
|
||||
if (null === $site) {
|
||||
$site = new Site($name, $city, $postalCode, $color, $fullAddress);
|
||||
$manager->persist($site);
|
||||
|
||||
return $site;
|
||||
}
|
||||
|
||||
$site->setCity($city);
|
||||
$site->setPostalCode($postalCode);
|
||||
$site->setColor($color);
|
||||
$site->setFullAddress($fullAddress);
|
||||
|
||||
return $site;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Sites\Infrastructure\Doctrine;
|
||||
|
||||
use App\Module\Sites\Domain\Entity\Site;
|
||||
use App\Module\Sites\Domain\Repository\SiteRepositoryInterface;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Site>
|
||||
*/
|
||||
class DoctrineSiteRepository extends ServiceEntityRepository implements SiteRepositoryInterface
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Site::class);
|
||||
}
|
||||
|
||||
public function findById(int $id): ?Site
|
||||
{
|
||||
return $this->find($id);
|
||||
}
|
||||
|
||||
public function findByName(string $name): ?Site
|
||||
{
|
||||
return $this->findOneBy(['name' => $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<Site>
|
||||
*/
|
||||
public function findAllOrderedByName(): array
|
||||
{
|
||||
/** @var list<Site> $sites */
|
||||
return $this->findBy([], ['name' => 'ASC']);
|
||||
}
|
||||
|
||||
public function save(Site $site): void
|
||||
{
|
||||
$this->getEntityManager()->persist($site);
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
public function remove(Site $site): void
|
||||
{
|
||||
$this->getEntityManager()->remove($site);
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
37
src/Module/Sites/SitesModule.php
Normal file
37
src/Module/Sites/SitesModule.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Sites;
|
||||
|
||||
final class SitesModule
|
||||
{
|
||||
public const string ID = 'sites';
|
||||
public const string LABEL = 'Sites';
|
||||
public const bool REQUIRED = false;
|
||||
|
||||
/**
|
||||
* Liste declarative des permissions RBAC exposees par le module Sites.
|
||||
*
|
||||
* Consommee par la commande `app:sync-permissions` (SyncPermissionsCommand)
|
||||
* qui se charge d'upserter ces entrees dans la table `permission`, de
|
||||
* reactiver les codes precedemment marques orphelins et de marquer comme
|
||||
* orphelins ceux qui ont disparu du code source.
|
||||
*
|
||||
* La cle `module` est auto-injectee par le sync command a partir de
|
||||
* `self::ID`, il est donc inutile de la repeter dans chaque entree.
|
||||
*
|
||||
* Convention de nommage des codes : `module.resource[.sub].action` en
|
||||
* snake_case, le prefixe module devant correspondre exactement a
|
||||
* `self::ID` (verifie par la commande de synchronisation).
|
||||
*
|
||||
* @return array<int, array{code: string, label: string}>
|
||||
*/
|
||||
public static function permissions(): array
|
||||
{
|
||||
return [
|
||||
['code' => 'sites.view', 'label' => 'Voir les sites'],
|
||||
['code' => 'sites.manage', 'label' => 'Gerer les sites (creer, editer, supprimer)'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user