feat: Permettre aux enseignants de créer et gérer les devoirs
Some checks failed
CI / Backend Tests (push) Has been cancelled
CI / Frontend Tests (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
CI / Naming Conventions (push) Has been cancelled
CI / Build Check (push) Has been cancelled

Les enseignants avaient besoin d'un outil pour créer des devoirs assignés
à leurs classes, avec filtrage automatique par matière selon la classe
sélectionnée. Le système valide que la date d'échéance tombe un jour
ouvrable (lundi-vendredi) et empêche les dates dans le passé.

Le domaine modélise le devoir comme un agrégat avec pièces jointes,
statut brouillon/publié, et événements métier (création, modification,
suppression). Les handlers de notification écoutent ces événements pour
les futurs envois aux parents et élèves.
This commit is contained in:
2026-03-12 10:11:06 +01:00
parent 56bc808d85
commit e9efb90f59
51 changed files with 4776 additions and 7 deletions

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Command\CreateHomework;
final readonly class CreateHomeworkCommand
{
public function __construct(
public string $tenantId,
public string $classId,
public string $subjectId,
public string $teacherId,
public string $title,
public ?string $description,
public string $dueDate,
) {
}
}

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Command\CreateHomework;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Application\Port\CurrentCalendarProvider;
use App\Scolarite\Application\Port\EnseignantAffectationChecker;
use App\Scolarite\Domain\Exception\EnseignantNonAffecteException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Scolarite\Domain\Service\DueDateValidator;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler(bus: 'command.bus')]
final readonly class CreateHomeworkHandler
{
public function __construct(
private HomeworkRepository $homeworkRepository,
private EnseignantAffectationChecker $affectationChecker,
private CurrentCalendarProvider $calendarProvider,
private DueDateValidator $dueDateValidator,
private Clock $clock,
) {
}
public function __invoke(CreateHomeworkCommand $command): Homework
{
$tenantId = TenantId::fromString($command->tenantId);
$classId = ClassId::fromString($command->classId);
$subjectId = SubjectId::fromString($command->subjectId);
$teacherId = UserId::fromString($command->teacherId);
$now = $this->clock->now();
if (!$this->affectationChecker->estAffecte($teacherId, $classId, $subjectId, $tenantId)) {
throw EnseignantNonAffecteException::pourClasseEtMatiere($teacherId, $classId, $subjectId);
}
$calendar = $this->calendarProvider->forCurrentYear($tenantId);
$dueDate = new DateTimeImmutable($command->dueDate);
$this->dueDateValidator->valider($dueDate, $now, $calendar);
$homework = Homework::creer(
tenantId: $tenantId,
classId: $classId,
subjectId: $subjectId,
teacherId: $teacherId,
title: $command->title,
description: $command->description,
dueDate: $dueDate,
now: $now,
);
$this->homeworkRepository->save($homework);
return $homework;
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Command\DeleteHomework;
final readonly class DeleteHomeworkCommand
{
public function __construct(
public string $tenantId,
public string $homeworkId,
public string $teacherId,
) {
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Command\DeleteHomework;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Domain\Exception\NonProprietaireDuDevoirException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler(bus: 'command.bus')]
final readonly class DeleteHomeworkHandler
{
public function __construct(
private HomeworkRepository $homeworkRepository,
private Clock $clock,
) {
}
public function __invoke(DeleteHomeworkCommand $command): Homework
{
$tenantId = TenantId::fromString($command->tenantId);
$homeworkId = HomeworkId::fromString($command->homeworkId);
$homework = $this->homeworkRepository->get($homeworkId, $tenantId);
$teacherId = UserId::fromString($command->teacherId);
if ((string) $homework->teacherId !== (string) $teacherId) {
throw NonProprietaireDuDevoirException::withId($homeworkId);
}
$homework->supprimer($this->clock->now());
$this->homeworkRepository->save($homework);
return $homework;
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Command\UpdateHomework;
final readonly class UpdateHomeworkCommand
{
public function __construct(
public string $tenantId,
public string $homeworkId,
public string $teacherId,
public string $title,
public ?string $description,
public string $dueDate,
) {
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Command\UpdateHomework;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Application\Port\CurrentCalendarProvider;
use App\Scolarite\Domain\Exception\NonProprietaireDuDevoirException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Scolarite\Domain\Service\DueDateValidator;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler(bus: 'command.bus')]
final readonly class UpdateHomeworkHandler
{
public function __construct(
private HomeworkRepository $homeworkRepository,
private CurrentCalendarProvider $calendarProvider,
private DueDateValidator $dueDateValidator,
private Clock $clock,
) {
}
public function __invoke(UpdateHomeworkCommand $command): Homework
{
$tenantId = TenantId::fromString($command->tenantId);
$homeworkId = HomeworkId::fromString($command->homeworkId);
$now = $this->clock->now();
$homework = $this->homeworkRepository->get($homeworkId, $tenantId);
$teacherId = UserId::fromString($command->teacherId);
if ((string) $homework->teacherId !== (string) $teacherId) {
throw NonProprietaireDuDevoirException::withId($homeworkId);
}
$calendar = $this->calendarProvider->forCurrentYear($tenantId);
$dueDate = new DateTimeImmutable($command->dueDate);
$this->dueDateValidator->valider($dueDate, $now, $calendar);
$homework->modifier(
title: $command->title,
description: $command->description,
dueDate: $dueDate,
now: $now,
);
$this->homeworkRepository->save($homework);
return $homework;
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Command\UploadHomeworkAttachment;
final readonly class UploadHomeworkAttachmentCommand
{
public function __construct(
public string $tenantId,
public string $homeworkId,
public string $filename,
public string $mimeType,
public int $fileSize,
public string $tempFilePath,
) {
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Command\UploadHomeworkAttachment;
use App\Scolarite\Application\Port\FileStorage;
use App\Scolarite\Domain\Exception\PieceJointeInvalideException;
use App\Scolarite\Domain\Model\Homework\HomeworkAttachment;
use App\Scolarite\Domain\Model\Homework\HomeworkAttachmentId;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use function file_get_contents;
use function sprintf;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler(bus: 'command.bus')]
final readonly class UploadHomeworkAttachmentHandler
{
public function __construct(
private HomeworkRepository $homeworkRepository,
private FileStorage $fileStorage,
private Clock $clock,
) {
}
public function __invoke(UploadHomeworkAttachmentCommand $command): HomeworkAttachment
{
$tenantId = TenantId::fromString($command->tenantId);
$homeworkId = HomeworkId::fromString($command->homeworkId);
// Valider que le devoir existe
$this->homeworkRepository->get($homeworkId, $tenantId);
$attachmentId = HomeworkAttachmentId::generate();
$storagePath = sprintf('homework/%s/%s/%s', $command->tenantId, $command->homeworkId, $command->filename);
$content = file_get_contents($command->tempFilePath);
if ($content === false) {
throw PieceJointeInvalideException::lectureFichierImpossible($command->filename);
}
$this->fileStorage->upload($storagePath, $content, $command->mimeType);
return new HomeworkAttachment(
id: $attachmentId,
filename: $command->filename,
filePath: $storagePath,
fileSize: $command->fileSize,
mimeType: $command->mimeType,
uploadedAt: $this->clock->now(),
);
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Application\Port;
interface FileStorage
{
/**
* @param resource|string $content
*/
public function upload(string $path, mixed $content, string $mimeType): string;
public function delete(string $path): void;
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Event;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Shared\Domain\DomainEvent;
use DateTimeImmutable;
use Override;
use Ramsey\Uuid\UuidInterface;
final readonly class DevoirCree implements DomainEvent
{
public function __construct(
public HomeworkId $homeworkId,
public string $classId,
public string $subjectId,
public string $teacherId,
public string $title,
public DateTimeImmutable $dueDate,
private DateTimeImmutable $occurredOn,
) {
}
#[Override]
public function occurredOn(): DateTimeImmutable
{
return $this->occurredOn;
}
#[Override]
public function aggregateId(): UuidInterface
{
return $this->homeworkId->value;
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Event;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Shared\Domain\DomainEvent;
use DateTimeImmutable;
use Override;
use Ramsey\Uuid\UuidInterface;
final readonly class DevoirModifie implements DomainEvent
{
public function __construct(
public HomeworkId $homeworkId,
public string $title,
public DateTimeImmutable $dueDate,
private DateTimeImmutable $occurredOn,
) {
}
#[Override]
public function occurredOn(): DateTimeImmutable
{
return $this->occurredOn;
}
#[Override]
public function aggregateId(): UuidInterface
{
return $this->homeworkId->value;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Event;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Shared\Domain\DomainEvent;
use DateTimeImmutable;
use Override;
use Ramsey\Uuid\UuidInterface;
final readonly class DevoirSupprime implements DomainEvent
{
public function __construct(
public HomeworkId $homeworkId,
private DateTimeImmutable $occurredOn,
) {
}
#[Override]
public function occurredOn(): DateTimeImmutable
{
return $this->occurredOn;
}
#[Override]
public function aggregateId(): UuidInterface
{
return $this->homeworkId->value;
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Exception;
use DateTimeImmutable;
use DomainException;
use function sprintf;
final class DateEcheanceInvalideException extends DomainException
{
public static function dansLePasse(DateTimeImmutable $dueDate): self
{
return new self(sprintf(
'La date d\'échéance "%s" doit être dans le futur.',
$dueDate->format('Y-m-d'),
));
}
public static function jourBloque(DateTimeImmutable $dueDate, string $raison): self
{
return new self(sprintf(
'La date d\'échéance "%s" est bloquée : %s.',
$dueDate->format('Y-m-d'),
$raison,
));
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Exception;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use DomainException;
use function sprintf;
final class DevoirDejaSupprimeException extends DomainException
{
public static function withId(HomeworkId $id): self
{
return new self(sprintf(
'Le devoir avec l\'ID "%s" a déjà été supprimé.',
$id,
));
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Exception;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use DomainException;
use function sprintf;
final class HomeworkNotFoundException extends DomainException
{
public static function withId(HomeworkId $id): self
{
return new self(sprintf(
'Le devoir avec l\'ID "%s" n\'a pas été trouvé.',
$id,
));
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Exception;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use DomainException;
use function sprintf;
final class NonProprietaireDuDevoirException extends DomainException
{
public static function withId(HomeworkId $id): self
{
return new self(sprintf(
'Vous n\'êtes pas le propriétaire du devoir "%s".',
$id,
));
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Exception;
use DomainException;
use function sprintf;
final class PieceJointeInvalideException extends DomainException
{
public static function fichierTropGros(int $fileSize, int $maxSize): self
{
return new self(sprintf(
'Le fichier fait %d octets, la taille maximum est de %d octets (10 Mo).',
$fileSize,
$maxSize,
));
}
public static function typeFichierNonAutorise(string $mimeType): self
{
return new self(sprintf(
'Le type de fichier "%s" n\'est pas autorisé. Types acceptés : PDF, JPEG, PNG.',
$mimeType,
));
}
public static function lectureFichierImpossible(string $filename): self
{
return new self(sprintf(
'Impossible de lire le fichier "%s".',
$filename,
));
}
}

View File

@@ -0,0 +1,144 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Model\Homework;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Domain\Event\DevoirCree;
use App\Scolarite\Domain\Event\DevoirModifie;
use App\Scolarite\Domain\Event\DevoirSupprime;
use App\Scolarite\Domain\Exception\DevoirDejaSupprimeException;
use App\Shared\Domain\AggregateRoot;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
final class Homework extends AggregateRoot
{
public private(set) DateTimeImmutable $updatedAt;
private function __construct(
public private(set) HomeworkId $id,
public private(set) TenantId $tenantId,
public private(set) ClassId $classId,
public private(set) SubjectId $subjectId,
public private(set) UserId $teacherId,
public private(set) string $title,
public private(set) ?string $description,
public private(set) DateTimeImmutable $dueDate,
public private(set) HomeworkStatus $status,
public private(set) DateTimeImmutable $createdAt,
) {
$this->updatedAt = $createdAt;
}
public static function creer(
TenantId $tenantId,
ClassId $classId,
SubjectId $subjectId,
UserId $teacherId,
string $title,
?string $description,
DateTimeImmutable $dueDate,
DateTimeImmutable $now,
): self {
$homework = new self(
id: HomeworkId::generate(),
tenantId: $tenantId,
classId: $classId,
subjectId: $subjectId,
teacherId: $teacherId,
title: $title,
description: $description,
dueDate: $dueDate,
status: HomeworkStatus::PUBLISHED,
createdAt: $now,
);
$homework->recordEvent(new DevoirCree(
homeworkId: $homework->id,
classId: (string) $classId,
subjectId: (string) $subjectId,
teacherId: (string) $teacherId,
title: $title,
dueDate: $dueDate,
occurredOn: $now,
));
return $homework;
}
public function modifier(
string $title,
?string $description,
DateTimeImmutable $dueDate,
DateTimeImmutable $now,
): void {
if ($this->status === HomeworkStatus::DELETED) {
throw DevoirDejaSupprimeException::withId($this->id);
}
$this->title = $title;
$this->description = $description;
$this->dueDate = $dueDate;
$this->updatedAt = $now;
$this->recordEvent(new DevoirModifie(
homeworkId: $this->id,
title: $title,
dueDate: $dueDate,
occurredOn: $now,
));
}
public function supprimer(DateTimeImmutable $now): void
{
if ($this->status === HomeworkStatus::DELETED) {
throw DevoirDejaSupprimeException::withId($this->id);
}
$this->status = HomeworkStatus::DELETED;
$this->updatedAt = $now;
$this->recordEvent(new DevoirSupprime(
homeworkId: $this->id,
occurredOn: $now,
));
}
/**
* @internal Pour usage Infrastructure uniquement
*/
public static function reconstitute(
HomeworkId $id,
TenantId $tenantId,
ClassId $classId,
SubjectId $subjectId,
UserId $teacherId,
string $title,
?string $description,
DateTimeImmutable $dueDate,
HomeworkStatus $status,
DateTimeImmutable $createdAt,
DateTimeImmutable $updatedAt,
): self {
$homework = new self(
id: $id,
tenantId: $tenantId,
classId: $classId,
subjectId: $subjectId,
teacherId: $teacherId,
title: $title,
description: $description,
dueDate: $dueDate,
status: $status,
createdAt: $createdAt,
);
$homework->updatedAt = $updatedAt;
return $homework;
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Model\Homework;
use App\Scolarite\Domain\Exception\PieceJointeInvalideException;
use DateTimeImmutable;
use function in_array;
final class HomeworkAttachment
{
private const int MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 Mo
private const array ALLOWED_MIME_TYPES = [
'application/pdf',
'image/jpeg',
'image/png',
];
public function __construct(
public private(set) HomeworkAttachmentId $id,
public private(set) string $filename,
public private(set) string $filePath,
public private(set) int $fileSize {
set(int $fileSize) {
if ($fileSize > self::MAX_FILE_SIZE) {
throw PieceJointeInvalideException::fichierTropGros($fileSize, self::MAX_FILE_SIZE);
}
$this->fileSize = $fileSize;
}
},
public private(set) string $mimeType {
set(string $mimeType) {
if (!in_array($mimeType, self::ALLOWED_MIME_TYPES, true)) {
throw PieceJointeInvalideException::typeFichierNonAutorise($mimeType);
}
$this->mimeType = $mimeType;
}
},
public private(set) DateTimeImmutable $uploadedAt,
) {
}
}

View File

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

View File

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

View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Model\Homework;
enum HomeworkStatus: string
{
case PUBLISHED = 'published';
case DELETED = 'deleted';
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Repository;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Shared\Domain\Tenant\TenantId;
interface HomeworkRepository
{
public function save(Homework $homework): void;
/** @throws HomeworkNotFoundException */
public function get(HomeworkId $id, TenantId $tenantId): Homework;
public function findById(HomeworkId $id, TenantId $tenantId): ?Homework;
/** @return array<Homework> */
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array;
/** @return array<Homework> */
public function findByClass(ClassId $classId, TenantId $tenantId): array;
public function delete(HomeworkId $id, TenantId $tenantId): void;
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Domain\Service;
use App\Administration\Domain\Model\SchoolCalendar\SchoolCalendar;
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
use DateTimeImmutable;
final readonly class DueDateValidator
{
public function valider(DateTimeImmutable $dueDate, DateTimeImmutable $now, SchoolCalendar $calendar): void
{
$tomorrow = $now->modify('+1 day')->setTime(0, 0);
if ($dueDate < $tomorrow) {
throw DateEcheanceInvalideException::dansLePasse($dueDate);
}
if (!$calendar->estJourOuvre($dueDate)) {
$entry = $calendar->trouverEntreePourDate($dueDate);
$raison = $entry !== null ? $entry->label : 'weekend';
throw DateEcheanceInvalideException::jourBloque($dueDate, $raison);
}
}
}

View File

@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Api\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Administration\Infrastructure\Security\SecurityUser;
use App\Scolarite\Application\Command\CreateHomework\CreateHomeworkCommand;
use App\Scolarite\Application\Command\CreateHomework\CreateHomeworkHandler;
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
use App\Scolarite\Domain\Exception\EnseignantNonAffecteException;
use App\Scolarite\Infrastructure\Api\Resource\HomeworkResource;
use App\Shared\Infrastructure\Tenant\TenantContext;
use Override;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
/**
* @implements ProcessorInterface<HomeworkResource, HomeworkResource>
*/
final readonly class CreateHomeworkProcessor implements ProcessorInterface
{
public function __construct(
private CreateHomeworkHandler $handler,
private TenantContext $tenantContext,
private MessageBusInterface $eventBus,
private Security $security,
) {
}
/**
* @param HomeworkResource $data
*/
#[Override]
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): HomeworkResource
{
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
$user = $this->security->getUser();
if (!$user instanceof SecurityUser) {
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
}
try {
$command = new CreateHomeworkCommand(
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
classId: $data->classId ?? '',
subjectId: $data->subjectId ?? '',
teacherId: $user->userId(),
title: $data->title ?? '',
description: $data->description,
dueDate: $data->dueDate ?? '',
);
$homework = ($this->handler)($command);
foreach ($homework->pullDomainEvents() as $event) {
$this->eventBus->dispatch($event);
}
return HomeworkResource::fromDomain($homework);
} catch (EnseignantNonAffecteException|DateEcheanceInvalideException $e) {
throw new BadRequestHttpException($e->getMessage());
} catch (InvalidUuidStringException $e) {
throw new BadRequestHttpException('UUID invalide : ' . $e->getMessage());
}
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Api\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Administration\Infrastructure\Security\SecurityUser;
use App\Scolarite\Application\Command\DeleteHomework\DeleteHomeworkCommand;
use App\Scolarite\Application\Command\DeleteHomework\DeleteHomeworkHandler;
use App\Scolarite\Domain\Exception\DevoirDejaSupprimeException;
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
use App\Scolarite\Domain\Exception\NonProprietaireDuDevoirException;
use App\Scolarite\Infrastructure\Api\Resource\HomeworkResource;
use App\Shared\Infrastructure\Tenant\TenantContext;
use Override;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
/**
* @implements ProcessorInterface<HomeworkResource, HomeworkResource>
*/
final readonly class DeleteHomeworkProcessor implements ProcessorInterface
{
public function __construct(
private DeleteHomeworkHandler $handler,
private TenantContext $tenantContext,
private MessageBusInterface $eventBus,
private Security $security,
) {
}
/**
* @param HomeworkResource $data
*/
#[Override]
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): HomeworkResource
{
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
$user = $this->security->getUser();
if (!$user instanceof SecurityUser) {
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
}
/** @var string $id */
$id = $uriVariables['id'];
try {
$command = new DeleteHomeworkCommand(
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
homeworkId: $id,
teacherId: $user->userId(),
);
$homework = ($this->handler)($command);
foreach ($homework->pullDomainEvents() as $event) {
$this->eventBus->dispatch($event);
}
return HomeworkResource::fromDomain($homework);
} catch (HomeworkNotFoundException $e) {
throw new NotFoundHttpException($e->getMessage());
} catch (NonProprietaireDuDevoirException $e) {
throw new AccessDeniedHttpException($e->getMessage());
} catch (DevoirDejaSupprimeException $e) {
throw new BadRequestHttpException($e->getMessage());
}
}
}

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Api\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Administration\Infrastructure\Security\SecurityUser;
use App\Scolarite\Application\Command\UpdateHomework\UpdateHomeworkCommand;
use App\Scolarite\Application\Command\UpdateHomework\UpdateHomeworkHandler;
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
use App\Scolarite\Domain\Exception\DevoirDejaSupprimeException;
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
use App\Scolarite\Domain\Exception\NonProprietaireDuDevoirException;
use App\Scolarite\Infrastructure\Api\Resource\HomeworkResource;
use App\Shared\Infrastructure\Tenant\TenantContext;
use Override;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
/**
* @implements ProcessorInterface<HomeworkResource, HomeworkResource>
*/
final readonly class UpdateHomeworkProcessor implements ProcessorInterface
{
public function __construct(
private UpdateHomeworkHandler $handler,
private TenantContext $tenantContext,
private MessageBusInterface $eventBus,
private Security $security,
) {
}
/**
* @param HomeworkResource $data
*/
#[Override]
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): HomeworkResource
{
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
$user = $this->security->getUser();
if (!$user instanceof SecurityUser) {
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
}
/** @var string $id */
$id = $uriVariables['id'];
try {
$command = new UpdateHomeworkCommand(
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
homeworkId: $id,
teacherId: $user->userId(),
title: $data->title ?? '',
description: $data->description,
dueDate: $data->dueDate ?? '',
);
$homework = ($this->handler)($command);
foreach ($homework->pullDomainEvents() as $event) {
$this->eventBus->dispatch($event);
}
return HomeworkResource::fromDomain($homework);
} catch (HomeworkNotFoundException $e) {
throw new NotFoundHttpException($e->getMessage());
} catch (NonProprietaireDuDevoirException $e) {
throw new AccessDeniedHttpException($e->getMessage());
} catch (DateEcheanceInvalideException|DevoirDejaSupprimeException $e) {
throw new BadRequestHttpException($e->getMessage());
}
}
}

View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Api\Provider;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Domain\Repository\ClassRepository;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Administration\Infrastructure\Security\SecurityUser;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Scolarite\Infrastructure\Api\Resource\HomeworkResource;
use App\Shared\Infrastructure\Tenant\TenantContext;
use function array_map;
use Override;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
/**
* @implements ProviderInterface<HomeworkResource>
*/
final readonly class HomeworkCollectionProvider implements ProviderInterface
{
public function __construct(
private HomeworkRepository $homeworkRepository,
private TenantContext $tenantContext,
private Security $security,
private ClassRepository $classRepository,
private SubjectRepository $subjectRepository,
) {
}
/** @return array<HomeworkResource> */
#[Override]
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
{
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
$user = $this->security->getUser();
if (!$user instanceof SecurityUser) {
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
}
$tenantId = $this->tenantContext->getCurrentTenantId();
$teacherId = UserId::fromString($user->userId());
$homeworks = $this->homeworkRepository->findByTeacher($teacherId, $tenantId);
return array_map(fn (Homework $homework) => HomeworkResource::fromDomain(
$homework,
$this->resolveClassName($homework),
$this->resolveSubjectName($homework),
), $homeworks);
}
private function resolveClassName(Homework $homework): ?string
{
$class = $this->classRepository->findById($homework->classId);
return $class !== null ? (string) $class->name : null;
}
private function resolveSubjectName(Homework $homework): ?string
{
$subject = $this->subjectRepository->findById($homework->subjectId);
return $subject !== null ? (string) $subject->name : null;
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Api\Provider;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Administration\Domain\Repository\ClassRepository;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Administration\Infrastructure\Security\SecurityUser;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Scolarite\Infrastructure\Api\Resource\HomeworkResource;
use App\Shared\Infrastructure\Tenant\TenantContext;
use Override;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
/**
* @implements ProviderInterface<HomeworkResource>
*/
final readonly class HomeworkItemProvider implements ProviderInterface
{
public function __construct(
private HomeworkRepository $homeworkRepository,
private TenantContext $tenantContext,
private Security $security,
private ClassRepository $classRepository,
private SubjectRepository $subjectRepository,
) {
}
#[Override]
public function provide(Operation $operation, array $uriVariables = [], array $context = []): HomeworkResource
{
if (!$this->tenantContext->hasTenant()) {
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
}
$user = $this->security->getUser();
if (!$user instanceof SecurityUser) {
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
}
/** @var string $id */
$id = $uriVariables['id'];
$homework = $this->homeworkRepository->findById(
HomeworkId::fromString($id),
$this->tenantContext->getCurrentTenantId(),
);
if ($homework === null) {
throw new NotFoundHttpException('Devoir non trouvé.');
}
if ((string) $homework->teacherId !== $user->userId()) {
throw new AccessDeniedHttpException('Vous n\'êtes pas le propriétaire de ce devoir.');
}
$class = $this->classRepository->findById($homework->classId);
$subject = $this->subjectRepository->findById($homework->subjectId);
return HomeworkResource::fromDomain(
$homework,
$class !== null ? (string) $class->name : null,
$subject !== null ? (string) $subject->name : null,
);
}
}

View File

@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\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\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Infrastructure\Api\Processor\CreateHomeworkProcessor;
use App\Scolarite\Infrastructure\Api\Processor\DeleteHomeworkProcessor;
use App\Scolarite\Infrastructure\Api\Processor\UpdateHomeworkProcessor;
use App\Scolarite\Infrastructure\Api\Provider\HomeworkCollectionProvider;
use App\Scolarite\Infrastructure\Api\Provider\HomeworkItemProvider;
use DateTimeImmutable;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource(
shortName: 'Homework',
operations: [
new GetCollection(
uriTemplate: '/homework',
provider: HomeworkCollectionProvider::class,
name: 'get_homework_list',
),
new Get(
uriTemplate: '/homework/{id}',
provider: HomeworkItemProvider::class,
name: 'get_homework',
),
new Post(
uriTemplate: '/homework',
processor: CreateHomeworkProcessor::class,
validationContext: ['groups' => ['Default', 'create']],
name: 'create_homework',
),
new Patch(
uriTemplate: '/homework/{id}',
provider: HomeworkItemProvider::class,
processor: UpdateHomeworkProcessor::class,
validationContext: ['groups' => ['Default', 'update']],
name: 'update_homework',
),
new Delete(
uriTemplate: '/homework/{id}',
provider: HomeworkItemProvider::class,
processor: DeleteHomeworkProcessor::class,
name: 'delete_homework',
),
],
)]
final class HomeworkResource
{
#[ApiProperty(identifier: true)]
public ?string $id = null;
#[Assert\NotBlank(message: 'La classe est requise.', groups: ['create'])]
#[Assert\Uuid(message: 'L\'identifiant de la classe doit être un UUID valide.', groups: ['create'])]
public ?string $classId = null;
#[Assert\NotBlank(message: 'La matière est requise.', groups: ['create'])]
#[Assert\Uuid(message: 'L\'identifiant de la matière doit être un UUID valide.', groups: ['create'])]
public ?string $subjectId = null;
public ?string $teacherId = null;
#[Assert\NotBlank(message: 'Le titre est requis.', groups: ['create', 'update'])]
#[Assert\Length(max: 255, maxMessage: 'Le titre ne peut pas dépasser 255 caractères.')]
public ?string $title = null;
public ?string $description = null;
#[Assert\NotBlank(message: 'La date d\'échéance est requise.', groups: ['create', 'update'])]
public ?string $dueDate = null;
public ?string $status = null;
public ?string $className = null;
public ?string $subjectName = null;
public ?DateTimeImmutable $createdAt = null;
public ?DateTimeImmutable $updatedAt = null;
public static function fromDomain(
Homework $homework,
?string $className = null,
?string $subjectName = null,
): self {
$resource = new self();
$resource->id = (string) $homework->id;
$resource->classId = (string) $homework->classId;
$resource->subjectId = (string) $homework->subjectId;
$resource->teacherId = (string) $homework->teacherId;
$resource->title = $homework->title;
$resource->description = $homework->description;
$resource->dueDate = $homework->dueDate->format('Y-m-d');
$resource->status = $homework->status->value;
$resource->className = $className;
$resource->subjectName = $subjectName;
$resource->createdAt = $homework->createdAt;
$resource->updatedAt = $homework->updatedAt;
return $resource;
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Messaging;
use App\Scolarite\Domain\Event\DevoirCree;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
/**
* Réagit à la création d'un devoir.
*
* @todo #0 Implémenter la notification push/email aux élèves et parents (dépend de Epic 9)
*/
#[AsMessageHandler(bus: 'event.bus')]
final readonly class OnDevoirCreeHandler
{
public function __construct(
private LoggerInterface $logger,
) {
}
public function __invoke(DevoirCree $event): void
{
$this->logger->info('Nouveau devoir créé — notification prévue', [
'homework_id' => (string) $event->homeworkId,
'class_id' => $event->classId,
'subject_id' => $event->subjectId,
'teacher_id' => $event->teacherId,
'title' => $event->title,
'due_date' => $event->dueDate->format('Y-m-d'),
]);
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Messaging;
use App\Scolarite\Domain\Event\DevoirModifie;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
/**
* Réagit à la modification d'un devoir publié.
*
* @todo #0 Implémenter la notification push/email aux concernés (dépend de Epic 9)
*/
#[AsMessageHandler(bus: 'event.bus')]
final readonly class OnDevoirModifieHandler
{
public function __construct(
private LoggerInterface $logger,
) {
}
public function __invoke(DevoirModifie $event): void
{
$this->logger->info('Devoir modifié — notification prévue', [
'homework_id' => (string) $event->homeworkId,
'title' => $event->title,
'due_date' => $event->dueDate->format('Y-m-d'),
]);
}
}

View File

@@ -0,0 +1,172 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Persistence\Doctrine;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Model\Homework\HomeworkStatus;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Shared\Domain\Tenant\TenantId;
use function array_map;
use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Override;
final readonly class DoctrineHomeworkRepository implements HomeworkRepository
{
public function __construct(
private Connection $connection,
) {
}
#[Override]
public function save(Homework $homework): void
{
$this->connection->executeStatement(
'INSERT INTO homework (id, tenant_id, class_id, subject_id, teacher_id, title, description, due_date, status, created_at, updated_at)
VALUES (:id, :tenant_id, :class_id, :subject_id, :teacher_id, :title, :description, :due_date, :status, :created_at, :updated_at)
ON CONFLICT (id) DO UPDATE SET
title = EXCLUDED.title,
description = EXCLUDED.description,
due_date = EXCLUDED.due_date,
status = EXCLUDED.status,
updated_at = EXCLUDED.updated_at',
[
'id' => (string) $homework->id,
'tenant_id' => (string) $homework->tenantId,
'class_id' => (string) $homework->classId,
'subject_id' => (string) $homework->subjectId,
'teacher_id' => (string) $homework->teacherId,
'title' => $homework->title,
'description' => $homework->description,
'due_date' => $homework->dueDate->format('Y-m-d'),
'status' => $homework->status->value,
'created_at' => $homework->createdAt->format(DateTimeImmutable::ATOM),
'updated_at' => $homework->updatedAt->format(DateTimeImmutable::ATOM),
],
);
}
#[Override]
public function get(HomeworkId $id, TenantId $tenantId): Homework
{
$homework = $this->findById($id, $tenantId);
if ($homework === null) {
throw HomeworkNotFoundException::withId($id);
}
return $homework;
}
#[Override]
public function findById(HomeworkId $id, TenantId $tenantId): ?Homework
{
$row = $this->connection->fetchAssociative(
'SELECT * FROM homework WHERE id = :id AND tenant_id = :tenant_id',
['id' => (string) $id, 'tenant_id' => (string) $tenantId],
);
if ($row === false) {
return null;
}
return $this->hydrate($row);
}
#[Override]
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array
{
$rows = $this->connection->fetchAllAssociative(
'SELECT * FROM homework
WHERE teacher_id = :teacher_id
AND tenant_id = :tenant_id
AND status != :deleted
ORDER BY due_date ASC',
[
'teacher_id' => (string) $teacherId,
'tenant_id' => (string) $tenantId,
'deleted' => HomeworkStatus::DELETED->value,
],
);
return array_map($this->hydrate(...), $rows);
}
#[Override]
public function findByClass(ClassId $classId, TenantId $tenantId): array
{
$rows = $this->connection->fetchAllAssociative(
'SELECT * FROM homework
WHERE class_id = :class_id
AND tenant_id = :tenant_id
AND status != :deleted
ORDER BY due_date ASC',
[
'class_id' => (string) $classId,
'tenant_id' => (string) $tenantId,
'deleted' => HomeworkStatus::DELETED->value,
],
);
return array_map($this->hydrate(...), $rows);
}
#[Override]
public function delete(HomeworkId $id, TenantId $tenantId): void
{
$this->connection->executeStatement(
'DELETE FROM homework WHERE id = :id AND tenant_id = :tenant_id',
['id' => (string) $id, 'tenant_id' => (string) $tenantId],
);
}
/** @param array<string, mixed> $row */
private function hydrate(array $row): Homework
{
/** @var string $id */
$id = $row['id'];
/** @var string $tenantId */
$tenantId = $row['tenant_id'];
/** @var string $classId */
$classId = $row['class_id'];
/** @var string $subjectId */
$subjectId = $row['subject_id'];
/** @var string $teacherId */
$teacherId = $row['teacher_id'];
/** @var string $title */
$title = $row['title'];
/** @var string|null $description */
$description = $row['description'];
/** @var string $dueDate */
$dueDate = $row['due_date'];
/** @var string $status */
$status = $row['status'];
/** @var string $createdAt */
$createdAt = $row['created_at'];
/** @var string $updatedAt */
$updatedAt = $row['updated_at'];
return Homework::reconstitute(
id: HomeworkId::fromString($id),
tenantId: TenantId::fromString($tenantId),
classId: ClassId::fromString($classId),
subjectId: SubjectId::fromString($subjectId),
teacherId: UserId::fromString($teacherId),
title: $title,
description: $description,
dueDate: new DateTimeImmutable($dueDate),
status: HomeworkStatus::from($status),
createdAt: new DateTimeImmutable($createdAt),
updatedAt: new DateTimeImmutable($updatedAt),
);
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Persistence\InMemory;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Model\Homework\HomeworkStatus;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Shared\Domain\Tenant\TenantId;
use function array_filter;
use function array_values;
use Override;
final class InMemoryHomeworkRepository implements HomeworkRepository
{
/** @var array<string, Homework> */
private array $byId = [];
#[Override]
public function save(Homework $homework): void
{
$this->byId[(string) $homework->id] = $homework;
}
#[Override]
public function get(HomeworkId $id, TenantId $tenantId): Homework
{
$homework = $this->findById($id, $tenantId);
if ($homework === null) {
throw HomeworkNotFoundException::withId($id);
}
return $homework;
}
#[Override]
public function findById(HomeworkId $id, TenantId $tenantId): ?Homework
{
$homework = $this->byId[(string) $id] ?? null;
if ($homework === null || !$homework->tenantId->equals($tenantId)) {
return null;
}
return $homework;
}
#[Override]
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array
{
return array_values(array_filter(
$this->byId,
static fn (Homework $h): bool => $h->teacherId->equals($teacherId)
&& $h->tenantId->equals($tenantId)
&& $h->status !== HomeworkStatus::DELETED,
));
}
#[Override]
public function findByClass(ClassId $classId, TenantId $tenantId): array
{
return array_values(array_filter(
$this->byId,
static fn (Homework $h): bool => $h->classId->equals($classId)
&& $h->tenantId->equals($tenantId)
&& $h->status !== HomeworkStatus::DELETED,
));
}
#[Override]
public function delete(HomeworkId $id, TenantId $tenantId): void
{
$key = (string) $id;
if (isset($this->byId[$key]) && $this->byId[$key]->tenantId->equals($tenantId)) {
unset($this->byId[$key]);
}
}
}

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace App\Scolarite\Infrastructure\Storage;
use App\Scolarite\Application\Port\FileStorage;
use function dirname;
use function file_put_contents;
use function is_dir;
use function is_file;
use function is_string;
use function mkdir;
use Override;
use function unlink;
final readonly class LocalFileStorage implements FileStorage
{
public function __construct(
private string $storagePath,
) {
}
#[Override]
public function upload(string $path, mixed $content, string $mimeType): string
{
$fullPath = $this->storagePath . '/' . $path;
$dir = dirname($fullPath);
if (!is_dir($dir)) {
mkdir($dir, 0o755, true);
}
$data = is_string($content) ? $content : '';
file_put_contents($fullPath, $data);
return $path;
}
#[Override]
public function delete(string $path): void
{
$fullPath = $this->storagePath . '/' . $path;
if (is_file($fullPath)) {
unlink($fullPath);
}
}
}