Skip to content

Commit c8f5738

Browse files
committed
fixed CS
Updated coding standard to PSR-2 fixed CS issues and typos updated docs changed fetch() and fetchAll() signature to an array of parameters for flexibility in the future
1 parent 99ed0c7 commit c8f5738

File tree

6 files changed

+90
-57
lines changed

6 files changed

+90
-57
lines changed

doc/result_pager.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ $client = new Github\Client();
1010

1111
$organizationApi = $client->api('organization');
1212

13-
$paginator = new Github\ResultPager( $client );
14-
$result = $paginator->fetchAll( $organizationApi, 'repositories', 'github' );
13+
$paginator = new Github\ResultPager($client);
14+
$parameters = array('github');
15+
$result = $paginator->fetchAll($organizationApi, 'repositories', $parameters);
1516
```
1617

1718
Get the first page
@@ -20,9 +21,11 @@ $client = new Github\Client();
2021

2122
$organizationApi = $client->api('organization');
2223

23-
$paginator = new Github\ResultPager( $client );
24-
$result = $paginator->fetch( $organizationApi, 'repositories', 'github' );
24+
$paginator = new Github\ResultPager( $client );
25+
$parameters = array('github');
26+
$result = $paginator->fetch($organizationApi, 'repositories', $parameters);
2527
```
28+
2629
Check for a next page:
2730
```php
2831
$paginator->hasNext();
@@ -41,4 +44,9 @@ $paginator->hasPrevious();
4144
Get prevrious page:
4245
```php
4346
$paginator->fetchPrevious();
44-
```
47+
```
48+
49+
If you want to retrieve the pagination links (available after the call to fetch):
50+
```php
51+
$paginator->getPagination();
52+
```

lib/Github/Api/AbstractApi.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ abstract class AbstractApi implements ApiInterface
2121
/**
2222
* number of items per page (GitHub pagination)
2323
*
24-
* @var int
24+
* @var null|int
2525
*/
26-
protected $perPage = null;
26+
protected $perPage;
2727

2828
/**
2929
* @param Client $client
@@ -38,19 +38,19 @@ public function configure()
3838
}
3939

4040
/**
41-
* @return int|null
41+
* @return null|int
4242
*/
4343
public function getPerPage()
4444
{
4545
return $this->perPage;
4646
}
4747

4848
/**
49-
* @param Client $client
49+
* @param null|int $perPage
5050
*/
5151
public function setPerPage($perPage)
5252
{
53-
$this->perPage = (int) $perPage;
53+
$this->perPage = (null === $perPage ? $perPage : (int) $perPage);
5454
return $this;
5555
}
5656

lib/Github/HttpClient/HttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function put($path, array $parameters = array(), array $headers = array()
163163
*/
164164
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
165165
{
166-
if ( !empty($this->options['base_url']) AND 0 !== strpos( $path, $this->options['base_url'] )) {
166+
if (!empty($this->options['base_url']) && 0 !== strpos($path, $this->options['base_url'])) {
167167
$path = trim($this->options['base_url'].$path, '/');
168168
}
169169

lib/Github/ResultPager.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@ class ResultPager implements ResultPagerInterface
2424
* @var array pagination
2525
* Comes from pagination headers in Github API results
2626
*/
27-
protected $pagination = null;
27+
protected $pagination;
2828

29-
public function __construct( Client $client )
29+
30+
/**
31+
* The Github client to use for pagination. This must be the same
32+
* instance that you got the Api instance from, i.e.:
33+
*
34+
* $client = new \Github\Client();
35+
* $api = $client->api('someApi');
36+
* $pager = new \Github\ResultPager($client);
37+
*
38+
* @param \Github\Client $client
39+
*
40+
*/
41+
public function __construct(Client $client)
3042
{
3143
$this->client = $client;
3244
}
3345

3446
/**
35-
* @return null|array pagination result of last request
47+
* {@inheritdoc}
3648
*/
3749
public function getPagination()
3850
{
@@ -42,10 +54,8 @@ public function getPagination()
4254
/**
4355
* {@inheritdoc}
4456
*/
45-
public function fetch( ApiInterface $api, $method )
57+
public function fetch(ApiInterface $api, $method, array $parameters = array())
4658
{
47-
$parameters = array_slice(func_get_args(),2);
48-
4959
$result = call_user_func_array(array($api, $method), $parameters);
5060
$this->postFetch();
5161

@@ -55,9 +65,10 @@ public function fetch( ApiInterface $api, $method )
5565
/**
5666
* {@inheritdoc}
5767
*/
58-
public function fetchAll( ApiInterface $api, $method )
68+
public function fetchAll(ApiInterface $api, $method, array $parameters = array())
5969
{
60-
$parameters = array_slice(func_get_args(),2);
70+
// get the perPage from the api
71+
$perPage = $api->getPerPage();
6172

6273
// Set parameters per_page to GitHub max to minimize number of requests
6374
$api->setPerPage(100);
@@ -70,6 +81,9 @@ public function fetchAll( ApiInterface $api, $method )
7081
$result = array_merge($result, $this->fetchNext());
7182
}
7283

84+
// restore the perPage
85+
$api->setPerPage($perPage);
86+
7387
return $result;
7488
}
7589

@@ -134,24 +148,19 @@ public function fetchLast()
134148
*/
135149
protected function has($key)
136150
{
137-
if (!empty($this->pagination) and isset($this->pagination[$key])) {
138-
return true;
139-
}
140-
141-
return false;
151+
return !empty($this->pagination) && isset($this->pagination[$key]);
142152
}
143153

144154
/**
145155
* {@inheritdoc}
146156
*/
147157
protected function get($key)
148158
{
149-
if ( $this->has($key) ) {
159+
if ($this->has($key)) {
150160
$result = $this->client->getHttpClient()->get($this->pagination[$key]);
151161
$this->postFetch();
152162

153163
return $result->getContent();
154164
}
155165
}
156-
157166
}

lib/Github/ResultPagerInterface.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,34 @@
1212
*/
1313
interface ResultPagerInterface
1414
{
15+
16+
/**
17+
* @return null|array pagination result of last request
18+
*/
19+
public function getPagination();
20+
1521
/**
1622
* Fetch a single result (page) from an api call
23+
*
24+
* @param ApiInterface $api the Api instance
25+
* @param string $method the method name to call on the Api instance
26+
* @param array $parameters the method parameters in an array
27+
*
28+
* @return array returns the result of the Api::$method() call
1729
*/
18-
public function fetch( ApiInterface $api, $method );
30+
public function fetch(ApiInterface $api, $method, array $parameters = array());
1931

2032
/**
2133
* Fetch all results (pages) from an api call
2234
* Use with care - there is no maximum
35+
*
36+
* @param ApiInterface $api the Api instance
37+
* @param string $method the method name to call on the Api instance
38+
* @param array $parameters the method parameters in an array
39+
*
40+
* @return array returns a merge of the results of the Api::$method() call
2341
*/
24-
public function fetchAll( ApiInterface $api, $method );
42+
public function fetchAll(ApiInterface $api, $method, array $parameters = array());
2543

2644
/**
2745
* Method that performs the actual work to refresh the pagination property

test/Github/Tests/ResultPagerTest.php

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function shouldGetAllResults()
2525
{
2626
$amountLoops = 3;
2727
$content = array(1,2,3,4,5,6,7,8,9,10);
28-
$responseMock = new TestResponse( $amountLoops, $content );
28+
$responseMock = new TestResponse($amountLoops, $content);
2929

3030
// httpClient mock
3131
$httpClientMock = $this->getHttpClientMock($responseMock);
@@ -37,7 +37,7 @@ public function shouldGetAllResults()
3737
$clientMock = $this->getClientMock($httpClientMock);
3838

3939
// memberApi Mock
40-
$memberApiMock = $this->getApiMock( 'Github\Api\Organization\Members' );
40+
$memberApiMock = $this->getApiMock('Github\Api\Organization\Members');
4141
$memberApiMock
4242
->expects($this->once())
4343
->method('all')
@@ -47,8 +47,8 @@ public function shouldGetAllResults()
4747
$parameters = array('netwerven');
4848

4949
// Run fetchAll on result paginator
50-
$paginator = new Github\ResultPager( $clientMock );
51-
$result = $paginator->fetchAll( $memberApiMock, $method, $parameters );
50+
$paginator = new Github\ResultPager($clientMock);
51+
$result = $paginator->fetchAll($memberApiMock, $method, $parameters);
5252

5353
$this->assertEquals($amountLoops * count($content), count($result));
5454
}
@@ -64,19 +64,19 @@ public function shouldGetSomeResults()
6464
$resultContent = 'organization test';
6565

6666
$responseMock = $this->getResponseMock($pagination);
67-
$httpClient = $this->getHttpClientMock( $responseMock );
67+
$httpClient = $this->getHttpClientMock($responseMock);
6868
$client = $this->getClientMock($httpClient);
6969

70-
$organizationApiMock = $this->getApiMock( 'Github\Api\Organization' );
70+
$organizationApiMock = $this->getApiMock('Github\Api\Organization');
7171

7272
$organizationApiMock
7373
->expects($this->once())
7474
->method('show')
7575
->with('github')
7676
->will($this->returnValue($resultContent));
7777

78-
$paginator = new Github\ResultPager( $client );
79-
$result = $paginator->fetch($organizationApiMock, 'show', 'github');
78+
$paginator = new Github\ResultPager($client);
79+
$result = $paginator->fetch($organizationApiMock, 'show', array('github'));
8080

8181
$this->assertEquals($resultContent, $result);
8282
$this->assertEquals($pagination, $paginator->getPagination());
@@ -103,10 +103,10 @@ public function postFetch()
103103
->method('getPagination')
104104
->will($this->returnValue($pagination));
105105

106-
$httpClient = $this->getHttpClientMock( $responseMock );
106+
$httpClient = $this->getHttpClientMock($responseMock);
107107
$client = $this->getClientMock($httpClient);
108108

109-
$paginator = new Github\ResultPager( $client );
109+
$paginator = new Github\ResultPager($client);
110110
$paginator->postFetch();
111111

112112
$this->assertEquals($paginator->getPagination(), $pagination);
@@ -122,7 +122,7 @@ public function fetchNext()
122122
$pagination = array('next' => 'http://github.com/next');
123123
$resultContent = 'fetch test';
124124

125-
$responseMock = $this->getResponseMock( $pagination );
125+
$responseMock = $this->getResponseMock($pagination);
126126
$responseMock
127127
->expects($this->once())
128128
->method('getContent')
@@ -132,17 +132,17 @@ public function fetchNext()
132132
->expects($this->exactly(2))
133133
->method('getPagination');
134134

135-
$httpClient = $this->getHttpClientMock( $responseMock );
135+
$httpClient = $this->getHttpClientMock($responseMock);
136136

137137
$httpClient
138138
->expects($this->once())
139139
->method('get')
140-
->with( $pagination['next'] )
140+
->with($pagination['next'])
141141
->will($this->returnValue($responseMock));
142142

143143
$client = $this->getClientMock($httpClient);
144144

145-
$paginator = new Github\ResultPager( $client );
145+
$paginator = new Github\ResultPager($client);
146146
$paginator->postFetch();
147147

148148
$this->assertEquals($paginator->fetchNext(), $resultContent);
@@ -156,10 +156,10 @@ public function fetchNext()
156156
public function shouldHaveNext()
157157
{
158158
$responseMock = $this->getResponseMock(array('next' => 'http://github.com/next'));
159-
$httpClient = $this->getHttpClientMock( $responseMock );
159+
$httpClient = $this->getHttpClientMock($responseMock);
160160
$client = $this->getClientMock($httpClient);
161161

162-
$paginator = new Github\ResultPager( $client );
162+
$paginator = new Github\ResultPager($client);
163163
$paginator->postFetch();
164164

165165
$this->assertEquals($paginator->hasNext(), true);
@@ -174,34 +174,32 @@ public function shouldHaveNext()
174174
public function shouldHavePrevious()
175175
{
176176
$responseMock = $this->getResponseMock(array('prev' => 'http://github.com/previous'));
177-
$httpClient = $this->getHttpClientMock( $responseMock );
177+
$httpClient = $this->getHttpClientMock($responseMock);
178178
$client = $this->getClientMock($httpClient);
179179

180-
$paginator = new Github\ResultPager( $client );
180+
$paginator = new Github\ResultPager($client);
181181
$paginator->postFetch();
182182

183183
$this->assertEquals($paginator->hasPrevious(), true);
184184
$this->assertEquals($paginator->hasNext(), false);
185185
}
186186

187-
protected function getResponseMock( array $pagination )
187+
protected function getResponseMock(array $pagination)
188188
{
189189
// response mock
190190
$responseMock = $this->getMock('Github\HttpClient\Message\Response');
191191
$responseMock
192192
->expects($this->any())
193193
->method('getPagination')
194-
->will($this->returnValue(
195-
$pagination
196-
));
194+
->will($this->returnValue($pagination));
197195

198196
return $responseMock;
199197
}
200198

201-
protected function getClientMock( HttpClientInterface $httpClient = null )
199+
protected function getClientMock(HttpClientInterface $httpClient = null)
202200
{
203201
// if no httpClient isset use the default HttpClient mock
204-
if( !$httpClient ){
202+
if (!$httpClient) {
205203
$httpClient = $this->getHttpClientMock();
206204
}
207205

@@ -211,7 +209,7 @@ protected function getClientMock( HttpClientInterface $httpClient = null )
211209
return $client;
212210
}
213211

214-
protected function getHttpClientMock( $responseMock = null )
212+
protected function getHttpClientMock($responseMock = null)
215213
{
216214
// mock the client interface
217215
$clientInterfaceMock = $this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send'));
@@ -230,7 +228,7 @@ protected function getHttpClientMock( $responseMock = null )
230228
// create the httpClient mock
231229
$httpClientMock = $this->getMock('Github\HttpClient\HttpClient', array(), array(array(), $clientInterfaceMock));
232230

233-
if( $responseMock ){
231+
if ($responseMock) {
234232
$httpClientMock
235233
->expects($this->any())
236234
->method('getLastResponse')
@@ -240,12 +238,12 @@ protected function getHttpClientMock( $responseMock = null )
240238
return $httpClientMock;
241239
}
242240

243-
protected function getApiMock( $apiClass )
241+
protected function getApiMock($apiClass)
244242
{
245243
$client = $this->getClientMock();
246244

247-
return $this->getMockBuilder( $apiClass )
245+
return $this->getMockBuilder($apiClass)
248246
->setConstructorArgs(array($client))
249247
->getMock();
250248
}
251-
}
249+
}

0 commit comments

Comments
 (0)