Skip to content

Commit a951de6

Browse files
committed
Fix pagination handling
1 parent 32f37cc commit a951de6

File tree

6 files changed

+46
-42
lines changed

6 files changed

+46
-42
lines changed

spec/TaskLists/LendingRequestsSpec.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ function it_provides_filtering_options(AlmaClient $client, Library $library)
1919
]);
2020

2121
$client->getJSON('/task-lists/rs/lending-requests?printed=N&status=REQUEST_CREATED_LEND&library=SOME_LIBRARY')
22-
->shouldBeCalled()
22+
->shouldBeCalledTimes(1)
2323
->willReturn(SpecHelper::getDummyData('lending_requests_created.json'));
2424

25-
$result = $this->all();
26-
$result->shouldBeArray();
27-
$result->shouldHaveCount(3);
28-
$result[0]->shouldBeAnInstanceOf(ResourceSharingRequest::class);
25+
$this->all()->shouldBeArray();
26+
$this->all()->shouldHaveCount(3);
27+
$this->all()[0]->shouldBeAnInstanceOf(ResourceSharingRequest::class);
2928
}
3029
}

src/Analytics/Report.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ protected function fetchBatch($attempt = 1, $chunkSize = null)
146146
}
147147
}
148148

149+
protected function fetchData()
150+
{
151+
do {
152+
$this->fetchBatch();
153+
} while (!$this->isFinished);
154+
return null;
155+
}
156+
149157
/**
150158
* Read column headers from response, and check that we got the right number of columns back.
151159
*

src/Model/LazyResource.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function init($data = null)
7171
$data = $this->fetchData();
7272
}
7373

74-
if (!is_null($data) && $this->isInitialized($data)) {
74+
if ($this->isInitialized($data)) {
7575
$this->initialized = true;
7676
}
7777

@@ -84,15 +84,16 @@ public function init($data = null)
8484
}
8585

8686
/**
87-
* Get the model data.
87+
* Get and return the model data.
88+
* @return object
8889
*/
8990
protected function fetchData()
9091
{
9192
return $this->client->getJSON($this->url());
9293
}
9394

9495
/**
95-
* Called when data is available to be processed.
96+
* Called when data is available on the object.
9697
* The resource classes can use this method to process the data.
9798
*
9899
* @param mixed $data

src/Model/LazyResourceList.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
/**
88
* The LazyResourceList extends the LazyResource class with functionality for
9-
* working with lists of resources, such as holdings, items, loans, etc.
9+
* working with non-paginated lists of resources, such as holdings, items, loans, etc.
1010
*/
1111
abstract class LazyResourceList extends LazyResource implements \Countable
1212
{
13+
/* @var integer */
14+
protected $totalRecordCount = null;
15+
1316
/* @var array */
1417
protected $resources = [];
1518

@@ -41,18 +44,24 @@ public function __construct(Client $client, $responseKey)
4144
abstract protected function convertToResource($data);
4245

4346
/**
44-
* Called when data is available to be processed.
47+
* Called when data is available on the object.
48+
* The resource classes can use this method to process the data.
4549
*
46-
* @param mixed $data
50+
* @param $data
4751
*/
48-
public function onData($data)
52+
protected function onData($data)
4953
{
50-
$this->resources = ($data->total_record_count === 0) ? [] : array_map(
51-
function (\stdClass $res) {
52-
return $this->convertToResource($res);
53-
},
54-
$data->{$this->responseKey}
55-
);
54+
if (is_null($this->totalRecordCount)) {
55+
$this->totalRecordCount = $data->total_record_count;
56+
}
57+
58+
if (!isset($data->{$this->responseKey})) {
59+
return;
60+
}
61+
62+
foreach ($data->{$this->responseKey} as $result) {
63+
$this->resources[] = $this->convertToResource($result);
64+
}
5665
}
5766

5867
/**

src/Model/PaginatedList.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ trait PaginatedList
77
/* @var integer */
88
protected $position = 0;
99

10-
/**
11-
* Fetch all the data.
12-
*/
13-
protected function fetchData()
14-
{
15-
return iterator_to_array($this);
16-
}
17-
1810
/**
1911
* Rewind the Iterator to the first element.
2012
*

src/Model/SimplePaginatedList.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ abstract class SimplePaginatedList extends LazyResourceList
2020
/* @var integer */
2121
protected $limit = 10;
2222

23-
/* @var integer */
24-
protected $totalRecordCount = null;
2523

2624
protected function fetchBatch()
2725
{
@@ -33,24 +31,21 @@ protected function fetchBatch()
3331
'offset' => $this->offset,
3432
'limit' => $this->limit,
3533
]));
34+
return $this->onData($response);
35+
}
3636

37-
if (is_null($this->totalRecordCount)) {
38-
$this->totalRecordCount = $response->total_record_count;
39-
}
40-
41-
if ($this->totalRecordCount === 0) {
42-
return;
43-
}
44-
45-
foreach ($response->{$this->responseKey} as $res) {
46-
$this->resources[] = $this->convertToResource($res);
47-
}
48-
$this->offset = count($this->resources);
37+
protected function fetchData()
38+
{
39+
do {
40+
$this->fetchBatch();
41+
} while (!$this->isInitialized($this->resources));
42+
return null;
4943
}
5044

51-
public function fetchData()
45+
protected function onData($data)
5246
{
53-
$this->all();
47+
parent::onData($data);
48+
$this->offset = count($this->resources);
5449
}
5550

5651
/**

0 commit comments

Comments
 (0)