Skip to content

Commit d2b65a4

Browse files
committed
Refactor Analytics Report from iterator to generator
1 parent 89cc742 commit d2b65a4

File tree

3 files changed

+77
-16
lines changed

3 files changed

+77
-16
lines changed

spec/Analytics/ReportSpec.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,15 @@ public function it_supports_setting_filter(Client $almaClient)
3535
$this->filter->shouldBe('la la la');
3636
}
3737

38-
public function it_can_be_counted(Client $almaClient)
39-
{
40-
$almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
41-
->shouldBeCalledTimes(1)
42-
->willReturn(SpecHelper::getDummyData('analytics_response.xml'));
43-
44-
$this->exists()->shouldReturn(true);
45-
$this->shouldHaveCount(25);
46-
}
47-
4838
public function it_parses_column_headers(Client $almaClient)
4939
{
5040
$almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
5141
->shouldBeCalledTimes(1)
5242
->willReturn(SpecHelper::getDummyData('analytics_response.xml'));
5343

54-
$this->getHeaders()->shouldHaveCount(11);
44+
// $this->getHeaders()->shouldHaveCount(11);
5545
$this->getHeaders()->shouldContain('Event Start Date and Time');
5646

57-
$this->rewind();
58-
$this->valid();
5947
$firstRow = $this->current();
6048
$firstRow->shouldHaveType(Row::class);
6149
$firstRow['Event Start Date and Time']->shouldBe('2017-08-29T15:43:53');
@@ -83,7 +71,7 @@ public function it_supports_resumption(Client $almaClient)
8371
SpecHelper::getDummyData('analytics_response_part3.xml')
8472
);
8573

86-
$this->shouldHaveCount(150 + 150 + 88);
74+
$this->count()->shouldBe(150 + 150 + 88);
8775
}
8876

8977
public function it_might_not_exist(Client $almaClient)

src/Analytics/Report.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
use Scriptotek\Alma\Client;
77
use Scriptotek\Alma\Exception\RequestFailed;
88
use Scriptotek\Alma\Model\LazyResource;
9-
use Scriptotek\Alma\Model\PaginatedList;
9+
use Scriptotek\Alma\Model\PaginatedListGenerator;
1010

1111
/**
1212
* A single Report resource.
1313
*/
1414
class Report extends LazyResource implements \Iterator, \Countable
1515
{
16-
use PaginatedList;
16+
use PaginatedListGenerator;
1717

1818
/** @var Client */
1919
protected $client;

src/Model/PaginatedListGenerator.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Scriptotek\Alma\Model;
4+
5+
trait PaginatedListGenerator
6+
{
7+
/* @var integer */
8+
protected $position = 0;
9+
10+
/**
11+
* Rewind the Iterator to the first element.
12+
*
13+
* @link http://php.net/manual/en/iterator.rewind.php
14+
*
15+
* @return void
16+
*/
17+
public function rewind()
18+
{
19+
throw new \Exception('Cannot rewind a generator that was already run');
20+
}
21+
22+
/**
23+
* Checks if current position is valid.
24+
*
25+
* @link http://php.net/manual/en/iterator.valid.php
26+
*
27+
* @return bool
28+
*/
29+
public function valid()
30+
{
31+
if (!isset($this->resources[0])) {
32+
$this->fetchBatch();
33+
}
34+
35+
return isset($this->resources[0]);
36+
}
37+
38+
/**
39+
* Return the current element.
40+
*
41+
* @link http://php.net/manual/en/iterator.current.php
42+
*
43+
* @return mixed
44+
*/
45+
public function current()
46+
{
47+
return array_shift($this->resources);
48+
}
49+
50+
/**
51+
* Move forward to next element.
52+
*
53+
* @link http://php.net/manual/en/iterator.next.php
54+
*
55+
* @return void
56+
*/
57+
public function next()
58+
{
59+
$this->position++;
60+
}
61+
62+
/**
63+
* Return the key of the current element.
64+
*
65+
* @link http://php.net/manual/en/iterator.key.php
66+
*
67+
* @return int|null Scalar on success, or null on failure.
68+
*/
69+
public function key()
70+
{
71+
return $this->position;
72+
}
73+
}

0 commit comments

Comments
 (0)