Skip to content

Commit

Permalink
Retrieving the next list items based on nextpagetoken flag
Browse files Browse the repository at this point in the history
  • Loading branch information
shishir-intelli committed Jan 29, 2024
1 parent 189c2dc commit 59e9354
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions src/Api/ApigeeX/Controller/PaginationHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Utility methods for those controllers that supports paginated listing.
*
* @see \Apigee\Edge\Api\ApigeeX\Controller\PaginatedEntityListingControllerInterface
* @see \PaginatedEntityListingControllerInterface
*/
trait PaginationHelperTrait
{
Expand All @@ -35,7 +35,7 @@ trait PaginationHelperTrait
/**
* {@inheritdoc}
*/
public function createPager(int $limit = 0, string $pageToken = null): PagerInterface
public function createPager(int $limit = 0, ?string $pageToken = null): PagerInterface
{
// Create an anonymous class here because this class should not exist and be in use
// in those controllers that do not work with entities that belongs to an organization.
Expand Down Expand Up @@ -90,7 +90,7 @@ public function setLimit(int $limit): int
/**
* Loads paginated list of entities from Apigee X.
*
* @param \Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null $pager
* @param \Structure\PagerInterface|null $pager

Check failure on line 93 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.1, )

UndefinedDocblockClass

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:93:15: UndefinedDocblockClass: Docblock-defined class, interface or enum named Structure\PagerInterface does not exist (see https://psalm.dev/200)

Check failure on line 93 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.1, )

MismatchingDocblockParamType

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:93:15: MismatchingDocblockParamType: Parameter $pager has wrong type 'Structure\PagerInterface|null', should be 'Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null' (see https://psalm.dev/141)

Check failure on line 93 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.2, )

UndefinedDocblockClass

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:93:15: UndefinedDocblockClass: Docblock-defined class, interface or enum named Structure\PagerInterface does not exist (see https://psalm.dev/200)

Check failure on line 93 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.2, )

MismatchingDocblockParamType

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:93:15: MismatchingDocblockParamType: Parameter $pager has wrong type 'Structure\PagerInterface|null', should be 'Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null' (see https://psalm.dev/141)
* Pager.
* @param array $query_params
* Additional query parameters.
Expand All @@ -103,7 +103,7 @@ public function setLimit(int $limit): int
* @psalm-suppress PossiblyNullArrayOffset $tmp->id() is always not null here.
* @psalm-suppress PossiblyFalseArgument $tmp not be false.
*/
protected function listEntities(PagerInterface $pager = null, array $query_params = [], string $key_provider = 'id'): array
protected function listEntities(?PagerInterface $pager = null, array $query_params = [], string $key_provider = 'id'): array

Check failure on line 106 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.1, )

ImplementedParamTypeMismatch

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:106:53: ImplementedParamTypeMismatch: Argument 1 of Apigee\Edge\Api\ApigeeX\Controller\AppByOwnerController::listEntities has wrong type 'Structure\PagerInterface|null', expecting 'Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null' as defined by Apigee\Edge\Api\ApigeeX\Controller\PaginatedEntityListingControllerTrait::listEntities (see https://psalm.dev/199)

Check failure on line 106 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.1, )

ImplementedParamTypeMismatch

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:106:53: ImplementedParamTypeMismatch: Argument 1 of Apigee\Edge\Api\ApigeeX\Controller\PaginationHelperTrait::listEntities has wrong type 'Structure\PagerInterface|null', expecting 'Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null' as defined by Apigee\Edge\Api\ApigeeX\Controller\PaginatedEntityListingControllerTrait::listEntities (see https://psalm.dev/199)

Check failure on line 106 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.1, )

ImplementedParamTypeMismatch

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:106:53: ImplementedParamTypeMismatch: Argument 1 of Apigee\Edge\Api\ApigeeX\Controller\AppGroupController::listEntities has wrong type 'Structure\PagerInterface|null', expecting 'Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null' as defined by Apigee\Edge\Api\ApigeeX\Controller\PaginatedEntityListingControllerTrait::listEntities (see https://psalm.dev/199)

Check failure on line 106 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.2, )

ImplementedParamTypeMismatch

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:106:53: ImplementedParamTypeMismatch: Argument 1 of Apigee\Edge\Api\ApigeeX\Controller\AppByOwnerController::listEntities has wrong type 'Structure\PagerInterface|null', expecting 'Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null' as defined by Apigee\Edge\Api\ApigeeX\Controller\PaginatedEntityListingControllerTrait::listEntities (see https://psalm.dev/199)

Check failure on line 106 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.2, )

ImplementedParamTypeMismatch

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:106:53: ImplementedParamTypeMismatch: Argument 1 of Apigee\Edge\Api\ApigeeX\Controller\PaginationHelperTrait::listEntities has wrong type 'Structure\PagerInterface|null', expecting 'Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null' as defined by Apigee\Edge\Api\ApigeeX\Controller\PaginatedEntityListingControllerTrait::listEntities (see https://psalm.dev/199)

Check failure on line 106 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.2, )

ImplementedParamTypeMismatch

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:106:53: ImplementedParamTypeMismatch: Argument 1 of Apigee\Edge\Api\ApigeeX\Controller\AppGroupController::listEntities has wrong type 'Structure\PagerInterface|null', expecting 'Apigee\Edge\Api\ApigeeX\Structure\PagerInterface|null' as defined by Apigee\Edge\Api\ApigeeX\Controller\PaginatedEntityListingControllerTrait::listEntities (see https://psalm.dev/199)
{
if ($pager) {
$responseArray = $this->getResultsInRange($pager, $query_params);
Expand All @@ -115,6 +115,8 @@ protected function listEntities(PagerInterface $pager = null, array $query_param
} else {
// Pass an empty pager to load all entities.
$responseArray = $this->getResultsInRange($this->createPager(), $query_params);

Check failure on line 117 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.1, )

InvalidArgument

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:117:55: InvalidArgument: Argument 1 of Apigee\Edge\Api\ApigeeX\Controller\AppGroupController::getResultsInRange expects Structure\PagerInterface, but Apigee\Edge\Api\ApigeeX\Structure\PagerInterface provided (see https://psalm.dev/004)

Check failure on line 117 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.2, )

InvalidArgument

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:117:55: InvalidArgument: Argument 1 of Apigee\Edge\Api\ApigeeX\Controller\AppGroupController::getResultsInRange expects Structure\PagerInterface, but Apigee\Edge\Api\ApigeeX\Structure\PagerInterface provided (see https://psalm.dev/004)
// Check flag 'nextPageToken' to get next items from the list.
$nextPageToken = array_key_exists('nextPageToken', $responseArray) ? $responseArray['nextPageToken'] : false;
// Ignore entity type key from response, ex.: developer, apiproduct,
// etc.
$responseArray = reset($responseArray);
Expand All @@ -123,30 +125,25 @@ protected function listEntities(PagerInterface $pager = null, array $query_param
return [];
}
$entities = $this->responseArrayToArrayOfEntities($responseArray, $key_provider);
$lastEntity = end($entities);
$lastId = $lastEntity->{$key_provider}();
do {
$tmp = $this->getResultsInRange($this->createPager(0, $lastId), $query_params);
// Ignore entity type key from response, ex.: developer,
// apiproduct, etc.
$tmp = reset($tmp);
// Remove the first item from the list because it is the same
// as the last item of $entities at this moment.
// Apigee X response always starts with the requested entity
// (pageToken).
array_shift($tmp);
$tmpEntities = $this->responseArrayToArrayOfEntities($tmp, $key_provider);

if (count($tmpEntities) > 0) {
if ($nextPageToken) {

Check failure on line 128 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.1, )

RiskyTruthyFalsyComparison

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:128:17: RiskyTruthyFalsyComparison: Operand of type false|mixed contains type mixed, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

Check failure on line 128 in src/Api/ApigeeX/Controller/PaginationHelperTrait.php

View workflow job for this annotation

GitHub Actions / test_and_lint (8.2, )

RiskyTruthyFalsyComparison

src/Api/ApigeeX/Controller/PaginationHelperTrait.php:128:17: RiskyTruthyFalsyComparison: Operand of type false|mixed contains type mixed, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
do {
$tmp = $this->getResultsInRange($this->createPager(0, $nextPageToken), $query_params);
// Check the flag 'nextPageToken' to get next items from the list.
$nextPageToken = array_key_exists('nextPageToken', $tmp) ? $tmp['nextPageToken'] : false;
// Ignore entity type key from response, ex.: developer,
// apiproduct, etc.
$tmp = reset($tmp);
// Remove the first item from the list because it is the same
// as the last item of $entities at this moment.
// Apigee X response always starts with the requested entity
// (pageToken).
array_shift($tmp);
$tmpEntities = $this->responseArrayToArrayOfEntities($tmp, $key_provider);
// The returned entity array is keyed by entity id which
// is unique so we can do this.
$entities += $tmpEntities;
$lastEntity = end($tmpEntities);
$lastId = $lastEntity->{$key_provider}();
} else {
$lastId = false;
}
} while ($lastId);
} while ($nextPageToken);
}

return $entities;
}
Expand All @@ -155,7 +152,7 @@ protected function listEntities(PagerInterface $pager = null, array $query_param
/**
* Gets entities and entity ids in a provided range from Apigee X.
*
* @param \Apigee\Edge\Api\ApigeeX\Structure\PagerInterface $pager
* @param \Structure\PagerInterface $pager
* limit object with configured pageToken and limit.
* @param array $query_params
* Query parameters for the API call.
Expand Down

0 comments on commit 59e9354

Please sign in to comment.