Skip to content

Commit d53e1e7

Browse files
committed
Check hard limit in client methods
1 parent eb253e2 commit d53e1e7

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

lib/Client/CurseForgeAPIClient.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ public function setHttpClient(?ClientInterface $httpClient): static
131131
* @return PaginatedGameList
132132
* @throws ApiException
133133
*/
134-
public function getGames(int $offset = 0, int $pageSize = 50): PaginatedGameList
134+
public function getGames(int $offset = 0, int $pageSize = PaginatedGameList::MAX_PAGE_SIZE): PaginatedGameList
135135
{
136-
return new PaginatedGameList($this, $this->games->getGames($offset, $pageSize));
136+
return new PaginatedGameList($this, $this->games->getGames(
137+
$offset,
138+
PaginatedGameList::getAllowedPageSize($offset, $pageSize),
139+
));
137140
}
138141

139142
/**
@@ -243,7 +246,7 @@ public function searchMods(ModSearchOptions $options): PaginatedModList
243246
$options->getAuthorId(),
244247
$options->getSlug(),
245248
$options->getOffset(),
246-
$options->getPageSize()
249+
PaginatedModList::getAllowedPageSize($options->getOffset(), $options->getPageSize()),
247250
), $options);
248251
}
249252

@@ -313,7 +316,7 @@ public function getModFiles(ModFilesOptions $options): PaginatedFilesList
313316
$options->getModLoaderType()?->value,
314317
$options->getGameVersionTypeId(),
315318
$options->getOffset(),
316-
$options->getPageSize(),
319+
PaginatedFilesList::getAllowedPageSize($options->getOffset(), $options->getPageSize()),
317320
), $options);
318321
}
319322

lib/Client/List/PaginatedList.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,30 @@ abstract class PaginatedList implements Iterator, ArrayAccess, Countable
2020
{
2121
/**
2222
* The limit for how many results are allowed to be requested.
23-
* @type int
23+
* @var int
2424
*/
2525
public const LIMIT = 10_000;
2626

27+
/**
28+
* The maximum page size that can be requested.
29+
* @var int
30+
*/
31+
public const MAX_PAGE_SIZE = 50;
32+
2733
protected int $iterator = 0;
2834

35+
36+
/**
37+
* Get the maximum page size that can be requested.
38+
* @param int $offset
39+
* @param int $target
40+
* @return int
41+
*/
42+
public static function getAllowedPageSize(int $offset, int $target = self::MAX_PAGE_SIZE): int
43+
{
44+
return min($target, self::MAX_PAGE_SIZE, static::LIMIT - $offset);
45+
}
46+
2947
protected function __construct(
3048
protected Pagination $pagination,
3149
/**
@@ -235,7 +253,7 @@ public function count(): int
235253
*/
236254
protected function getNextPageSize(int $offset): int
237255
{
238-
return min($this->pagination->getPageSize(), static::LIMIT - $offset);
256+
return static::getAllowedPageSize($offset, $this->pagination->getPageSize());
239257
}
240258

241259
/**

lib/Client/Options/ModFiles/ModFilesOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Aternos\CurseForgeApi\Client\Options\ModFiles;
44

5+
use Aternos\CurseForgeApi\Client\List\PaginatedFilesList;
56
use Aternos\CurseForgeApi\Client\Options\ModSearch\ModLoaderType;
67

78
class ModFilesOptions
@@ -12,7 +13,7 @@ public function __construct(
1213
protected ?ModLoaderType $modLoaderType = null,
1314
protected ?int $gameVersionTypeId = null,
1415
protected int $offset = 0,
15-
protected int $pageSize = 50,
16+
protected int $pageSize = PaginatedFilesList::MAX_PAGE_SIZE,
1617
)
1718
{
1819
}

lib/Client/Options/ModSearch/ModSearchOptions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Aternos\CurseForgeApi\Client\Options\ModSearch;
44

5+
use Aternos\CurseForgeApi\Client\List\PaginatedModList;
6+
57
class ModSearchOptions
68
{
79
/**
@@ -32,7 +34,7 @@ public function __construct(
3234
protected ?int $authorId = null,
3335
protected ?string $slug = null,
3436
protected int $offset = 0,
35-
protected int $pageSize = 50,
37+
protected int $pageSize = PaginatedModList::MAX_PAGE_SIZE,
3638
)
3739
{
3840
}

tests/Integration/Client/ClientTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ public function testSearchMods()
211211
}
212212

213213
public function testSearchModsLastPage()
214+
{
215+
$options = new ModSearchOptions(static::MINECRAFT_GAME_ID);
216+
$options->setPageSize(50);
217+
$options->setOffset(9_960);
218+
219+
$mods = $this->apiClient->searchMods($options);
220+
$this->assertNotEmpty($mods);
221+
$this->assertFalse($mods->hasNextPage());
222+
$this->assertEquals($mods::LIMIT, $mods->getPagination()->getIndex() + $mods->getPagination()->getPageSize());
223+
}
224+
225+
public function testSearchModsLastPageNext()
214226
{
215227
$options = new ModSearchOptions(static::MINECRAFT_GAME_ID);
216228
$options->setPageSize(50);

0 commit comments

Comments
 (0)