Skip to content

[3.x] Re-worked pagination to not mutate the api classes #907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"psr/cache": "^1.0",
"psr/http-client-implementation": "^1.0",
"psr/http-factory-implementation": "^1.0",
"psr/http-message": "^1.0"
"psr/http-message": "^1.0",
"symfony/polyfill-php80": "^1.17"
},
"require-dev": {
"symfony/cache": "^5.1.8",
Expand Down
66 changes: 22 additions & 44 deletions lib/Github/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,59 @@
use Github\HttpClient\Message\ResponseMediator;

/**
* Abstract class for Api classes.
*
* @author Joseph Bielawski <[email protected]>
* @author Graham Campbell <[email protected]>
*/
abstract class AbstractApi implements ApiInterface
abstract class AbstractApi
{
/**
* The client.
* The client instance.
*
* @var Client
*/
protected $client;
private $client;

/**
* The requested page (GitHub pagination).
* The per page parameter.
*
* @var null|int
* @var int|null
*/
private $page;
private $perPage;

/**
* Number of items per page (GitHub pagination).
* Create a new API instance.
*
* @var null|int
*/
protected $perPage;

/**
* @param Client $client
*
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
}

public function configure()
{
}

/**
* @return null|int
*/
public function getPage()
{
return $this->page;
}

/**
* @param null|int $page
* Get the client instance.
*
* @return Client
*/
public function setPage($page)
protected function getClient()
{
$this->page = (null === $page ? $page : (int) $page);

return $this;
return $this->client;
}

/**
* @return null|int
* Get the API version.
*
* @return string
*/
public function getPerPage()
protected function getApiVersion()
{
return $this->perPage;
return $this->client->getApiVersion();
}

/**
* @param null|int $perPage
*/
public function setPerPage($perPage)
public function configure()
{
$this->perPage = (null === $perPage ? $perPage : (int) $perPage);

return $this;
}

/**
Expand All @@ -92,12 +72,10 @@ public function setPerPage($perPage)
*/
protected function get($path, array $parameters = [], array $requestHeaders = [])
{
if (null !== $this->page && !isset($parameters['page'])) {
$parameters['page'] = $this->page;
}
if (null !== $this->perPage && !isset($parameters['per_page'])) {
$parameters['per_page'] = $this->perPage;
}

if (array_key_exists('ref', $parameters) && null === $parameters['ref']) {
unset($parameters['ref']);
}
Expand Down
15 changes: 0 additions & 15 deletions lib/Github/Api/ApiInterface.php

This file was deleted.

14 changes: 7 additions & 7 deletions lib/Github/Api/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public function update(array $params)
*/
public function emails()
{
return new Emails($this->client);
return new Emails($this->getClient());
}

/**
* @return Followers
*/
public function follow()
{
return new Followers($this->client);
return new Followers($this->getClient());
}

public function followers($page = 1)
Expand Down Expand Up @@ -71,23 +71,23 @@ public function issues(array $params = [], $includeOrgIssues = true)
*/
public function keys()
{
return new PublicKeys($this->client);
return new PublicKeys($this->getClient());
}

/**
* @return Notifications
*/
public function notifications()
{
return new Notifications($this->client);
return new Notifications($this->getClient());
}

/**
* @return Memberships
*/
public function memberships()
{
return new Memberships($this->client);
return new Memberships($this->getClient());
}

/**
Expand Down Expand Up @@ -147,15 +147,15 @@ public function repositories($type = 'owner', $sort = 'full_name', $direction =
*/
public function watchers()
{
return new Watchers($this->client);
return new Watchers($this->getClient());
}

/**
* @return Starring
*/
public function starring()
{
return new Starring($this->client);
return new Starring($this->getClient());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Github/Api/CurrentUser/Starring.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Starring extends AbstractApi
public function configure($bodyType = null)
{
if ('star' === $bodyType) {
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.star+json', $this->client->getApiVersion());
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.star+json', $this->getApiVersion());
}

return $this;
Expand Down
8 changes: 4 additions & 4 deletions lib/Github/Api/Enterprise.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@ class Enterprise extends AbstractApi
*/
public function stats()
{
return new Stats($this->client);
return new Stats($this->getClient());
}

/**
* @return License
*/
public function license()
{
return new License($this->client);
return new License($this->getClient());
}

/**
* @return ManagementConsole
*/
public function console()
{
return new ManagementConsole($this->client);
return new ManagementConsole($this->getClient());
}

/**
* @return UserAdmin
*/
public function userAdmin()
{
return new UserAdmin($this->client);
return new UserAdmin($this->getClient());
}
}
2 changes: 1 addition & 1 deletion lib/Github/Api/Gist/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function configure($bodyType = null)
$bodyType = 'raw';
}

$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->getApiVersion(), $bodyType);

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Github/Api/Gists.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function configure($bodyType = null)
$bodyType = 'raw';
}

$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType);
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->getApiVersion(), $bodyType);

return $this;
}
Expand Down Expand Up @@ -177,6 +177,6 @@ public function unstar($id)
*/
public function comments()
{
return new Comments($this->client);
return new Comments($this->getClient());
}
}
10 changes: 5 additions & 5 deletions lib/Github/Api/GitData.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,38 @@ class GitData extends AbstractApi
*/
public function blobs()
{
return new Blobs($this->client);
return new Blobs($this->getClient());
}

/**
* @return Commits
*/
public function commits()
{
return new Commits($this->client);
return new Commits($this->getClient());
}

/**
* @return References
*/
public function references()
{
return new References($this->client);
return new References($this->getClient());
}

/**
* @return Tags
*/
public function tags()
{
return new Tags($this->client);
return new Tags($this->getClient());
}

/**
* @return Trees
*/
public function trees()
{
return new Trees($this->client);
return new Trees($this->getClient());
}
}
2 changes: 1 addition & 1 deletion lib/Github/Api/GitData/Blobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Blobs extends AbstractApi
public function configure($bodyType = null)
{
if ('raw' === $bodyType) {
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->client->getApiVersion());
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.raw', $this->getApiVersion());
}

return $this;
Expand Down
14 changes: 7 additions & 7 deletions lib/Github/Api/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function configure($bodyType = null)
$bodyType = 'raw';
}

$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->getApiVersion(), $bodyType);

return $this;
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public function unlock($username, $repository, $id)
*/
public function comments()
{
return new Comments($this->client);
return new Comments($this->getClient());
}

/**
Expand All @@ -188,7 +188,7 @@ public function comments()
*/
public function events()
{
return new Events($this->client);
return new Events($this->getClient());
}

/**
Expand All @@ -200,7 +200,7 @@ public function events()
*/
public function labels()
{
return new Labels($this->client);
return new Labels($this->getClient());
}

/**
Expand All @@ -212,7 +212,7 @@ public function labels()
*/
public function milestones()
{
return new Milestones($this->client);
return new Milestones($this->getClient());
}

/**
Expand All @@ -224,7 +224,7 @@ public function milestones()
*/
public function assignees()
{
return new Assignees($this->client);
return new Assignees($this->getClient());
}

/**
Expand All @@ -236,6 +236,6 @@ public function assignees()
*/
public function timeline()
{
return new Timeline($this->client);
return new Timeline($this->getClient());
}
}
2 changes: 1 addition & 1 deletion lib/Github/Api/Issue/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function configure($bodyType = null)
$bodyType = 'full';
}

$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->getApiVersion(), $bodyType);

return $this;
}
Expand Down
Loading