Skip to content

Commit

Permalink
Refactor extract CbpIterator getPageIfNecessary()
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Oct 18, 2023
1 parent c5016b9 commit 238b314
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/Zendesk/API/Traits/Utility/CbpIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class CbpIterator implements Iterator
*/
public const DEFAULT_PAGE_SIZE = 100;


/**
* @var string The root key in the response with the page resources (eg: users, tickets).
*/
Expand Down Expand Up @@ -62,17 +61,6 @@ public function __construct($resourcesRoot, $resourcesKey, $pageSize = self::DEF
$this->pageSize = $pageSize;
}

/**
* @return mixed (using FindAll) The current resource, maybe fetching a new page.
*/
public function current()
{
if ($this->isEndOfPage()) {
$this->getPage();
}
return $this->page[$this->position];
}

/**
* @return int The current position.
*/
Expand Down Expand Up @@ -102,17 +90,28 @@ public function rewind()
*/
public function valid()
{
if ($this->isEndOfPage()) {
$this->getPage();
}
$this->getPageIfNecessary();
return isset($this->page[$this->position]);
}

/**
* @return mixed (using FindAll) The current resource, maybe fetching a new page.
*/
public function current()
{
$this->getPageIfNecessary();
return $this->page[$this->position];
}

/**
* Fetches the next page of resources from the API.
*/
private function getPage()
private function getPageIfNecessary()
{
if (!$this->isEndOfPage()) {
return;
}

$this->started = true;
$params = ['page[size]' => $this->pageSize];
if ($this->afterCursor) {
Expand Down

0 comments on commit 238b314

Please sign in to comment.