Files
Mangarr/src/Domain/Manga/Application/CommandHandler/DeleteChapterHandler.php

42 lines
1.4 KiB
PHP

<?php
namespace App\Domain\Manga\Application\CommandHandler;
use App\Domain\Manga\Application\Command\DeleteChapter;
use App\Domain\Manga\Domain\Contract\Repository\ChapterRepositoryInterface;
use App\Domain\Manga\Domain\Exception\ChapterNotFoundException;
use App\Domain\Shared\Domain\Contract\CommandHandlerInterface;
use App\Domain\Shared\Domain\Contract\CommandInterface;
readonly class DeleteChapterHandler implements CommandHandlerInterface
{
public function __construct(
private ChapterRepositoryInterface $chapterRepository
) {}
public function handle(CommandInterface $command): void
{
assert($command instanceof DeleteChapter);
$chapter = $this->chapterRepository->findVisibleById($command->chapterId);
if (!$chapter) {
throw new ChapterNotFoundException($command->chapterId);
}
// Soft delete by setting isVisible to false
$updatedChapter = new \App\Domain\Manga\Domain\Model\Chapter(
new \App\Domain\Manga\Domain\Model\ValueObject\ChapterId($chapter->getId()),
$chapter->getMangaId(),
$chapter->getNumber(),
$chapter->getTitle(),
$chapter->getVolume(),
false, // isVisible = false (soft delete)
$chapter->isAvailable(),
$chapter->getCreatedAt()
);
$this->chapterRepository->save($updatedChapter);
}
}