Skip to content
Commits on Source (23)
This diff is collapsed.
......@@ -6,6 +6,7 @@ namespace Dbp\Relay\BasePersonBundle\API;
use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use Dbp\Relay\CoreBundle\Pagination\Paginator;
interface PersonProviderInterface
{
......@@ -16,10 +17,8 @@ interface PersonProviderInterface
* * LocalData::QUERY_PARAMETER_NAME
*
* @throws ApiError
*
* @return Person[]
*/
public function getPersons(array $options): array;
public function getPersons(array $options): Paginator;
/**
* Throws an HTTP_NOT_FOUND exception if no person with the given ID can be found.
......
......@@ -8,19 +8,21 @@ use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use Dbp\Relay\BasePersonBundle\API\PersonProviderInterface;
use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Helpers\ArrayFullPaginator;
use Dbp\Relay\CoreBundle\LocalData\LocalData;
use Dbp\Relay\CoreBundle\Pagination\Pagination;
use Dbp\Relay\CoreBundle\Pagination\Paginator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
final class PersonCollectionDataProvider extends AbstractController implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
public const ITEMS_PER_PAGE = 250;
public const MAX_NUM_ITEMS_PER_PAGE_DEFAULT = 50;
private $api;
/** @var PersonProviderInterface */
private $personProvider;
public function __construct(PersonProviderInterface $api)
public function __construct(PersonProviderInterface $personProvider)
{
$this->api = $api;
$this->personProvider = $personProvider;
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
......@@ -28,12 +30,10 @@ final class PersonCollectionDataProvider extends AbstractController implements C
return Person::class === $resourceClass;
}
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): ArrayFullPaginator
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): Paginator
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$perPage = self::ITEMS_PER_PAGE;
$page = 1;
$filters = $context['filters'] ?? [];
$options = [];
......@@ -42,18 +42,8 @@ final class PersonCollectionDataProvider extends AbstractController implements C
}
LocalData::addOptions($options, $filters);
Pagination::addOptions($options, $filters, self::MAX_NUM_ITEMS_PER_PAGE_DEFAULT);
$persons = $this->api->getPersons($filters);
if (isset($filters['page'])) {
$page = (int) $filters['page'];
}
if (isset($filters['perPage'])) {
$perPage = (int) $filters['perPage'];
}
// TODO: do pagination via API
return new ArrayFullPaginator($persons, $page, $perPage);
return $this->personProvider->getPersons($options);
}
}
......@@ -19,6 +19,7 @@ use Dbp\Relay\CoreBundle\LocalData\LocalDataAwareTrait;
* "parameters" = {
* {"name" = "includeLocal", "in" = "query", "description" = "Local data attributes to include", "type" = "string"},
* {"name" = "queryLocal", "in" = "query", "description" = "Local query parameters to apply", "type" = "string"},
* {"name" = "partialPagination", "in" = "query", "description" = "Partial pagination", "type" = "bool", "example" = "false"}
* }
* }
* },
......
......@@ -6,6 +6,9 @@ namespace Dbp\Relay\BasePersonBundle\Service;
use Dbp\Relay\BasePersonBundle\API\PersonProviderInterface;
use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Pagination\FullPaginator;
use Dbp\Relay\CoreBundle\Pagination\Pagination;
use Dbp\Relay\CoreBundle\Pagination\Paginator;
class DummyPersonProvider implements PersonProviderInterface
{
......@@ -19,14 +22,15 @@ class DummyPersonProvider implements PersonProviderInterface
$this->currentIdentifier = null;
}
public function getPersons(array $options): array
public function getPersons(array $options): Paginator
{
$person = $this->getCurrentPerson();
if ($person !== null) {
return [$person];
$persons = [];
$currentPerson = $this->getCurrentPerson();
if ($currentPerson !== null) {
$persons[] = $currentPerson;
}
return [];
return new FullPaginator($persons, 1, Pagination::MAX_NUM_ITEMS_PER_PAGE_DEFAULT, count($persons));
}
public function getPerson(string $id, array $options = []): Person
......
......@@ -6,6 +6,9 @@ namespace Dbp\Relay\BasePersonBundle\TestUtils;
use Dbp\Relay\BasePersonBundle\API\PersonProviderInterface;
use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Pagination\FullPaginator;
use Dbp\Relay\CoreBundle\Pagination\Pagination;
use Dbp\Relay\CoreBundle\Pagination\Paginator;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DummyPersonProvider implements PersonProviderInterface
......@@ -18,9 +21,11 @@ class DummyPersonProvider implements PersonProviderInterface
$this->person = $person;
}
public function getPersons(array $options): array
public function getPersons(array $options): Paginator
{
return [$this->person];
$persons = [$this->person];
return new FullPaginator($persons, 1, Pagination::MAX_NUM_ITEMS_PER_PAGE_DEFAULT, count($persons));
}
public function getPerson(string $id, array $options = []): Person
......