feat: Gestion des matières scolaires

Les établissements ont besoin de définir leur référentiel de matières
pour pouvoir ensuite les associer aux enseignants et aux classes.
Cette fonctionnalité permet aux administrateurs de créer, modifier et
archiver les matières avec leurs propriétés (nom, code court, couleur).

L'architecture suit le pattern DDD avec des Value Objects utilisant
les property hooks PHP 8.5 pour garantir l'immutabilité et la validation.
L'isolation multi-tenant est assurée par vérification dans les handlers.
This commit is contained in:
2026-02-05 20:42:31 +01:00
parent 8e09e0abf1
commit 0d5a097c4c
50 changed files with 5882 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Command\ArchiveSubject;
/**
* Command pour archiver une matière.
*/
final readonly class ArchiveSubjectCommand
{
public function __construct(
public string $subjectId,
public string $tenantId,
) {
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Command\ArchiveSubject;
use App\Administration\Domain\Exception\SubjectNotFoundException;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
/**
* Handler pour archiver une matière.
*
* Note: La vérification des notes associées doit être faite par le Processor API
* avant d'appeler ce handler (via HasGradesForSubjectQuery).
*/
#[AsMessageHandler(bus: 'command.bus')]
final readonly class ArchiveSubjectHandler
{
public function __construct(
private SubjectRepository $subjectRepository,
private Clock $clock,
) {
}
public function __invoke(ArchiveSubjectCommand $command): Subject
{
$subjectId = SubjectId::fromString($command->subjectId);
$tenantId = TenantId::fromString($command->tenantId);
$subject = $this->subjectRepository->get($subjectId);
// Vérifier que la matière appartient au tenant (défense en profondeur)
if (!$subject->tenantId->equals($tenantId)) {
throw SubjectNotFoundException::withId($subjectId);
}
$subject->archiver($this->clock->now());
$this->subjectRepository->save($subject);
return $subject;
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Command\CreateSubject;
/**
* Command pour créer une nouvelle matière.
*/
final readonly class CreateSubjectCommand
{
public function __construct(
public string $tenantId,
public string $schoolId,
public string $name,
public string $code,
public ?string $color,
) {
}
}

View File

@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Command\CreateSubject;
use App\Administration\Domain\Exception\SubjectDejaExistanteException;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\Subject\SubjectCode;
use App\Administration\Domain\Model\Subject\SubjectColor;
use App\Administration\Domain\Model\Subject\SubjectName;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use function assert;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
/**
* Handler pour créer une nouvelle matière.
*/
#[AsMessageHandler(bus: 'command.bus')]
final readonly class CreateSubjectHandler
{
public function __construct(
private SubjectRepository $subjectRepository,
private Clock $clock,
) {
}
public function __invoke(CreateSubjectCommand $command): Subject
{
$tenantId = TenantId::fromString($command->tenantId);
$schoolId = SchoolId::fromString($command->schoolId);
// Value Objects validate input; assert non-empty for PHPStan
assert($command->code !== '');
assert($command->name !== '');
$code = new SubjectCode($command->code);
// Vérifier l'unicité du code dans le tenant et l'école
$existingSubject = $this->subjectRepository->findByCode($code, $tenantId, $schoolId);
if ($existingSubject !== null) {
throw SubjectDejaExistanteException::avecCode($code);
}
$color = null;
if ($command->color !== null) {
assert($command->color !== '');
$color = new SubjectColor($command->color);
}
$subject = Subject::creer(
tenantId: $tenantId,
schoolId: $schoolId,
name: new SubjectName($command->name),
code: $code,
color: $color,
createdAt: $this->clock->now(),
);
$this->subjectRepository->save($subject);
return $subject;
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Command\UpdateSubject;
/**
* Command pour modifier une matière existante.
*/
final readonly class UpdateSubjectCommand
{
public function __construct(
public string $subjectId,
public string $tenantId,
public string $schoolId,
public ?string $name = null,
public ?string $code = null,
public ?string $color = null,
public ?string $description = null,
public bool $clearColor = false,
public bool $clearDescription = false,
) {
}
}

View File

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Command\UpdateSubject;
use App\Administration\Domain\Exception\SubjectDejaExistanteException;
use App\Administration\Domain\Exception\SubjectNotFoundException;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\Subject\SubjectCode;
use App\Administration\Domain\Model\Subject\SubjectColor;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\Subject\SubjectName;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use function assert;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
/**
* Handler pour modifier une matière existante.
*/
#[AsMessageHandler(bus: 'command.bus')]
final readonly class UpdateSubjectHandler
{
public function __construct(
private SubjectRepository $subjectRepository,
private Clock $clock,
) {
}
public function __invoke(UpdateSubjectCommand $command): Subject
{
$subjectId = SubjectId::fromString($command->subjectId);
$tenantId = TenantId::fromString($command->tenantId);
$schoolId = SchoolId::fromString($command->schoolId);
$now = $this->clock->now();
$subject = $this->subjectRepository->get($subjectId);
// Vérifier que la matière appartient au tenant (défense en profondeur)
if (!$subject->tenantId->equals($tenantId)) {
throw SubjectNotFoundException::withId($subjectId);
}
// Mise à jour du nom
if ($command->name !== null) {
assert($command->name !== '');
$subject->renommer(new SubjectName($command->name), $now);
}
// Mise à jour du code
if ($command->code !== null) {
assert($command->code !== '');
$newCode = new SubjectCode($command->code);
// Vérifier l'unicité du code (sauf si c'est le même)
if (!$subject->code->equals($newCode)) {
$existingSubject = $this->subjectRepository->findByCode($newCode, $tenantId, $schoolId);
if ($existingSubject !== null && !$existingSubject->id->equals($subjectId)) {
throw SubjectDejaExistanteException::avecCode($newCode);
}
$subject->changerCode($newCode, $now);
}
}
// Mise à jour de la couleur
if ($command->clearColor) {
$subject->changerCouleur(null, $now);
} elseif ($command->color !== null) {
assert($command->color !== '');
$subject->changerCouleur(new SubjectColor($command->color), $now);
}
// Mise à jour de la description
if ($command->clearDescription) {
$subject->decrire(null, $now);
} elseif ($command->description !== null) {
$subject->decrire($command->description, $now);
}
$this->subjectRepository->save($subject);
return $subject;
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Query\GetSubjects;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Shared\Domain\Tenant\TenantId;
use function array_map;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
/**
* Handler pour récupérer les matières actives d'un tenant.
*/
#[AsMessageHandler(bus: 'query.bus')]
final readonly class GetSubjectsHandler
{
public function __construct(
private SubjectRepository $subjectRepository,
) {
}
/**
* @return SubjectDto[]
*/
public function __invoke(GetSubjectsQuery $query): array
{
$subjects = $this->subjectRepository->findActiveByTenantAndSchool(
TenantId::fromString($query->tenantId),
SchoolId::fromString($query->schoolId),
);
// TODO: Récupérer les comptages d'enseignants et de classes
// quand les modules Affectations seront implémentés (T7)
return array_map(
static fn ($subject) => SubjectDto::fromDomain(
$subject,
teacherCount: 0, // Placeholder - T7
classCount: 0, // Placeholder - T7
),
$subjects,
);
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Query\GetSubjects;
/**
* Query pour récupérer les matières actives d'un tenant et d'une école.
*/
final readonly class GetSubjectsQuery
{
public function __construct(
public string $tenantId,
public string $schoolId,
) {
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App\Administration\Application\Query\GetSubjects;
use App\Administration\Domain\Model\Subject\Subject;
use DateTimeImmutable;
/**
* DTO pour représenter une matière dans les réponses de query.
*/
final readonly class SubjectDto
{
public function __construct(
public string $id,
public string $name,
public string $code,
public ?string $color,
public ?string $description,
public string $status,
public DateTimeImmutable $createdAt,
public DateTimeImmutable $updatedAt,
public int $teacherCount = 0,
public int $classCount = 0,
) {
}
public static function fromDomain(
Subject $subject,
int $teacherCount = 0,
int $classCount = 0,
): self {
return new self(
id: (string) $subject->id,
name: (string) $subject->name,
code: (string) $subject->code,
color: $subject->color !== null ? (string) $subject->color : null,
description: $subject->description,
status: $subject->status->value,
createdAt: $subject->createdAt,
updatedAt: $subject->updatedAt,
teacherCount: $teacherCount,
classCount: $classCount,
);
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Event;
use App\Administration\Domain\Model\Subject\SubjectCode;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\Subject\SubjectName;
use App\Shared\Domain\DomainEvent;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use Override;
use Ramsey\Uuid\UuidInterface;
/**
* Événement émis lors de la création d'une matière.
*/
final readonly class MatiereCreee implements DomainEvent
{
public function __construct(
public SubjectId $subjectId,
public TenantId $tenantId,
public SubjectName $name,
public SubjectCode $code,
private DateTimeImmutable $occurredOn,
) {
}
#[Override]
public function occurredOn(): DateTimeImmutable
{
return $this->occurredOn;
}
#[Override]
public function aggregateId(): UuidInterface
{
return $this->subjectId->value;
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Event;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\Subject\SubjectName;
use App\Shared\Domain\DomainEvent;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use Override;
use Ramsey\Uuid\UuidInterface;
/**
* Événement émis lors de la modification d'une matière.
*/
final readonly class MatiereModifiee implements DomainEvent
{
public function __construct(
public SubjectId $subjectId,
public TenantId $tenantId,
public SubjectName $ancienNom,
public SubjectName $nouveauNom,
private DateTimeImmutable $occurredOn,
) {
}
#[Override]
public function occurredOn(): DateTimeImmutable
{
return $this->occurredOn;
}
#[Override]
public function aggregateId(): UuidInterface
{
return $this->subjectId->value;
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Event;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Shared\Domain\DomainEvent;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use Override;
use Ramsey\Uuid\UuidInterface;
/**
* Événement émis lors de la suppression/archivage d'une matière.
*/
final readonly class MatiereSupprimee implements DomainEvent
{
public function __construct(
public SubjectId $subjectId,
public TenantId $tenantId,
private DateTimeImmutable $occurredOn,
) {
}
#[Override]
public function occurredOn(): DateTimeImmutable
{
return $this->occurredOn;
}
#[Override]
public function aggregateId(): UuidInterface
{
return $this->subjectId->value;
}
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Exception;
use RuntimeException;
use function sprintf;
final class SubjectCodeInvalideException extends RuntimeException
{
public static function pourFormat(string $value, int $min, int $max): self
{
return new self(sprintf(
'Le code de matière "%s" doit contenir entre %d et %d caractères alphanumériques majuscules (ex: "MATH", "FR", "EPS").',
$value,
$min,
$max,
));
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Exception;
use RuntimeException;
use function sprintf;
final class SubjectColorInvalideException extends RuntimeException
{
public static function pourFormat(string $value): self
{
return new self(sprintf(
'La couleur "%s" doit être au format hexadécimal #RRGGBB (ex: "#3B82F6").',
$value,
));
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Exception;
use App\Administration\Domain\Model\Subject\SubjectCode;
use RuntimeException;
use function sprintf;
final class SubjectDejaExistanteException extends RuntimeException
{
public static function avecCode(SubjectCode $code): self
{
return new self(sprintf(
'Une matière avec le code "%s" existe déjà dans cet établissement.',
(string) $code,
));
}
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Exception;
use RuntimeException;
use function sprintf;
final class SubjectNameInvalideException extends RuntimeException
{
public static function pourLongueur(string $value, int $min, int $max): self
{
return new self(sprintf(
'Le nom de matière "%s" doit contenir entre %d et %d caractères.',
$value,
$min,
$max,
));
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Exception;
use App\Administration\Domain\Model\Subject\SubjectId;
use RuntimeException;
use function sprintf;
final class SubjectNonSupprimableException extends RuntimeException
{
public static function avecNotes(SubjectId $id): self
{
return new self(sprintf(
'La matière "%s" ne peut pas être supprimée car des notes y sont associées.',
(string) $id,
));
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Exception;
use App\Administration\Domain\Model\Subject\SubjectId;
use RuntimeException;
use function sprintf;
final class SubjectNotFoundException extends RuntimeException
{
public static function withId(SubjectId $id): self
{
return new self(sprintf('Matière "%s" non trouvée.', (string) $id));
}
}

View File

@@ -0,0 +1,217 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Model\Subject;
use App\Administration\Domain\Event\MatiereCreee;
use App\Administration\Domain\Event\MatiereModifiee;
use App\Administration\Domain\Event\MatiereSupprimee;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Shared\Domain\AggregateRoot;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
/**
* Aggregate Root représentant une matière enseignée.
*
* Une matière appartient à un établissement (tenant) et une école.
* Elle a un nom, un code court unique et une couleur optionnelle pour l'affichage.
*
* @see FR74: Structurer l'offre pédagogique
*/
final class Subject extends AggregateRoot
{
public private(set) ?string $description = null;
public private(set) DateTimeImmutable $updatedAt;
public private(set) ?DateTimeImmutable $deletedAt = null;
private function __construct(
public private(set) SubjectId $id,
public private(set) TenantId $tenantId,
public private(set) SchoolId $schoolId,
public private(set) SubjectName $name,
public private(set) SubjectCode $code,
public private(set) ?SubjectColor $color,
public private(set) SubjectStatus $status,
public private(set) DateTimeImmutable $createdAt,
) {
$this->updatedAt = $createdAt;
}
/**
* Crée une nouvelle matière.
*/
public static function creer(
TenantId $tenantId,
SchoolId $schoolId,
SubjectName $name,
SubjectCode $code,
?SubjectColor $color,
DateTimeImmutable $createdAt,
): self {
$subject = new self(
id: SubjectId::generate(),
tenantId: $tenantId,
schoolId: $schoolId,
name: $name,
code: $code,
color: $color,
status: SubjectStatus::ACTIVE,
createdAt: $createdAt,
);
$subject->recordEvent(new MatiereCreee(
subjectId: $subject->id,
tenantId: $subject->tenantId,
name: $subject->name,
code: $subject->code,
occurredOn: $createdAt,
));
return $subject;
}
/**
* Renomme la matière.
*/
public function renommer(SubjectName $nouveauNom, DateTimeImmutable $at): void
{
if ($this->name->equals($nouveauNom)) {
return;
}
$ancienNom = $this->name;
$this->name = $nouveauNom;
$this->updatedAt = $at;
$this->recordEvent(new MatiereModifiee(
subjectId: $this->id,
tenantId: $this->tenantId,
ancienNom: $ancienNom,
nouveauNom: $nouveauNom,
occurredOn: $at,
));
}
/**
* Change le code de la matière.
*
* Note: L'unicité du code doit être vérifiée par l'Application Layer
* avant d'appeler cette méthode.
*/
public function changerCode(SubjectCode $nouveauCode, DateTimeImmutable $at): void
{
if ($this->code->equals($nouveauCode)) {
return;
}
$this->code = $nouveauCode;
$this->updatedAt = $at;
}
/**
* Change la couleur de la matière.
*/
public function changerCouleur(?SubjectColor $couleur, DateTimeImmutable $at): void
{
// Compare colors, considering null cases
$sameColor = ($this->color === null && $couleur === null)
|| ($this->color !== null && $couleur !== null && $this->color->equals($couleur));
if ($sameColor) {
return;
}
$this->color = $couleur;
$this->updatedAt = $at;
}
/**
* Ajoute ou modifie la description de la matière.
*/
public function decrire(?string $description, DateTimeImmutable $at): void
{
if ($this->description === $description) {
return;
}
$this->description = $description;
$this->updatedAt = $at;
}
/**
* Archive la matière (soft delete).
*
* Note: La vérification des notes associées doit être faite par l'Application Layer
* via une Query avant d'appeler cette méthode.
*/
public function archiver(DateTimeImmutable $at): void
{
if ($this->status === SubjectStatus::ARCHIVED) {
return;
}
$this->status = SubjectStatus::ARCHIVED;
$this->deletedAt = $at;
$this->updatedAt = $at;
$this->recordEvent(new MatiereSupprimee(
subjectId: $this->id,
tenantId: $this->tenantId,
occurredOn: $at,
));
}
/**
* Vérifie si la matière est active.
*/
public function estActive(): bool
{
return $this->status === SubjectStatus::ACTIVE;
}
/**
* Vérifie si la matière peut être utilisée.
*/
public function peutEtreUtilisee(): bool
{
return $this->status->peutEtreUtilisee();
}
/**
* Reconstitue un Subject depuis le stockage.
*
* @internal Pour usage Infrastructure uniquement
*/
public static function reconstitute(
SubjectId $id,
TenantId $tenantId,
SchoolId $schoolId,
SubjectName $name,
SubjectCode $code,
?SubjectColor $color,
SubjectStatus $status,
?string $description,
DateTimeImmutable $createdAt,
DateTimeImmutable $updatedAt,
?DateTimeImmutable $deletedAt,
): self {
$subject = new self(
id: $id,
tenantId: $tenantId,
schoolId: $schoolId,
name: $name,
code: $code,
color: $color,
status: $status,
createdAt: $createdAt,
);
$subject->description = $description;
$subject->updatedAt = $updatedAt;
$subject->deletedAt = $deletedAt;
return $subject;
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Model\Subject;
use App\Administration\Domain\Exception\SubjectCodeInvalideException;
use function assert;
use function preg_match;
use function strtoupper;
use function trim;
/**
* Value Object représentant le code court d'une matière.
*
* Contraintes :
* - Entre 2 et 10 caractères
* - Lettres majuscules et chiffres uniquement
* - Exemple: "MATH", "FR", "HG", "EPS"
*/
final class SubjectCode
{
private const int MIN_LENGTH = 2;
private const int MAX_LENGTH = 10;
private const string PATTERN = '/^[A-Z0-9]{2,10}$/';
public function __construct(
/** @var non-empty-string */
public private(set) string $value {
set(string $value) {
$normalized = strtoupper(trim($value));
if (preg_match(self::PATTERN, $normalized) !== 1) {
throw SubjectCodeInvalideException::pourFormat($value, self::MIN_LENGTH, self::MAX_LENGTH);
}
// After validation, $normalized is guaranteed to be non-empty (MIN_LENGTH >= 2)
assert($normalized !== '');
$this->value = $normalized;
}
},
) {
}
public function equals(self $other): bool
{
return $this->value === $other->value;
}
public function __toString(): string
{
return $this->value;
}
}

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Model\Subject;
use App\Administration\Domain\Exception\SubjectColorInvalideException;
use function assert;
use function preg_match;
use function strtoupper;
use function trim;
/**
* Value Object représentant la couleur d'une matière (format hexadécimal).
*
* Contraintes :
* - Format #RRGGBB
* - Exemple: "#3B82F6" (bleu), "#EF4444" (rouge)
*/
final class SubjectColor
{
private const string PATTERN = '/^#[0-9A-F]{6}$/';
public function __construct(
/** @var non-empty-string */
public private(set) string $value {
set(string $value) {
$normalized = strtoupper(trim($value));
if (preg_match(self::PATTERN, $normalized) !== 1) {
throw SubjectColorInvalideException::pourFormat($value);
}
// After validation, $normalized is guaranteed to be non-empty (#RRGGBB = 7 chars)
assert($normalized !== '');
$this->value = $normalized;
}
},
) {
}
public function equals(self $other): bool
{
return $this->value === $other->value;
}
public function __toString(): string
{
return $this->value;
}
}

View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Model\Subject;
use App\Shared\Domain\EntityId;
final readonly class SubjectId extends EntityId
{
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Model\Subject;
use App\Administration\Domain\Exception\SubjectNameInvalideException;
use function assert;
use function mb_strlen;
use function trim;
/**
* Value Object représentant le nom d'une matière.
*
* Contraintes :
* - Entre 2 et 100 caractères
* - Non vide après trim
*/
final class SubjectName
{
private const int MIN_LENGTH = 2;
private const int MAX_LENGTH = 100;
public function __construct(
/** @var non-empty-string */
public private(set) string $value {
set(string $value) {
$trimmed = trim($value);
$length = mb_strlen($trimmed);
if ($length < self::MIN_LENGTH || $length > self::MAX_LENGTH) {
throw SubjectNameInvalideException::pourLongueur($value, self::MIN_LENGTH, self::MAX_LENGTH);
}
// After validation, $trimmed is guaranteed to be non-empty (MIN_LENGTH >= 2)
assert($trimmed !== '');
$this->value = $trimmed;
}
},
) {
}
public function equals(self $other): bool
{
return $this->value === $other->value;
}
/**
* @return non-empty-string
*/
public function __toString(): string
{
return $this->value;
}
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Model\Subject;
/**
* Statut d'une matière.
*/
enum SubjectStatus: string
{
case ACTIVE = 'active';
case ARCHIVED = 'archived';
/**
* Vérifie si la matière peut être utilisée.
*/
public function peutEtreUtilisee(): bool
{
return $this === self::ACTIVE;
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App\Administration\Domain\Repository;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\Subject\SubjectCode;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Shared\Domain\Tenant\TenantId;
interface SubjectRepository
{
public function save(Subject $subject): void;
/**
* @throws \App\Administration\Domain\Exception\SubjectNotFoundException
*/
public function get(SubjectId $id): Subject;
public function findById(SubjectId $id): ?Subject;
/**
* Recherche une matière par code dans un tenant et une école.
*/
public function findByCode(
SubjectCode $code,
TenantId $tenantId,
SchoolId $schoolId,
): ?Subject;
/**
* Retourne toutes les matières actives d'un tenant et une école.
*
* @return Subject[]
*/
public function findActiveByTenantAndSchool(
TenantId $tenantId,
SchoolId $schoolId,
): array;
/**
* Supprime une matière du repository.
*/
public function delete(SubjectId $id): void;
}

View File

@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Api\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Administration\Application\Command\CreateSubject\CreateSubjectCommand;
use App\Administration\Application\Command\CreateSubject\CreateSubjectHandler;
use App\Administration\Domain\Exception\SubjectCodeInvalideException;
use App\Administration\Domain\Exception\SubjectColorInvalideException;
use App\Administration\Domain\Exception\SubjectDejaExistanteException;
use App\Administration\Domain\Exception\SubjectNameInvalideException;
use App\Administration\Infrastructure\Api\Resource\SubjectResource;
use App\Administration\Infrastructure\School\SchoolIdResolver;
use App\Administration\Infrastructure\Security\SubjectVoter;
use App\Shared\Infrastructure\Tenant\TenantContext;
use Override;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* Processor API Platform pour créer une matière.
*
* @implements ProcessorInterface<SubjectResource, SubjectResource>
*/
final readonly class CreateSubjectProcessor implements ProcessorInterface
{
public function __construct(
private CreateSubjectHandler $handler,
private TenantContext $tenantContext,
private MessageBusInterface $eventBus,
private AuthorizationCheckerInterface $authorizationChecker,
private SchoolIdResolver $schoolIdResolver,
) {
}
/**
* @param SubjectResource $data
*/
#[Override]
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): SubjectResource
{
if (!$this->authorizationChecker->isGranted(SubjectVoter::CREATE)) {
throw new AccessDeniedHttpException('Vous n\'êtes pas autorisé à créer une matière.');
}
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
$tenantId = (string) $this->tenantContext->getCurrentTenantId();
$schoolId = $this->schoolIdResolver->resolveForTenant($tenantId);
try {
$command = new CreateSubjectCommand(
tenantId: $tenantId,
schoolId: $schoolId,
name: $data->name ?? '',
code: $data->code ?? '',
color: $data->color,
);
$subject = ($this->handler)($command);
// Dispatch domain events from the created aggregate
foreach ($subject->pullDomainEvents() as $event) {
$this->eventBus->dispatch($event);
}
// Return the created resource
$resource = new SubjectResource();
$resource->id = (string) $subject->id;
$resource->name = (string) $subject->name;
$resource->code = (string) $subject->code;
$resource->color = $subject->color !== null ? (string) $subject->color : null;
$resource->description = $subject->description;
$resource->status = $subject->status->value;
$resource->createdAt = $subject->createdAt;
$resource->updatedAt = $subject->updatedAt;
return $resource;
} catch (SubjectNameInvalideException|SubjectCodeInvalideException|SubjectColorInvalideException $e) {
throw new BadRequestHttpException($e->getMessage());
} catch (SubjectDejaExistanteException $e) {
throw new ConflictHttpException($e->getMessage());
}
}
}

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Api\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Administration\Application\Command\ArchiveSubject\ArchiveSubjectCommand;
use App\Administration\Application\Command\ArchiveSubject\ArchiveSubjectHandler;
use App\Administration\Domain\Exception\SubjectNotFoundException;
use App\Administration\Infrastructure\Api\Resource\SubjectResource;
use App\Administration\Infrastructure\Security\SubjectVoter;
use App\Shared\Infrastructure\Tenant\TenantContext;
use Override;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* Processor API Platform pour supprimer (archiver) une matière.
*
* Note: Cette implémentation fait un soft delete (archivage).
* La vérification des notes associées (T6) sera ajoutée ultérieurement
* quand le module Notes sera implémenté.
*
* @implements ProcessorInterface<SubjectResource, null>
*/
final readonly class DeleteSubjectProcessor implements ProcessorInterface
{
public function __construct(
private ArchiveSubjectHandler $handler,
private TenantContext $tenantContext,
private MessageBusInterface $eventBus,
private AuthorizationCheckerInterface $authorizationChecker,
) {
}
/**
* @param SubjectResource $data
*/
#[Override]
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): null
{
if (!$this->authorizationChecker->isGranted(SubjectVoter::DELETE)) {
throw new AccessDeniedHttpException('Vous n\'êtes pas autorisé à supprimer cette matière.');
}
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
/** @var string|null $subjectId */
$subjectId = $uriVariables['id'] ?? null;
if ($subjectId === null) {
throw new NotFoundHttpException('Matière non trouvée.');
}
$tenantId = (string) $this->tenantContext->getCurrentTenantId();
try {
// TODO: Vérifier si des notes sont associées (T6)
// et retourner un warning si c'est le cas (via query param ?confirm=true)
$command = new ArchiveSubjectCommand(
subjectId: $subjectId,
tenantId: $tenantId,
);
$subject = ($this->handler)($command);
// Dispatch domain events from the archived aggregate
foreach ($subject->pullDomainEvents() as $event) {
$this->eventBus->dispatch($event);
}
return null;
} catch (SubjectNotFoundException|InvalidUuidStringException) {
throw new NotFoundHttpException('Matière non trouvée.');
}
}
}

View File

@@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Api\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Administration\Application\Command\UpdateSubject\UpdateSubjectCommand;
use App\Administration\Application\Command\UpdateSubject\UpdateSubjectHandler;
use App\Administration\Domain\Exception\SubjectCodeInvalideException;
use App\Administration\Domain\Exception\SubjectColorInvalideException;
use App\Administration\Domain\Exception\SubjectDejaExistanteException;
use App\Administration\Domain\Exception\SubjectNameInvalideException;
use App\Administration\Domain\Exception\SubjectNotFoundException;
use App\Administration\Infrastructure\Api\Resource\SubjectResource;
use App\Administration\Infrastructure\School\SchoolIdResolver;
use App\Administration\Infrastructure\Security\SubjectVoter;
use App\Shared\Infrastructure\Tenant\TenantContext;
use Override;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* Processor API Platform pour modifier une matière.
*
* @implements ProcessorInterface<SubjectResource, SubjectResource>
*/
final readonly class UpdateSubjectProcessor implements ProcessorInterface
{
public function __construct(
private UpdateSubjectHandler $handler,
private TenantContext $tenantContext,
private MessageBusInterface $eventBus,
private AuthorizationCheckerInterface $authorizationChecker,
private SchoolIdResolver $schoolIdResolver,
) {
}
/**
* @param SubjectResource $data
*/
#[Override]
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): SubjectResource
{
if (!$this->authorizationChecker->isGranted(SubjectVoter::EDIT)) {
throw new AccessDeniedHttpException('Vous n\'êtes pas autorisé à modifier cette matière.');
}
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
/** @var string|null $subjectId */
$subjectId = $uriVariables['id'] ?? null;
if ($subjectId === null) {
throw new NotFoundHttpException('Matière non trouvée.');
}
$tenantId = (string) $this->tenantContext->getCurrentTenantId();
$schoolId = $this->schoolIdResolver->resolveForTenant($tenantId);
try {
$command = new UpdateSubjectCommand(
subjectId: $subjectId,
tenantId: $tenantId,
schoolId: $schoolId,
name: $data->name,
code: $data->code,
color: $data->color,
description: $data->description,
clearColor: $data->clearColor ?? false,
clearDescription: $data->clearDescription ?? false,
);
$subject = ($this->handler)($command);
// Dispatch domain events from the updated aggregate
foreach ($subject->pullDomainEvents() as $event) {
$this->eventBus->dispatch($event);
}
// Return the updated resource
$resource = new SubjectResource();
$resource->id = (string) $subject->id;
$resource->name = (string) $subject->name;
$resource->code = (string) $subject->code;
$resource->color = $subject->color !== null ? (string) $subject->color : null;
$resource->description = $subject->description;
$resource->status = $subject->status->value;
$resource->createdAt = $subject->createdAt;
$resource->updatedAt = $subject->updatedAt;
return $resource;
} catch (SubjectNotFoundException|InvalidUuidStringException) {
throw new NotFoundHttpException('Matière non trouvée.');
} catch (SubjectNameInvalideException|SubjectCodeInvalideException|SubjectColorInvalideException $e) {
throw new BadRequestHttpException($e->getMessage());
} catch (SubjectDejaExistanteException $e) {
throw new ConflictHttpException($e->getMessage());
}
}
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Api\Provider;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Administration\Application\Query\GetSubjects\GetSubjectsHandler;
use App\Administration\Application\Query\GetSubjects\GetSubjectsQuery;
use App\Administration\Infrastructure\Api\Resource\SubjectResource;
use App\Administration\Infrastructure\School\SchoolIdResolver;
use App\Administration\Infrastructure\Security\SubjectVoter;
use App\Shared\Infrastructure\Tenant\TenantContext;
use function array_map;
use Override;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* State Provider pour récupérer la liste des matières.
*
* @implements ProviderInterface<SubjectResource>
*/
final readonly class SubjectCollectionProvider implements ProviderInterface
{
public function __construct(
private GetSubjectsHandler $handler,
private TenantContext $tenantContext,
private AuthorizationCheckerInterface $authorizationChecker,
private SchoolIdResolver $schoolIdResolver,
) {
}
/**
* @return SubjectResource[]
*/
#[Override]
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
{
// Vérifier les permissions de lecture (sans sujet spécifique)
if (!$this->authorizationChecker->isGranted(SubjectVoter::VIEW)) {
throw new AccessDeniedHttpException('Vous n\'êtes pas autorisé à voir les matières.');
}
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
$tenantId = (string) $this->tenantContext->getCurrentTenantId();
$schoolId = $this->schoolIdResolver->resolveForTenant($tenantId);
$query = new GetSubjectsQuery(
tenantId: $tenantId,
schoolId: $schoolId,
);
$subjectDtos = ($this->handler)($query);
return array_map(
static function ($dto) {
$resource = new SubjectResource();
$resource->id = $dto->id;
$resource->name = $dto->name;
$resource->code = $dto->code;
$resource->color = $dto->color;
$resource->description = $dto->description;
$resource->status = $dto->status;
$resource->createdAt = $dto->createdAt;
$resource->updatedAt = $dto->updatedAt;
$resource->teacherCount = $dto->teacherCount;
$resource->classCount = $dto->classCount;
return $resource;
},
$subjectDtos,
);
}
}

View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Api\Provider;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Administration\Domain\Exception\SubjectNotFoundException;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Administration\Infrastructure\Api\Resource\SubjectResource;
use App\Administration\Infrastructure\Security\SubjectVoter;
use App\Shared\Infrastructure\Tenant\TenantContext;
use Override;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* State Provider pour récupérer une matière par son ID.
*
* @implements ProviderInterface<SubjectResource>
*/
final readonly class SubjectItemProvider implements ProviderInterface
{
public function __construct(
private SubjectRepository $subjectRepository,
private TenantContext $tenantContext,
private AuthorizationCheckerInterface $authorizationChecker,
) {
}
#[Override]
public function provide(Operation $operation, array $uriVariables = [], array $context = []): SubjectResource
{
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
/** @var string|null $subjectId */
$subjectId = $uriVariables['id'] ?? null;
if ($subjectId === null) {
throw new NotFoundHttpException('Matière non trouvée.');
}
try {
$subject = $this->subjectRepository->get(SubjectId::fromString($subjectId));
} catch (SubjectNotFoundException|InvalidUuidStringException) {
throw new NotFoundHttpException('Matière non trouvée.');
}
// Vérifier que la matière appartient au tenant actuel
if ((string) $subject->tenantId !== (string) $this->tenantContext->getCurrentTenantId()) {
throw new NotFoundHttpException('Matière non trouvée.');
}
// Vérifier les permissions de lecture
if (!$this->authorizationChecker->isGranted(SubjectVoter::VIEW, $subject)) {
throw new AccessDeniedHttpException('Vous n\'êtes pas autorisé à voir cette matière.');
}
$resource = new SubjectResource();
$resource->id = (string) $subject->id;
$resource->name = (string) $subject->name;
$resource->code = (string) $subject->code;
$resource->color = $subject->color !== null ? (string) $subject->color : null;
$resource->description = $subject->description;
$resource->status = $subject->status->value;
$resource->createdAt = $subject->createdAt;
$resource->updatedAt = $subject->updatedAt;
return $resource;
}
}

View File

@@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Api\Resource;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Administration\Infrastructure\Api\Processor\CreateSubjectProcessor;
use App\Administration\Infrastructure\Api\Processor\DeleteSubjectProcessor;
use App\Administration\Infrastructure\Api\Processor\UpdateSubjectProcessor;
use App\Administration\Infrastructure\Api\Provider\SubjectCollectionProvider;
use App\Administration\Infrastructure\Api\Provider\SubjectItemProvider;
use DateTimeImmutable;
use Symfony\Component\Validator\Constraints as Assert;
/**
* API Resource pour la gestion des matières.
*
* @see Story 2.2 - Création et Gestion des Matières
* @see FR74 - Structurer l'offre pédagogique
*/
#[ApiResource(
shortName: 'Subject',
operations: [
new GetCollection(
uriTemplate: '/subjects',
provider: SubjectCollectionProvider::class,
name: 'get_subjects',
),
new Get(
uriTemplate: '/subjects/{id}',
provider: SubjectItemProvider::class,
name: 'get_subject',
),
new Post(
uriTemplate: '/subjects',
processor: CreateSubjectProcessor::class,
validationContext: ['groups' => ['Default', 'create']],
name: 'create_subject',
),
new Patch(
uriTemplate: '/subjects/{id}',
provider: SubjectItemProvider::class,
processor: UpdateSubjectProcessor::class,
validationContext: ['groups' => ['Default', 'update']],
name: 'update_subject',
),
new Delete(
uriTemplate: '/subjects/{id}',
provider: SubjectItemProvider::class,
processor: DeleteSubjectProcessor::class,
name: 'delete_subject',
),
],
)]
final class SubjectResource
{
#[ApiProperty(identifier: true)]
public ?string $id = null;
#[Assert\NotBlank(message: 'Le nom de la matière est requis.', groups: ['create'])]
#[Assert\Length(
min: 2,
max: 100,
minMessage: 'Le nom de la matière doit contenir au moins {{ limit }} caractères.',
maxMessage: 'Le nom de la matière ne peut pas dépasser {{ limit }} caractères.',
)]
public ?string $name = null;
#[Assert\NotBlank(message: 'Le code de la matière est requis.', groups: ['create'])]
#[Assert\Regex(
pattern: '/^[A-Za-z0-9]{2,10}$/',
message: 'Le code doit contenir entre 2 et 10 caractères alphanumériques.',
)]
public ?string $code = null;
#[Assert\Regex(
pattern: '/^#[0-9A-Fa-f]{6}$/',
message: 'La couleur doit être au format hexadécimal #RRGGBB.',
)]
public ?string $color = null;
public ?string $description = null;
public ?string $status = null;
public ?DateTimeImmutable $createdAt = null;
public ?DateTimeImmutable $updatedAt = null;
/**
* Statistiques : nombre d'enseignants associés à cette matière.
* Disponible uniquement dans GetCollection.
*/
#[ApiProperty(readable: true, writable: false)]
public ?int $teacherCount = null;
/**
* Statistiques : nombre de classes associées à cette matière.
* Disponible uniquement dans GetCollection.
*/
#[ApiProperty(readable: true, writable: false)]
public ?int $classCount = null;
/**
* Permet de supprimer explicitement la couleur lors d'un PATCH.
* Si true, la couleur sera mise à null même si color n'est pas fourni.
*/
#[ApiProperty(readable: false)]
public ?bool $clearColor = null;
/**
* Permet de supprimer explicitement la description lors d'un PATCH.
* Si true, la description sera mise à null même si description n'est pas fourni.
*/
#[ApiProperty(readable: false)]
public ?bool $clearDescription = null;
/**
* Indique si la matière a des notes associées (pour avertissement avant suppression).
*/
#[ApiProperty(readable: true, writable: false)]
public ?bool $hasGrades = null;
}

View File

@@ -0,0 +1,176 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Persistence\Doctrine;
use App\Administration\Domain\Exception\SubjectNotFoundException;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\Subject\SubjectCode;
use App\Administration\Domain\Model\Subject\SubjectColor;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\Subject\SubjectName;
use App\Administration\Domain\Model\Subject\SubjectStatus;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Override;
final readonly class DoctrineSubjectRepository implements SubjectRepository
{
public function __construct(
private Connection $connection,
) {
}
#[Override]
public function save(Subject $subject): void
{
$data = [
'id' => (string) $subject->id,
'tenant_id' => (string) $subject->tenantId,
'school_id' => (string) $subject->schoolId,
'name' => (string) $subject->name,
'code' => (string) $subject->code,
'color' => $subject->color !== null ? (string) $subject->color : null,
'status' => $subject->status->value,
'description' => $subject->description,
'created_at' => $subject->createdAt->format(DateTimeImmutable::ATOM),
'updated_at' => $subject->updatedAt->format(DateTimeImmutable::ATOM),
'deleted_at' => $subject->deletedAt?->format(DateTimeImmutable::ATOM),
];
$exists = $this->findById($subject->id) !== null;
if ($exists) {
$this->connection->update('subjects', $data, ['id' => (string) $subject->id]);
} else {
$this->connection->insert('subjects', $data);
}
}
#[Override]
public function get(SubjectId $id): Subject
{
$subject = $this->findById($id);
if ($subject === null) {
throw SubjectNotFoundException::withId($id);
}
return $subject;
}
#[Override]
public function findById(SubjectId $id): ?Subject
{
$row = $this->connection->fetchAssociative(
'SELECT * FROM subjects WHERE id = :id',
['id' => (string) $id],
);
if ($row === false) {
return null;
}
return $this->hydrate($row);
}
#[Override]
public function findByCode(
SubjectCode $code,
TenantId $tenantId,
SchoolId $schoolId,
): ?Subject {
$row = $this->connection->fetchAssociative(
'SELECT * FROM subjects
WHERE tenant_id = :tenant_id
AND school_id = :school_id
AND code = :code
AND deleted_at IS NULL',
[
'tenant_id' => (string) $tenantId,
'school_id' => (string) $schoolId,
'code' => (string) $code,
],
);
if ($row === false) {
return null;
}
return $this->hydrate($row);
}
#[Override]
public function findActiveByTenantAndSchool(
TenantId $tenantId,
SchoolId $schoolId,
): array {
$rows = $this->connection->fetchAllAssociative(
'SELECT * FROM subjects
WHERE tenant_id = :tenant_id
AND school_id = :school_id
AND status = :status
ORDER BY name ASC',
[
'tenant_id' => (string) $tenantId,
'school_id' => (string) $schoolId,
'status' => SubjectStatus::ACTIVE->value,
],
);
return array_map(fn ($row) => $this->hydrate($row), $rows);
}
#[Override]
public function delete(SubjectId $id): void
{
$this->connection->delete('subjects', ['id' => (string) $id]);
}
/**
* @param array<string, mixed> $row
*/
private function hydrate(array $row): Subject
{
/** @var string $id */
$id = $row['id'];
/** @var string $tenantId */
$tenantId = $row['tenant_id'];
/** @var string $schoolId */
$schoolId = $row['school_id'];
/** @var non-empty-string $name */
$name = $row['name'];
/** @var non-empty-string $code */
$code = $row['code'];
/** @var non-empty-string|null $color */
$color = $row['color'];
/** @var string $status */
$status = $row['status'];
/** @var string|null $description */
$description = $row['description'];
/** @var string $createdAt */
$createdAt = $row['created_at'];
/** @var string $updatedAt */
$updatedAt = $row['updated_at'];
/** @var string|null $deletedAt */
$deletedAt = $row['deleted_at'];
return Subject::reconstitute(
id: SubjectId::fromString($id),
tenantId: TenantId::fromString($tenantId),
schoolId: SchoolId::fromString($schoolId),
name: new SubjectName($name),
code: new SubjectCode($code),
color: $color !== null ? new SubjectColor($color) : null,
status: SubjectStatus::from($status),
description: $description,
createdAt: new DateTimeImmutable($createdAt),
updatedAt: new DateTimeImmutable($updatedAt),
deletedAt: $deletedAt !== null ? new DateTimeImmutable($deletedAt) : null,
);
}
}

View File

@@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Persistence\InMemory;
use App\Administration\Domain\Exception\SubjectNotFoundException;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\Subject\SubjectCode;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\Subject\SubjectStatus;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Shared\Domain\Tenant\TenantId;
use function mb_strtolower;
use Override;
final class InMemorySubjectRepository implements SubjectRepository
{
/** @var array<string, Subject> Indexed by ID */
private array $byId = [];
/** @var array<string, Subject> Indexed by tenant:school:code */
private array $byTenantSchoolCode = [];
/** @var array<string, string> Maps subject ID to its current code key */
private array $codeKeyById = [];
#[Override]
public function save(Subject $subject): void
{
$subjectIdStr = (string) $subject->id;
// If subject already exists, remove the old code key (handles code changes)
if (isset($this->codeKeyById[$subjectIdStr])) {
$oldKey = $this->codeKeyById[$subjectIdStr];
unset($this->byTenantSchoolCode[$oldKey]);
}
$newKey = $this->codeKey($subject->code, $subject->tenantId, $subject->schoolId);
$this->byId[$subjectIdStr] = $subject;
$this->byTenantSchoolCode[$newKey] = $subject;
$this->codeKeyById[$subjectIdStr] = $newKey;
}
#[Override]
public function get(SubjectId $id): Subject
{
$subject = $this->findById($id);
if ($subject === null) {
throw SubjectNotFoundException::withId($id);
}
return $subject;
}
#[Override]
public function findById(SubjectId $id): ?Subject
{
return $this->byId[(string) $id] ?? null;
}
#[Override]
public function findByCode(
SubjectCode $code,
TenantId $tenantId,
SchoolId $schoolId,
): ?Subject {
$subject = $this->byTenantSchoolCode[$this->codeKey($code, $tenantId, $schoolId)] ?? null;
// Filtrer les matières archivées (comme Doctrine avec deleted_at IS NULL)
if ($subject !== null && $subject->deletedAt !== null) {
return null;
}
return $subject;
}
#[Override]
public function findActiveByTenantAndSchool(
TenantId $tenantId,
SchoolId $schoolId,
): array {
$result = [];
foreach ($this->byId as $subject) {
if ($subject->tenantId->equals($tenantId)
&& $subject->schoolId->equals($schoolId)
&& $subject->status === SubjectStatus::ACTIVE
) {
$result[] = $subject;
}
}
return $result;
}
#[Override]
public function delete(SubjectId $id): void
{
$subjectIdStr = (string) $id;
if (isset($this->byId[$subjectIdStr])) {
if (isset($this->codeKeyById[$subjectIdStr])) {
unset($this->byTenantSchoolCode[$this->codeKeyById[$subjectIdStr]]);
unset($this->codeKeyById[$subjectIdStr]);
}
unset($this->byId[$subjectIdStr]);
}
}
private function codeKey(SubjectCode $code, TenantId $tenantId, SchoolId $schoolId): string
{
return $tenantId . ':' . $schoolId . ':' . mb_strtolower((string) $code, 'UTF-8');
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\School;
use Ramsey\Uuid\Uuid;
/**
* Service temporaire pour résoudre le schoolId à partir du tenant.
*
* TODO: Ce service sera remplacé quand le module Schools sera implémenté.
* Actuellement, il génère un UUID déterministe basé sur le tenantId,
* ce qui suppose qu'un tenant = une école.
*
* Quand le module multi-écoles sera implémenté :
* 1. Ce service lira le schoolId depuis le contexte utilisateur
* 2. Les données existantes devront être migrées vers les vraies écoles
*/
final readonly class SchoolIdResolver
{
/**
* Résout le schoolId pour le tenant courant.
*
* @param string $tenantId L'identifiant du tenant
*
* @return string L'identifiant de l'école (UUID)
*/
public function resolveForTenant(string $tenantId): string
{
// Génère un UUID déterministe basé sur le tenantId
// Cela garantit que le même tenantId donne toujours le même schoolId
return Uuid::uuid5(Uuid::NAMESPACE_DNS, "school-{$tenantId}")->toString();
}
}

View File

@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace App\Administration\Infrastructure\Security;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Infrastructure\Api\Resource\SubjectResource;
use function in_array;
use Override;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Voter pour les autorisations sur les matières.
*
* Règles d'accès :
* - ADMIN et SUPER_ADMIN : accès complet (CRUD)
* - ENSEIGNANT : lecture seule (via affectations)
* - VIE_SCOLAIRE, SECRETARIAT : lecture seule
* - ELEVE et PARENT : pas d'accès direct
*
* @extends Voter<string, Subject|SubjectResource>
*/
final class SubjectVoter extends Voter
{
public const string VIEW = 'SUBJECT_VIEW';
public const string CREATE = 'SUBJECT_CREATE';
public const string EDIT = 'SUBJECT_EDIT';
public const string DELETE = 'SUBJECT_DELETE';
private const array SUPPORTED_ATTRIBUTES = [
self::VIEW,
self::CREATE,
self::EDIT,
self::DELETE,
];
#[Override]
protected function supports(string $attribute, mixed $subject): bool
{
if (!in_array($attribute, self::SUPPORTED_ATTRIBUTES, true)) {
return false;
}
// CREATE, EDIT, DELETE, and VIEW (for collections) don't require a subject
// since authorization is role-based, not object-based
if ($subject === null) {
return true;
}
return $subject instanceof Subject || $subject instanceof SubjectResource;
}
#[Override]
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
// Récupérer le rôle depuis les rôles Symfony
$roles = $user->getRoles();
return match ($attribute) {
self::VIEW => $this->canView($roles),
self::CREATE => $this->canCreate($roles),
self::EDIT => $this->canEdit($roles),
self::DELETE => $this->canDelete($roles),
default => false,
};
}
/**
* @param string[] $roles
*/
private function canView(array $roles): bool
{
// Personnel de l'établissement uniquement
return $this->hasAnyRole($roles, [
Role::SUPER_ADMIN->value,
Role::ADMIN->value,
Role::PROF->value,
Role::VIE_SCOLAIRE->value,
Role::SECRETARIAT->value,
]);
}
/**
* @param string[] $roles
*/
private function canCreate(array $roles): bool
{
// Seuls ADMIN et SUPER_ADMIN peuvent créer des matières
return $this->hasAnyRole($roles, [
Role::SUPER_ADMIN->value,
Role::ADMIN->value,
]);
}
/**
* @param string[] $roles
*/
private function canEdit(array $roles): bool
{
// Seuls ADMIN et SUPER_ADMIN peuvent modifier des matières
return $this->hasAnyRole($roles, [
Role::SUPER_ADMIN->value,
Role::ADMIN->value,
]);
}
/**
* @param string[] $roles
*/
private function canDelete(array $roles): bool
{
// Seuls ADMIN et SUPER_ADMIN peuvent supprimer des matières
return $this->hasAnyRole($roles, [
Role::SUPER_ADMIN->value,
Role::ADMIN->value,
]);
}
/**
* @param string[] $userRoles
* @param string[] $allowedRoles
*/
private function hasAnyRole(array $userRoles, array $allowedRoles): bool
{
foreach ($userRoles as $role) {
if (in_array($role, $allowedRoles, true)) {
return true;
}
}
return false;
}
}