refactor(manga): Chapter entité DDD de Manga + AggregateRoot

- Ajoute AggregateRoot dans Shared (domain events + pull pattern)
- Manga extends AggregateRoot, devient vrai aggregate root DDD
- Chapter passe de readonly à entité mutable avec MangaId VO
- Manga expose les méthodes domaine pour toute mutation de chapitre :
  addChapter, updateChapterTitle/Volume/Pages, hideChapter, removeChapterPages
- Supprime saveChapter/updateChapter/deleteChapter de MangaRepositoryInterface
- save(Manga) gère désormais la persistance des chapitres via pull pattern
- Tous les handlers/listeners passent par l'agrégat (plus d'accès direct)
- phparkitect autorise AggregateRoot dans les couches Domain

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2026-03-09 19:15:11 +01:00
parent a4b3d8a5f1
commit 2c051351a8
18 changed files with 226 additions and 255 deletions

View File

@@ -7,8 +7,6 @@ use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Contract\Service\FileServiceInterface;
use App\Domain\Manga\Domain\Exception\ChapterNotFoundException;
use App\Domain\Manga\Domain\Exception\CbzFileNotFoundException;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
use App\Domain\Shared\Domain\Contract\CommandHandlerInterface;
use App\Domain\Shared\Domain\Contract\CommandInterface;
@@ -33,18 +31,8 @@ readonly class DeleteCbzHandler implements CommandHandlerInterface
throw new CbzFileNotFoundException($command->chapterId);
}
$updatedChapter = new Chapter(
new ChapterId($chapter->getId()),
$chapter->getMangaId(),
$chapter->getNumber(),
$chapter->getTitle(),
$chapter->getVolume(),
$chapter->isVisible(),
null,
0,
$chapter->getCreatedAt()
);
$this->mangaRepository->updateChapter($updatedChapter);
$manga = $this->mangaRepository->findById($chapter->getMangaId()->getValue());
$manga->removeChapterPages($chapter);
$this->mangaRepository->save($manga);
}
}

View File

@@ -5,8 +5,6 @@ namespace App\Domain\Manga\Application\CommandHandler;
use App\Domain\Manga\Application\Command\DeleteChapter;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Exception\ChapterNotFoundException;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
use App\Domain\Shared\Domain\Contract\CommandHandlerInterface;
use App\Domain\Shared\Domain\Contract\CommandInterface;
@@ -26,18 +24,8 @@ readonly class DeleteChapterHandler implements CommandHandlerInterface
throw new ChapterNotFoundException($command->chapterId);
}
$updatedChapter = new Chapter(
id: new ChapterId($chapter->getId()),
mangaId: $chapter->getMangaId(),
number: $chapter->getNumber(),
title: $chapter->getTitle(),
volume: $chapter->getVolume(),
isVisible: false,
pagesDirectory: $chapter->getPagesDirectory(),
pageCount: $chapter->getPageCount(),
createdAt: $chapter->getCreatedAt()
);
$this->mangaRepository->updateChapter($updatedChapter);
$manga = $this->mangaRepository->findById($chapter->getMangaId()->getValue());
$manga->hideChapter($chapter);
$this->mangaRepository->save($manga);
}
}

View File

@@ -21,17 +21,17 @@ readonly class EditMultipleChaptersHandler
throw new ChapterNotFoundException($chapterData->id);
}
$updatedChapter = $chapter;
$manga = $this->mangaRepository->findById($chapter->getMangaId()->getValue());
if ($chapterData->title !== null) {
$updatedChapter = $updatedChapter->updateTitle($chapterData->title);
$manga->updateChapterTitle($chapter, $chapterData->title);
}
if ($chapterData->volume !== null) {
$updatedChapter = $updatedChapter->updateVolume($chapterData->volume);
$manga->updateChapterVolume($chapter, $chapterData->volume);
}
$this->mangaRepository->updateChapter($updatedChapter);
$this->mangaRepository->save($manga);
}
}
}

View File

@@ -29,5 +29,6 @@ readonly class FetchMangaChaptersHandler
// Synchronisation initiale (pas d'événements)
$this->chapterSynchronizationService->synchronizeChapters($manga);
$this->mangaRepository->save($manga);
}
}

View File

@@ -6,8 +6,6 @@ use App\Domain\Manga\Application\Command\ImportChapter;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
use App\Domain\Manga\Domain\Exception\ChapterNotFoundException;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
use App\Domain\Shared\Domain\Contract\MangaPathManagerInterface;
readonly class ImportChapterHandler
@@ -43,20 +41,9 @@ readonly class ImportChapterHandler
// 4. Save the CBZ file to storage using the path manager
$cbzPath = $this->saveCbzFile($command, $manga, $existingChapter);
// 5. Update existing chapter with new path
// Note: pagesDirectory holds CBZ path during transition; Phase 3 will store individual images
$updatedChapter = new Chapter(
id: new ChapterId($existingChapter->getId()),
mangaId: $existingChapter->getMangaId(),
number: $existingChapter->getNumber(),
title: $existingChapter->getTitle(),
volume: $existingChapter->getVolume(),
isVisible: $existingChapter->isVisible(),
pagesDirectory: $cbzPath,
pageCount: $existingChapter->getPageCount(),
createdAt: $existingChapter->getCreatedAt()
);
$this->mangaRepository->updateChapter($updatedChapter);
// 5. Update existing chapter with new path through the aggregate
$manga->updateChapterPages($existingChapter, $cbzPath, $existingChapter->getPageCount());
$this->mangaRepository->save($manga);
}
private function isValidCbzFile(string $fileBinary): bool
@@ -66,7 +53,7 @@ readonly class ImportChapterHandler
return strpos($fileBinary, $zipMagicNumber) === 0;
}
private function saveCbzFile(ImportChapter $command, \App\Domain\Manga\Domain\Model\Manga $manga, Chapter $chapter): string
private function saveCbzFile(ImportChapter $command, \App\Domain\Manga\Domain\Model\Manga $manga, \App\Domain\Manga\Domain\Model\Chapter $chapter): string
{
$volumeNumber = $chapter->getVolume() ?? 0;
$cbzPath = $this->pathManager->buildChapterCbzPath(

View File

@@ -5,8 +5,6 @@ namespace App\Domain\Manga\Application\CommandHandler;
use App\Domain\Manga\Application\Command\ImportVolume;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
use App\Domain\Shared\Domain\Contract\MangaPathManagerInterface;
readonly class ImportVolumeHandler
@@ -44,22 +42,11 @@ readonly class ImportVolumeHandler
// 4. Save the CBZ file to storage using the path manager
$cbzPath = $this->saveCbzFile($command, $manga);
// 5. Update all chapters with the volume path
// Note: pagesDirectory holds CBZ path during transition; Phase 3 will store individual images
// 5. Update all chapters with the volume path through the aggregate
foreach ($chapters as $chapter) {
$updatedChapter = new Chapter(
id: new ChapterId($chapter->getId()),
mangaId: $chapter->getMangaId(),
number: $chapter->getNumber(),
title: $chapter->getTitle(),
volume: $chapter->getVolume(),
isVisible: $chapter->isVisible(),
pagesDirectory: $cbzPath,
pageCount: $chapter->getPageCount(),
createdAt: $chapter->getCreatedAt()
);
$this->mangaRepository->updateChapter($updatedChapter);
$manga->updateChapterPages($chapter, $cbzPath, $chapter->getPageCount());
}
$this->mangaRepository->save($manga);
}
private function isValidCbzFile(string $fileBinary): bool

View File

@@ -5,8 +5,6 @@ declare(strict_types=1);
namespace App\Domain\Manga\Application\EventListener;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
use App\Domain\Shared\Domain\Event\ChapterImported;
@@ -26,18 +24,8 @@ readonly class ChapterImportedEventListener
$chapters = $this->mangaRepository->findVisibleChaptersByMangaIdAndVolume($manga->getId()->getValue(), (int) $event->volume);
foreach ($chapters as $chapter) {
if ($chapter->getNumber() === (float) $event->chapterNumber) {
$updated = new Chapter(
new ChapterId($chapter->getId()),
$chapter->getMangaId(),
$chapter->getNumber(),
$chapter->getTitle(),
$chapter->getVolume(),
$chapter->isVisible(),
$event->cbzPath,
$chapter->getPageCount(),
$chapter->getCreatedAt(),
);
$this->mangaRepository->updateChapter($updated);
$manga->updateChapterPages($chapter, $event->cbzPath, $chapter->getPageCount());
$this->mangaRepository->save($manga);
break;
}
}

View File

@@ -5,8 +5,6 @@ declare(strict_types=1);
namespace App\Domain\Manga\Application\EventListener;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
use App\Domain\Shared\Domain\Event\VolumeImported;
@@ -29,18 +27,8 @@ readonly class VolumeImportedEventListener
}
foreach ($chapters as $chapter) {
$updated = new Chapter(
new ChapterId($chapter->getId()),
$chapter->getMangaId(),
$chapter->getNumber(),
$chapter->getTitle(),
$chapter->getVolume(),
$chapter->isVisible(),
$event->cbzPath,
$chapter->getPageCount(),
$chapter->getCreatedAt(),
);
$this->mangaRepository->updateChapter($updated);
$manga->updateChapterPages($chapter, $event->cbzPath, $chapter->getPageCount());
}
$this->mangaRepository->save($manga);
}
}