Skip to content

Commit

Permalink
FindAll iterator() using $objectNamePlural
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Oct 26, 2023
1 parent 5922fb6 commit aec8905
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/Zendesk/API/Resources/Core/AppInstallations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\ObpStrategy;

/**
* The AppInstallations class exposes methods seen at
Expand Down Expand Up @@ -123,4 +124,8 @@ public function update($id = null, array $updateResourceFields = [], $routeKey =
$updateResourceFields
);
}

private function paginationStrategyClass() {
return ObpStrategy::class;
}
}
4 changes: 0 additions & 4 deletions src/Zendesk/API/Resources/Core/Tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,4 @@ public function merge(array $params = [])

return $response;
}

protected function paginatedPath() {
return 'tickets';
}
}
14 changes: 0 additions & 14 deletions src/Zendesk/API/Resources/Core/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@ class Users extends ResourceAbstract
*/
protected $identities;

/**
* Usage:
* foreach ($usersIterator as $user) {
* process($user)
* }
*
* @return PaginationIterator to fetch all pages.
*/
public function iterator()
{
$strategy = new CbpStrategy($this, 'users', 2);
return new PaginationIterator($strategy);
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Zendesk/API/Traits/Resource/FindAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function findAll(array $params = [], $routeKey = __FUNCTION__)
);
}

// TODO: own trait?
// TODO: own trait
// TODO: page size 100
/**
* Usage:
* foreach ($ticketsIterator as $ticket) {
Expand All @@ -50,7 +51,7 @@ public function findAll(array $params = [], $routeKey = __FUNCTION__)
public function iterator()
{
$strategyClass = $this->paginationStrategyClass();
$strategy = new $strategyClass($this, $this->paginatedPath(), 2);
$strategy = new $strategyClass($this, $this->resourcesRoot(), 2);
return new PaginationIterator($strategy);
}

Expand All @@ -59,8 +60,7 @@ private function paginationStrategyClass() {
return CbpStrategy::class;
}

// TODO: abstract
protected function paginatedPath() {
return "/";
protected function resourcesRoot() {
return $this->objectNamePlural;
}
}
6 changes: 6 additions & 0 deletions src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Zendesk\API\Traits\Utility\Pagination;


/**
* Offset Based Pagination
* Can also be used for no pagination
*/
class ObpStrategy extends PaginationStrategy
{
private $pageNumber = 0;
Expand All @@ -11,6 +16,7 @@ public function getPage()
++$this->pageNumber;
$params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize];
$response = $this->resourcesRoot->findAll($params);

return $response->{$this->resourcesKey};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private function getPageIfNecessary()
return;
}

// TODO: don't keep all pages
$this->page = array_merge($this->page, $this->strategy->getPage());
}

Expand Down

0 comments on commit aec8905

Please sign in to comment.