feat: GetPage endpoint

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-16 18:22:20 +01:00
parent 55945adc53
commit 33f5a5568a
14 changed files with 710 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace App\Tests\Domain\Reader\Adapter;
use App\Domain\Reader\Domain\Contract\Repository\ChapterRepositoryInterface;
use App\Domain\Reader\Domain\Exception\ChapterNotFoundException;
use App\Domain\Reader\Domain\Exception\PageNotFoundException;
use App\Domain\Reader\Domain\Model\ChapterContext;
use App\Domain\Reader\Domain\Model\Page;
use App\Domain\Reader\Domain\Model\PageContent;
use App\Domain\Reader\Domain\ValueObject\ChapterId;
use App\Domain\Reader\Domain\ValueObject\PageNumber;
final class InMemoryChapterRepository implements ChapterRepositoryInterface
{
/** @var array<string, array{pages: array<Page>, context: ChapterContext}> */
private array $chapters = [];
public function addChapter(ChapterId $chapterId, ChapterContext $context, array $pages): void
{
$this->chapters[$chapterId->getValue()] = [
'pages' => $pages,
'context' => $context
];
}
public function getPagesForChapter(ChapterId $chapterId, int $page = 1, int $itemsPerPage = 20): array
{
if (!isset($this->chapters[$chapterId->getValue()])) {
return [];
}
$pages = $this->chapters[$chapterId->getValue()]['pages'];
$offset = ($page - 1) * $itemsPerPage;
return array_slice($pages, $offset, $itemsPerPage);
}
public function getChapterContext(ChapterId $chapterId): ChapterContext
{
if (!isset($this->chapters[$chapterId->getValue()])) {
throw ChapterNotFoundException::forChapter($chapterId);
}
return $this->chapters[$chapterId->getValue()]['context'];
}
public function getTotalPagesForChapter(ChapterId $chapterId): int
{
if (!isset($this->chapters[$chapterId->getValue()])) {
return 0;
}
return count($this->chapters[$chapterId->getValue()]['pages']);
}
public function getPreviousChapterId(ChapterId $chapterId): ?ChapterId
{
$currentChapter = $this->getChapterContext($chapterId);
$currentNumber = $currentChapter->getNumber();
$previousChapter = null;
$previousNumber = 0.0;
foreach ($this->chapters as $id => $chapter) {
$number = $chapter['context']->getNumber();
if ($number < $currentNumber && $number > $previousNumber) {
$previousChapter = $id;
$previousNumber = $number;
}
}
return $previousChapter ? new ChapterId($previousChapter) : null;
}
public function getNextChapterId(ChapterId $chapterId): ?ChapterId
{
$currentChapter = $this->getChapterContext($chapterId);
$currentNumber = $currentChapter->getNumber();
$nextChapter = null;
$nextNumber = PHP_FLOAT_MAX;
foreach ($this->chapters as $id => $chapter) {
$number = $chapter['context']->getNumber();
if ($number > $currentNumber && $number < $nextNumber) {
$nextChapter = $id;
$nextNumber = $number;
}
}
return $nextChapter ? new ChapterId($nextChapter) : null;
}
public function getPageContent(ChapterId $chapterId, PageNumber $pageNumber): PageContent
{
if (!isset($this->chapters[$chapterId->getValue()])) {
throw ChapterNotFoundException::forChapter($chapterId);
}
$pages = $this->chapters[$chapterId->getValue()]['pages'];
$index = $pageNumber->getValue() - 1;
if (!isset($pages[$index])) {
throw PageNotFoundException::forPage($chapterId, $pageNumber);
}
$page = $pages[$index];
return new PageContent(
$page->getId(),
$page->getPageNumber(),
base64_encode('fake-image-content'),
'image/jpeg',
800,
600
);
}
}

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace App\Tests\Domain\Reader\Application\QueryHandler;
use App\Domain\Reader\Application\Query\GetChapterPage;
use App\Domain\Reader\Application\QueryHandler\GetChapterPageHandler;
use App\Domain\Reader\Domain\Exception\ChapterNotFoundException;
use App\Domain\Reader\Domain\Exception\PageNotFoundException;
use App\Domain\Reader\Domain\Model\ChapterContext;
use App\Domain\Reader\Domain\Model\Page;
use App\Domain\Reader\Domain\ValueObject\ChapterId;
use App\Domain\Reader\Domain\ValueObject\PageNumber;
use App\Tests\Domain\Reader\Adapter\InMemoryChapterRepository;
use PHPUnit\Framework\TestCase;
final class GetChapterPageHandlerTest extends TestCase
{
private InMemoryChapterRepository $repository;
private GetChapterPageHandler $handler;
protected function setUp(): void
{
$this->repository = new InMemoryChapterRepository();
$this->handler = new GetChapterPageHandler($this->repository);
// Préparation des données de test
$chapterId = new ChapterId('chapter-1');
$context = new ChapterContext(
$chapterId,
null,
null,
'Test Manga',
1.0,
'Chapter 1',
'path/to/cbz',
1,
10,
true,
new \DateTimeImmutable()
);
$pages = [];
for ($i = 1; $i <= 10; $i++) {
$pages[] = new Page(
sprintf('page-%d', $i),
new PageNumber($i),
sprintf('/api/chapters/chapter-1/pages/%d', $i),
800,
600
);
}
$this->repository->addChapter($chapterId, $context, $pages);
}
public function testItThrowsExceptionWhenChapterDoesNotExist(): void
{
$this->expectException(ChapterNotFoundException::class);
$this->handler->handle(new GetChapterPage('invalid-id', 1));
}
public function testItThrowsExceptionWhenPageNumberExceedsTotalPages(): void
{
$this->expectException(PageNotFoundException::class);
$this->handler->handle(new GetChapterPage('chapter-1', 11));
}
public function testItReturnsPageContentSuccessfully(): void
{
$response = $this->handler->handle(new GetChapterPage('chapter-1', 5));
$this->assertEquals('page-5', $response->getId());
$this->assertEquals(5, $response->getPageNumber());
$this->assertNotEmpty($response->getBase64Content());
$this->assertEquals('image/jpeg', $response->getMimeType());
$this->assertEquals(['width' => 800, 'height' => 600], $response->getDimensions());
}
}