-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Red 1953 pagination #513
Merged
Merged
Red 1953 pagination #513
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1f32c17
Extract TicketsIterator isEndOfPage()
ecoologic f4c4ee5
Rename TicketsIterator $resources $resourcesRoot
ecoologic 408ca26
Rename TicketsIterator tickets to resources
ecoologic 04cfebf
Rename TicketsIterator.php to CbpIterator.php
ecoologic c5016b9
Add Users iterator()
ecoologic 238b314
Refactor extract CbpIterator getPageIfNecessary()
ecoologic 73457ea
Add Zendesk\API\Traits\Utility\Pagination
ecoologic c65f626
ObpStrategy
ecoologic 5922fb6
Add FindAll iterator()
ecoologic aec8905
FindAll iterator() using $objectNamePlural
ecoologic 3191160
SinglePageStrategy
ecoologic 5994a59
Add AutomationsTest testIterator
ecoologic 3ef701d
add Traits/Resource/Pagination.php
ecoologic 8aad528
Rename Traits/Utility/Pagination/PaginationStrategy.php to AbstractSt…
ecoologic fdd8bd1
Reuse AbstractStrategy::DEFAULT_PAGE_SIZE
ecoologic 3bd8123
Rename trait Pagination resourcesRoot clientResources
ecoologic 72737d8
Rename trait Pagination resourcesRoot to clientList
ecoologic 48ac3f6
Add testIterator to resources
ecoologic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use Zendesk\API\Traits\Resource\Find; | ||
use Zendesk\API\Traits\Resource\FindAll; | ||
use Zendesk\API\Traits\Resource\Update; | ||
use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy; | ||
|
||
/** | ||
* The AppInstallations class exposes methods seen at | ||
|
@@ -123,4 +124,8 @@ public function update($id = null, array $updateResourceFields = [], $routeKey = | |
$updateResourceFields | ||
); | ||
} | ||
|
||
private function paginationStrategyClass() { | ||
return SinglePageStrategy::class; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const are not available in traits in the current PHP version (7.4) |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
use Zendesk\API\Traits\Resource\FindMany; | ||
use Zendesk\API\Traits\Resource\UpdateMany; | ||
use Zendesk\API\Traits\Utility\InstantiatorTrait; | ||
use Zendesk\API\Traits\Utility\TicketsIterator; | ||
|
||
/** | ||
* The Tickets class exposes key methods for reading and updating ticket data | ||
|
@@ -45,19 +44,6 @@ class Tickets extends ResourceAbstract | |
*/ | ||
protected $lastAttachments = []; | ||
|
||
/** | ||
* Usage: | ||
* foreach ($ticketsIterator as $ticket) { | ||
* process($ticket) | ||
* } | ||
* | ||
* @return TicketsIterator Returns a new TicketsIterator object. | ||
*/ | ||
public function iterator() | ||
{ | ||
return new TicketsIterator($this); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved into trait to make it generic to all resources. |
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Zendesk\API\Traits\Resource; | ||
|
||
use Zendesk\API\Traits\Utility\Pagination\AbstractStrategy; | ||
use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; | ||
use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; | ||
|
||
trait Pagination { | ||
|
||
/** | ||
* Usage: | ||
* foreach ($ticketsIterator as $ticket) { | ||
* process($ticket) | ||
* } | ||
* | ||
* @return PaginationIterator to fetch all pages. | ||
*/ | ||
public function iterator() | ||
{ | ||
$strategyClass = $this->paginationStrategyClass(); | ||
$strategy = new $strategyClass($this, $this->resourcesKey(), AbstractStrategy::DEFAULT_PAGE_SIZE); | ||
return new PaginationIterator($strategy); | ||
} | ||
|
||
protected function paginationStrategyClass() { | ||
return CbpStrategy::class; | ||
} | ||
|
||
/* | ||
* @return string eg: "job_statuses" | ||
*/ | ||
protected function resourcesKey() { | ||
return $this->objectNamePlural; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
namespace Zendesk\API\Traits\Utility\Pagination; | ||
|
||
abstract class AbstractStrategy | ||
{ | ||
public const DEFAULT_PAGE_SIZE = 100; | ||
|
||
/* | ||
* @var mixed use trait FindAll. The object handling the list, Ie: `$client->{clientList}()` | ||
*/ | ||
protected $clientList; | ||
|
||
/* | ||
* @var string The response key where the data is returned | ||
*/ | ||
protected $resourcesKey; | ||
protected $pageSize; | ||
|
||
public function __construct($clientList, $resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) | ||
{ | ||
$this->clientList = $clientList; | ||
$this->resourcesKey = $resourcesKey; | ||
$this->pageSize = $pageSize; | ||
} | ||
|
||
abstract public function getPage(); | ||
abstract public function shouldGetPage($position); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Zendesk\API\Traits\Utility\Pagination; | ||
|
||
class CbpStrategy extends AbstractStrategy | ||
{ | ||
private $afterCursor = null; | ||
private $started = false; | ||
|
||
public function getPage() | ||
{ | ||
$this->started = true; | ||
$params = ['page[size]' => $this->pageSize]; | ||
if ($this->afterCursor) { | ||
$params['page[after]'] = $this->afterCursor; | ||
} | ||
$response = $this->clientList->findAll($params); | ||
|
||
$this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; | ||
return $response->{$this->resourcesKey}; | ||
} | ||
|
||
public function shouldGetPage($position) { | ||
return !$this->started || $this->afterCursor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Zendesk\API\Traits\Utility\Pagination; | ||
|
||
|
||
/** | ||
* Offset Based Pagination | ||
*/ | ||
class ObpStrategy extends AbstractStrategy | ||
{ | ||
private $pageNumber = 0; | ||
|
||
public function getPage() | ||
{ | ||
++$this->pageNumber; | ||
$params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; | ||
$response = $this->clientList->findAll($params); | ||
|
||
return $response->{$this->resourcesKey}; | ||
} | ||
|
||
public function shouldGetPage($position) { | ||
return $this->pageNumber == 0 || $position >= $this->pageNumber * $this->pageSize; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Zendesk\API\Traits\Utility\Pagination; | ||
|
||
use Iterator; | ||
|
||
class PaginationIterator implements Iterator | ||
{ | ||
private $position = 0; | ||
private $page = []; | ||
private $strategy; | ||
|
||
public function __construct(AbstractStrategy $strategy) | ||
{ | ||
$this->strategy = $strategy; | ||
} | ||
|
||
public function key() | ||
{ | ||
return $this->position; | ||
} | ||
|
||
public function next() | ||
{ | ||
++$this->position; | ||
} | ||
|
||
public function rewind() | ||
{ | ||
$this->position = 0; | ||
} | ||
|
||
public function valid() | ||
{ | ||
$this->getPageIfNecessary(); | ||
return !!$this->current(); | ||
} | ||
|
||
public function current() | ||
{ | ||
if (isset($this->page[$this->position])) { | ||
return $this->page[$this->position]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private function getPageIfNecessary() | ||
{ | ||
if (!$this->strategy->shouldGetPage($this->position)) { | ||
return; | ||
} | ||
|
||
$this->page = array_merge($this->page, $this->strategy->getPage()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Zendesk\API\Traits\Utility\Pagination; | ||
|
||
|
||
/** | ||
* Single Page (no pagination) | ||
*/ | ||
class SinglePageStrategy extends AbstractStrategy | ||
{ | ||
protected $started = false; | ||
|
||
public function getPage() | ||
{ | ||
$this->started = true; | ||
$response = $this->clientList->findAll(); | ||
|
||
return $response->{$this->resourcesKey}; | ||
} | ||
|
||
public function shouldGetPage($position) { | ||
return !$this->started; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do some cleaning at the end, but these comments come useful to switch between tests.