Skip to content

Commit

Permalink
Check hard limit in paginated responses
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Jul 18, 2024
1 parent 3e8ccaf commit eb253e2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/Client/List/PaginatedFilesList.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public function __construct(
}, $filesResponse->getData()));
}

public function getOffset(int $offset): static
public function getOffset(int $offset, int $pageSize): static
{
$options = clone $this->options;
$options->setOffset($offset);
$options->setPageSize($pageSize);
return $this->client->getModFiles($options);
}
}
4 changes: 2 additions & 2 deletions lib/Client/List/PaginatedGameList.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function __construct(
}, $gamesResponse->getData()));
}

public function getOffset(int $offset): static
public function getOffset(int $offset, int $pageSize): static
{
return $this->client->getGames($offset, $this->pagination->getPageSize());
return $this->client->getGames($offset, $pageSize);
}
}
52 changes: 39 additions & 13 deletions lib/Client/List/PaginatedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/
abstract class PaginatedList implements Iterator, ArrayAccess, Countable
{
/**
* The limit for how many results are allowed to be requested.
* @type int
*/
public const LIMIT = 10_000;

protected int $iterator = 0;

protected function __construct(
Expand All @@ -30,6 +36,15 @@ protected function __construct(
{
}

/**
* Get the pagination part of the response
* @return Pagination
*/
public function getPagination(): Pagination
{
return $this->pagination;
}

/**
* @return T[]
*/
Expand All @@ -41,18 +56,19 @@ public function getResults(): array
/**
* Get a new page of results starting at the given offset
* @param int $offset
* @param int $pageSize
* @return $this
* @throws ApiException
*/
public abstract function getOffset(int $offset): static;
public abstract function getOffset(int $offset, int $pageSize): static;

/**
* returns true if there is a next page with results on it
* @return bool
*/
public function hasNextPage(): bool
{
return $this->pagination->getTotalCount() > $this->getNextOffset();
return min($this->pagination->getTotalCount(), static::LIMIT) > $this->getNextOffset();
}

/**
Expand All @@ -67,16 +83,8 @@ public function getNextPage(): ?static
return null;
}

return $this->getOffset($this->getNextOffset());
}

/**
* get the offset of the next page
* @return int
*/
protected function getNextOffset(): int
{
return $this->pagination->getIndex() + $this->pagination->getPageSize();
$offset = $this->getNextOffset();
return $this->getOffset($offset, $this->getNextPageSize($offset));
}

/**
Expand Down Expand Up @@ -110,7 +118,7 @@ public function getPreviousPage(): ?static
return null;
}

return $this->getOffset($this->getPreviousOffset());
return $this->getOffset($this->getPreviousOffset(), $this->pagination->getPageSize());
}

/**
Expand Down Expand Up @@ -220,4 +228,22 @@ public function count(): int
{
return count($this->results);
}

/**
* @param int $offset
* @return int
*/
protected function getNextPageSize(int $offset): int
{
return min($this->pagination->getPageSize(), static::LIMIT - $offset);
}

/**
* get the offset of the next page
* @return int
*/
protected function getNextOffset(): int
{
return $this->pagination->getIndex() + $this->pagination->getPageSize();
}
}
3 changes: 2 additions & 1 deletion lib/Client/List/PaginatedModList.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public function __construct(
}, $this->response->getData()));
}

public function getOffset(int $offset): static
public function getOffset(int $offset, int $pageSize): static
{
$options = clone $this->options;
$options->setOffset($offset);
$options->setPageSize($pageSize);
return $this->client->searchMods($options);
}
}
21 changes: 21 additions & 0 deletions tests/Integration/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,27 @@ public function testSearchMods()
}
}

public function testSearchModsLastPage()
{
$options = new ModSearchOptions(static::MINECRAFT_GAME_ID);
$options->setPageSize(50);
$options->setOffset(9_910);

$mods = $this->apiClient->searchMods($options);
$this->assertNotEmpty($mods);
foreach ($mods as $mod) {
$this->assertEquals(static::MINECRAFT_GAME_ID, $mod->getData()->getGameId());
}

$this->assertTrue($mods->hasNextPage());
$mods = $mods->getNextPage();

$this->assertNotEmpty($mods);
$this->assertFalse($mods->hasNextPage());
$this->assertEquals($mods::LIMIT, $mods->getPagination()->getIndex() + $mods->getPagination()->getPageSize());
}


/**
* Test searching mods in a category class
* @return void
Expand Down

0 comments on commit eb253e2

Please sign in to comment.