Skip to content

Commit fcd0ae3

Browse files
committed
Use an abstract page-based iterator for iteration
1 parent 96d5d71 commit fcd0ae3

File tree

3 files changed

+106
-57
lines changed

3 files changed

+106
-57
lines changed
Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
11
<?php
22
namespace Intercom\Model;
33

4-
use Guzzle\Service\Resource\ResourceIterator;
5-
use Intercom\Exception\IntercomException;
4+
use Intercom\Model\Iterators\AbstractPageIterator;
65

7-
/**
8-
* Iterate over a get_users command
9-
*/
10-
class GetUsersByFiltersIterator extends ResourceIterator
6+
class GetUsersByFiltersIterator extends AbstractPageIterator
117
{
12-
protected function sendRequest()
13-
{
14-
// Match up the page size on the iterator with the one on the request
15-
if ($this->pageSize) {
16-
$this->command->set('per_page', $this->pageSize);
17-
}
188

19-
// If a next token is set, then add it to the command
20-
if ($this->nextToken) {
21-
$this->command->set('page', $this->nextToken);
22-
}
23-
24-
echo "... Going to the API for more results" . PHP_EOL;
25-
26-
// Execute the command and parse the result
27-
$result = $this->command->execute();
28-
29-
// Parse the next token
30-
$this->nextToken = (isset($result['next_page'])) ? $result['next_page'] : false;
31-
32-
return $result->get('users');
33-
}
34-
35-
public function setPageSize($pageSize)
36-
{
37-
if ($pageSize < 1 || $pageSize > 60) {
38-
throw new IntercomException('Page size on the iterator must be between 1 and 60');
39-
}
40-
41-
return parent::setPageSize($pageSize);
42-
}
439
}
Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
<?php
22
namespace Intercom\Model;
33

4-
use Guzzle\Service\Resource\ResourceIterator;
4+
use Intercom\Model\Iterators\AbstractPageIterator;
55

6-
/**
7-
* Iterate over a get_users command
8-
*/
9-
class GetUsersIterator extends ResourceIterator
6+
class GetUsersIterator extends AbstractPageIterator
107
{
11-
protected function sendRequest()
12-
{
13-
// If a next token is set, then add it to the command
14-
if ($this->nextToken) {
15-
$this->command->set('page', $this->nextToken);
16-
}
178

18-
echo "... Going to the API for more results" . PHP_EOL;
19-
20-
// Execute the command and parse the result
21-
$result = $this->command->execute();
22-
23-
// Parse the next token
24-
$this->nextToken = (isset($result['next_page'])) ? $result['next_page'] : false;
25-
26-
return $result->get('users');
27-
}
289
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
namespace Intercom\Model\Iterators;
3+
4+
use Guzzle\Service\Resource\ResourceIterator;
5+
use Intercom\Exception\IntercomException as IntercomException;
6+
7+
8+
abstract class AbstractPageIterator extends ResourceIterator
9+
{
10+
/** @var int|null The total number of results this request has available */
11+
protected $totalResults;
12+
13+
/** @var string|null The type of object the paginated call returns */
14+
protected $objectListType;
15+
16+
protected function sendRequest()
17+
{
18+
// Match up the page size on the iterator with the one on the command
19+
if ($this->pageSize) {
20+
$this->command->set('per_page', $this->pageSize);
21+
}
22+
23+
// If a next token is set, then add it to the command
24+
if ($this->nextToken) {
25+
$this->command->set('page', $this->nextToken);
26+
}
27+
28+
// Execute the command and parse the result
29+
$result = $this->command->execute();
30+
31+
// Parse the next token
32+
$this->nextToken = (isset($result['next_page'])) ? $result['next_page'] : false;
33+
34+
// Set the total results
35+
$this->totalResults = $result['total_count'];
36+
37+
return $result->get($this->getObjectKeyFromListType($result['type']));
38+
}
39+
40+
/**
41+
* Sets the total number of results available from the API for this request
42+
*
43+
* @param int $totalResults
44+
*/
45+
protected function setTotalResults($totalResults)
46+
{
47+
$this->totalResults = $totalResults;
48+
}
49+
50+
/**
51+
* Gets the total results available from the API for this request
52+
*
53+
* @return null|int The total results
54+
*/
55+
public function getTotalResults()
56+
{
57+
return $this->totalResults;
58+
}
59+
60+
/**
61+
* Sets the number of results per page
62+
*
63+
* @param int $pageSize The requested page size
64+
* @return $this
65+
* @throws IntercomException If the page size is not between 1 and 60
66+
*/
67+
public function setPageSize($pageSize)
68+
{
69+
if ($pageSize < 1 || $pageSize > 60) {
70+
throw new IntercomException('Page size on the iterator must be between 1 and 60');
71+
}
72+
73+
return parent::setPageSize($pageSize);
74+
}
75+
76+
/**
77+
* Gets the name of the key that contains the results in a paginated API response
78+
*
79+
* @param string $type The returned type
80+
* @return string
81+
* @throws IntercomException If the $type is unknown
82+
*/
83+
protected function getObjectKeyFromListType($type)
84+
{
85+
switch ($type) {
86+
case 'company.list':
87+
return 'companies';
88+
case 'conversation.list':
89+
return 'conversations';
90+
case 'conversation.part.list':
91+
return 'parts';
92+
case 'segment.list':
93+
return 'segments';
94+
case 'tag.list':
95+
return 'tags';
96+
case 'user.list':
97+
return 'users';
98+
default:
99+
throw new IntercomException("Unknown list type returned ({$type}). Unable to use iterator to paginate");
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)