feat(catalog) : M7 — StorageProvider + StorageProcessor (liste paginée + filtres, 409 unicité RG-7.01, normalisation numéro) (ERP-213)
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Catalog\Application\Service;
|
||||
|
||||
/**
|
||||
* Normalisation serveur des champs texte d'un Storage, appliquee par le
|
||||
* StorageProcessor AVANT l'unicite metier et la persistance (RG-7.06). Jumeau du
|
||||
* ProductFieldNormalizer (M6), recentre sur l'unique champ texte du stockage.
|
||||
*
|
||||
* - numero : trim simple, SANS changement de casse (HP-M7-05 : pas d'UPPER par
|
||||
* defaut, contrairement au code produit). Le numero est saisi tel quel et sert
|
||||
* l'unicite metier (site, type, numero) parmi les actifs (RG-7.01).
|
||||
*
|
||||
* La methode est null-safe et trim l'entree ; une chaine vide apres trim devient
|
||||
* null (c'est l'Assert\NotBlank de l'entite qui rejette le vide, pas le normalizer).
|
||||
*/
|
||||
final class StorageFieldNormalizer
|
||||
{
|
||||
/**
|
||||
* Numero de stockage trimme (RG-7.06), sans changement de casse (HP-M7-05).
|
||||
* Conserve null tel quel ; une chaine vide apres trim devient null.
|
||||
*/
|
||||
public function normalizeNumero(?string $value): ?string
|
||||
{
|
||||
if (null === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = trim($value);
|
||||
|
||||
return '' === $value ? null : $value;
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,44 @@ declare(strict_types=1);
|
||||
namespace App\Module\Catalog\Domain\Repository;
|
||||
|
||||
use App\Module\Catalog\Domain\Entity\Storage;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
|
||||
interface StorageRepositoryInterface
|
||||
{
|
||||
public function findById(int $id): ?Storage;
|
||||
|
||||
public function save(Storage $storage): void;
|
||||
|
||||
/**
|
||||
* Vrai si un stockage actif (deleted_at IS NULL) porte deja le triplet
|
||||
* (site, storageType, numero). `$excludeId` exclut un stockage precis du test
|
||||
* (cas PATCH). Garantit l'unicite metier parmi les actifs (RG-7.01, index
|
||||
* partiel uq_storage_site_type_numero_active). Un numero redevient disponible
|
||||
* apres soft-delete (le test ignore les supprimes).
|
||||
*/
|
||||
public function existsActiveBySiteTypeNumero(
|
||||
int $siteId,
|
||||
int $storageTypeId,
|
||||
string $numero,
|
||||
?int $excludeId = null,
|
||||
): bool;
|
||||
|
||||
/**
|
||||
* QueryBuilder de la liste stockages (consomme par le StorageProvider) : exclut
|
||||
* par defaut les soft-deleted (RG-7.07), trie par site.code ASC, storageType.label
|
||||
* ASC, numero ASC (defaut spec § 4.1) et applique les filtres optionnels :
|
||||
* - `$search` : recherche partielle case-insensitive sur `numero`.
|
||||
* - `$siteIds` : stockage rattache a AU MOINS UN des sites passes.
|
||||
* - `$storageTypeId` : restreint a un type de stockage precis (par id).
|
||||
* - `$state` : appartenance a la colonne JSONB `states` (RECEPTION|PRODUCTION|TRIAGE).
|
||||
*
|
||||
* @param list<int> $siteIds
|
||||
*/
|
||||
public function createListQueryBuilder(
|
||||
bool $includeDeleted = false,
|
||||
?string $search = null,
|
||||
array $siteIds = [],
|
||||
?int $storageTypeId = null,
|
||||
?string $state = null,
|
||||
): QueryBuilder;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Catalog\Infrastructure\ApiPlatform\State\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Module\Catalog\Application\Service\StorageFieldNormalizer;
|
||||
use App\Module\Catalog\Domain\Entity\Storage;
|
||||
use App\Module\Catalog\Domain\Repository\StorageRepositoryInterface;
|
||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||
use Throwable;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Processor d'ecriture du stockage (M7, POST / PATCH). Cf. spec-back M7 § 4.3 /
|
||||
* § 4.4 + RG-7.01 / RG-7.06. Jumeau du ProductProcessor (normalisation serveur +
|
||||
* 409 doublon).
|
||||
*
|
||||
* Sequence (POST / PATCH) :
|
||||
* 1. Normalisation serveur (RG-7.06) via StorageFieldNormalizer : numero trim
|
||||
* (pas d'UPPER — HP-M7-05). Jouee AVANT l'unicite et la persistance ; la
|
||||
* validation (NotNull site/type, NotBlank/Length numero, Count/Choice states
|
||||
* RG-7.04) a deja joue cote API Platform sur la saisie brute.
|
||||
* 2. RG-7.01 : unicite metier du triplet (site, storageType, numero) parmi les
|
||||
* actifs. Pre-check deterministe (excluant le stockage courant en PATCH) -> 409 ;
|
||||
* l'index partiel uq_storage_site_type_numero_active reste le filet anti-race
|
||||
* au flush.
|
||||
* 3. Persistance via le persist_processor Doctrine ORM.
|
||||
*
|
||||
* RG-7.03 (« le type doit etre disponible sur le site choisi ») n'est PAS portee :
|
||||
* le concept type<->site a ete retire du modele en M6 (StorageType rendu plat,
|
||||
* jointure storage_type_site droppee — migration Version20260626100000). C'est
|
||||
* desormais l'entite Storage (1 site + 1 type) qui materialise cette disponibilite ;
|
||||
* il n'existe plus de referentiel a interroger. A reclarifier cote spec (signale).
|
||||
*
|
||||
* Mode strict PATCH (RETEX M1) : la security d'operation exige `catalog.storages.manage`
|
||||
* pour TOUS les champs ecrivables (un seul niveau de permission au M7 — admin-only).
|
||||
* Aucun champ « hors-permission » a re-gater finement ici : le 403 global est porte
|
||||
* par la security d'operation.
|
||||
*
|
||||
* @implements ProcessorInterface<Storage, Storage>
|
||||
*/
|
||||
final class StorageProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||
private readonly ProcessorInterface $persistProcessor,
|
||||
private readonly StorageFieldNormalizer $normalizer,
|
||||
#[Autowire(service: 'App\Module\Catalog\Infrastructure\Doctrine\DoctrineStorageRepository')]
|
||||
private readonly StorageRepositoryInterface $repository,
|
||||
) {}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
||||
{
|
||||
if (!$data instanceof Storage) {
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
// 1. RG-7.06 : normalisation serveur (numero trim, pas d'UPPER).
|
||||
$this->normalize($data);
|
||||
|
||||
// 2. RG-7.01 : unicite metier (site, storageType, numero) parmi les actifs
|
||||
// (exclut le stockage courant en PATCH). Pre-check explicite -> 409
|
||||
// deterministe. Le NotNull site/type + NotBlank numero ont deja joue.
|
||||
$siteId = $data->getSite()?->getId();
|
||||
$typeId = $data->getStorageType()?->getId();
|
||||
$numero = (string) $data->getNumero();
|
||||
if (null !== $siteId && null !== $typeId && '' !== $numero
|
||||
&& $this->repository->existsActiveBySiteTypeNumero($siteId, $typeId, $numero, $data->getId())) {
|
||||
throw $this->duplicateConflict($numero);
|
||||
}
|
||||
|
||||
// 3. Persistance, avec filet anti-race sur l'index partiel.
|
||||
try {
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
} catch (UniqueConstraintViolationException $e) {
|
||||
// Insertion concurrente du meme triplet entre le pre-check et le flush
|
||||
// (collision sur uq_storage_site_type_numero_active).
|
||||
throw $this->duplicateConflict($numero, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalisation serveur du stockage (RG-7.06). Le setter n'est touche que si une
|
||||
* valeur est presente, pour ne jamais ecraser l'existant lors d'un PATCH partiel.
|
||||
* Le cast (string) est sur : NotBlank a deja rejete le vide en amont.
|
||||
*/
|
||||
private function normalize(Storage $data): void
|
||||
{
|
||||
if (null !== $data->getNumero()) {
|
||||
$data->setNumero((string) $this->normalizer->normalizeNumero($data->getNumero()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* RG-7.01 : 409 sur doublon (site, type, numero). Le front mappe ce conflit sur
|
||||
* le champ `numero` (setError('numero', ...) + toast — convention useFormErrors
|
||||
* ERP-101) : le propertyPath exploitable est `numero`.
|
||||
*/
|
||||
private function duplicateConflict(string $numero, ?Throwable $previous = null): ConflictHttpException
|
||||
{
|
||||
return new ConflictHttpException(
|
||||
sprintf('Un stockage portant le numéro « %s » existe déjà pour ce site et ce type de stockage.', $numero),
|
||||
$previous,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Catalog\Infrastructure\ApiPlatform\State\Provider;
|
||||
|
||||
use ApiPlatform\Doctrine\Orm\Paginator;
|
||||
use ApiPlatform\Metadata\CollectionOperationInterface;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\Pagination\Pagination;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Module\Catalog\Domain\Entity\Storage;
|
||||
use App\Module\Catalog\Domain\Repository\StorageRepositoryInterface;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
use function in_array;
|
||||
use function is_int;
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
* Provider Storage (lecture, ERP-213) :
|
||||
* - LISTE : exclut par defaut les stockages soft-deleted (RG-7.07), trie par
|
||||
* site.code ASC, storageType.label ASC, numero ASC (defaut spec § 4.1), applique
|
||||
* les filtres (?search sur numero, ?siteId[], ?storageTypeId, ?state) et renvoie
|
||||
* une collection PAGINEE Hydra (regle ABSOLUE n°13 : jamais d'array brut sur une
|
||||
* operation de collection — on enveloppe le QueryBuilder dans le Paginator ORM).
|
||||
* Echappatoire ?pagination=false respectee (alimentation d'un select).
|
||||
* - ITEM : recharge le stockage puis renvoie null (404) s'il est soft-deleted — le
|
||||
* soft-delete n'est jamais expose (§ 2.8), aucun flag includeDeleted.
|
||||
*
|
||||
* @implements ProviderInterface<Storage>
|
||||
*/
|
||||
final class StorageProvider implements ProviderInterface
|
||||
{
|
||||
/** Etats valides du filtre ?state= (enum borne, RG-7.04). */
|
||||
private const array VALID_STATES = [
|
||||
Storage::STATE_RECEPTION,
|
||||
Storage::STATE_PRODUCTION,
|
||||
Storage::STATE_TRIAGE,
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
#[Autowire(service: 'App\Module\Catalog\Infrastructure\Doctrine\DoctrineStorageRepository')]
|
||||
private readonly StorageRepositoryInterface $repository,
|
||||
private readonly Pagination $pagination,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable|Paginator|Storage|null
|
||||
{
|
||||
if ($operation instanceof CollectionOperationInterface) {
|
||||
// includeDeleted toujours false : le soft-delete n'est pas expose (§ 2.8).
|
||||
$qb = $this->repository->createListQueryBuilder(
|
||||
false,
|
||||
$this->readSearch($context),
|
||||
$this->readSiteIds($context),
|
||||
$this->readStorageTypeId($context),
|
||||
$this->readState($context),
|
||||
);
|
||||
|
||||
// Echappatoire ?pagination=false : collection complete sans Paginator.
|
||||
if (!$this->pagination->isEnabled($operation, $context)) {
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
// Branche paginee standard : offset/limit via Pagination, enveloppe dans le
|
||||
// Paginator ORM. Les jointures site/storageType sont to-ONE (ManyToOne) :
|
||||
// pas de duplication de lignes, le comptage reste exact.
|
||||
$limit = $this->pagination->getLimit($operation, $context);
|
||||
$page = max(1, $this->pagination->getPage($context));
|
||||
$offset = ($page - 1) * $limit;
|
||||
|
||||
$qb->setFirstResult($offset)->setMaxResults($limit);
|
||||
|
||||
return new Paginator(new DoctrinePaginator($qb->getQuery()));
|
||||
}
|
||||
|
||||
// Get unitaire : recharger l'entite, puis appliquer le filtre soft-delete.
|
||||
$id = $uriVariables['id'] ?? null;
|
||||
if (!is_int($id) && !(is_string($id) && ctype_digit($id))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$storage = $this->repository->findById((int) $id);
|
||||
if (null === $storage) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// § 2.8 : un stockage soft-deleted n'est jamais expose (404).
|
||||
if (null !== $storage->getDeletedAt()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit le filtre `?search=` (recherche partielle sur numero). Renvoie la valeur
|
||||
* trimmee ou null si absente / vide.
|
||||
*/
|
||||
private function readSearch(array $context): ?string
|
||||
{
|
||||
$raw = $context['filters']['search'] ?? null;
|
||||
|
||||
if (!is_string($raw)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$raw = trim($raw);
|
||||
|
||||
return '' === $raw ? null : $raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit le filtre `?siteId[]=` : ids des sites coches (OR). Tolere une valeur
|
||||
* scalaire unique (`?siteId=1`) ou un tableau. Ignore les entrees non numeriques.
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
private function readSiteIds(array $context): array
|
||||
{
|
||||
$raw = $context['filters']['siteId'] ?? null;
|
||||
|
||||
if (null === $raw) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$values = is_array($raw) ? $raw : [$raw];
|
||||
|
||||
$ids = [];
|
||||
foreach ($values as $value) {
|
||||
if (is_int($value) || (is_string($value) && ctype_digit($value))) {
|
||||
$ids[] = (int) $value;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit le filtre `?storageTypeId=` (drawer « Filtrer »). Renvoie l'id entier ou
|
||||
* null si absent / non numerique.
|
||||
*/
|
||||
private function readStorageTypeId(array $context): ?int
|
||||
{
|
||||
$raw = $context['filters']['storageTypeId'] ?? null;
|
||||
|
||||
if (is_int($raw)) {
|
||||
return $raw;
|
||||
}
|
||||
|
||||
return is_string($raw) && ctype_digit($raw) ? (int) $raw : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit le filtre `?state=` (RECEPTION / PRODUCTION / TRIAGE). Normalise en
|
||||
* majuscules et n'accepte qu'une valeur de l'enum borne ; toute autre valeur est
|
||||
* ignoree (null).
|
||||
*/
|
||||
private function readState(array $context): ?string
|
||||
{
|
||||
$raw = $context['filters']['state'] ?? null;
|
||||
|
||||
if (!is_string($raw) || '' === trim($raw)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$state = mb_strtoupper(trim($raw), 'UTF-8');
|
||||
|
||||
return in_array($state, self::VALID_STATES, true) ? $state : null;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace App\Module\Catalog\Infrastructure\Doctrine;
|
||||
use App\Module\Catalog\Domain\Entity\Storage;
|
||||
use App\Module\Catalog\Domain\Repository\StorageRepositoryInterface;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
@@ -29,4 +30,110 @@ class DoctrineStorageRepository extends ServiceEntityRepository implements Stora
|
||||
$this->getEntityManager()->persist($storage);
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
public function existsActiveBySiteTypeNumero(
|
||||
int $siteId,
|
||||
int $storageTypeId,
|
||||
string $numero,
|
||||
?int $excludeId = null,
|
||||
): bool {
|
||||
$qb = $this->createQueryBuilder('s')
|
||||
->select('1')
|
||||
->andWhere('s.site = :siteId')
|
||||
->andWhere('s.storageType = :storageTypeId')
|
||||
->andWhere('s.numero = :numero')
|
||||
->andWhere('s.deletedAt IS NULL')
|
||||
->setParameter('siteId', $siteId)
|
||||
->setParameter('storageTypeId', $storageTypeId)
|
||||
->setParameter('numero', $numero)
|
||||
->setMaxResults(1)
|
||||
;
|
||||
|
||||
if (null !== $excludeId) {
|
||||
$qb->andWhere('s.id != :excludeId')->setParameter('excludeId', $excludeId);
|
||||
}
|
||||
|
||||
return [] !== $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function createListQueryBuilder(
|
||||
bool $includeDeleted = false,
|
||||
?string $search = null,
|
||||
array $siteIds = [],
|
||||
?int $storageTypeId = null,
|
||||
?string $state = null,
|
||||
): QueryBuilder {
|
||||
// Eager-load des relations embarquees en liste (storage:read) pour eviter un
|
||||
// N+1 par stockage : site et storageType sont des ManyToOne (to-ONE, sures —
|
||||
// pas de duplication de lignes, contrairement aux ManyToMany du Product). Les
|
||||
// jointures servent aussi le tri (site.code, storageType.label).
|
||||
$qb = $this->createQueryBuilder('s')
|
||||
->leftJoin('s.site', 'site')->addSelect('site')
|
||||
->leftJoin('s.storageType', 'st')->addSelect('st')
|
||||
->orderBy('site.code', 'ASC')
|
||||
->addOrderBy('st.label', 'ASC')
|
||||
->addOrderBy('s.numero', 'ASC')
|
||||
;
|
||||
|
||||
// RG-7.07 : la liste exclut par defaut les stockages soft-deleted.
|
||||
if (!$includeDeleted) {
|
||||
$qb->andWhere('s.deletedAt IS NULL');
|
||||
}
|
||||
|
||||
// ?search= : recherche partielle case-insensitive sur numero. Les
|
||||
// metacaracteres LIKE (%, _, \) sont echappes pour rester litteraux.
|
||||
if (null !== $search && '' !== trim($search)) {
|
||||
$escaped = str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], trim($search));
|
||||
$pattern = '%'.mb_strtolower($escaped, 'UTF-8').'%';
|
||||
$qb->andWhere('LOWER(s.numero) LIKE :search')->setParameter('search', $pattern);
|
||||
}
|
||||
|
||||
// ?siteId[]= : stockage rattache a AU MOINS UN des sites passes (OR). site est
|
||||
// un ManyToOne (to-one) -> filtre direct sur la jointure, sans sous-requete
|
||||
// EXISTS ni risque de masquer une collection (≠ Product.sites M2M).
|
||||
if ([] !== $siteIds) {
|
||||
$qb->andWhere('site.id IN (:siteIds)')->setParameter('siteIds', $siteIds);
|
||||
}
|
||||
|
||||
// ?storageTypeId= : filtre par type de stockage precis (id).
|
||||
if (null !== $storageTypeId) {
|
||||
$qb->andWhere('st.id = :storageTypeId')->setParameter('storageTypeId', $storageTypeId);
|
||||
}
|
||||
|
||||
// ?state= : appartenance a la colonne JSONB `states`. DQL ne sait pas exprimer
|
||||
// la containment jsonb -> on resout les ids matchant en SQL natif (operateur
|
||||
// @>), puis on contraint le QueryBuilder. Ids vides -> condition toujours
|
||||
// fausse (aucun stockage), sans casser le reste de la requete.
|
||||
if (null !== $state) {
|
||||
$stateIds = $this->matchingStateIds($state);
|
||||
if ([] === $stateIds) {
|
||||
$qb->andWhere('1 = 0');
|
||||
} else {
|
||||
$qb->andWhere('s.id IN (:stateIds)')->setParameter('stateIds', $stateIds);
|
||||
}
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ids des stockages dont la colonne JSONB `states` contient l'etat donne, via
|
||||
* l'operateur de containment Postgres `@>`. L'etat est borne a l'enum
|
||||
* {RECEPTION, PRODUCTION, TRIAGE} en amont (StorageProvider) — pas de saisie
|
||||
* libre ici.
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
private function matchingStateIds(string $state): array
|
||||
{
|
||||
$rows = $this->getEntityManager()->getConnection()
|
||||
->executeQuery(
|
||||
'SELECT id FROM storage WHERE states @> CAST(:state AS JSONB)',
|
||||
['state' => (string) json_encode([$state])],
|
||||
)
|
||||
->fetchFirstColumn()
|
||||
;
|
||||
|
||||
return array_map(static fn (mixed $id): int => (int) $id, $rows);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user