mirror of
https://github.com/sitelease/sugar-cube-client.git
synced 2025-10-29 11:02:30 +01:00
Refactored and reorganized
+ Created new a new interface and abstract class for Api classes that support the all() method + Refactored the Repositories Api class + Moved the abstract classes into an "Abstracts" folder + Moved Api Interfaces into an "Interfaces" folder + Corrected namespaces
This commit is contained in:
85
src/Api/Abstracts/AbstractAllApi.php
Normal file
85
src/Api/Abstracts/AbstractAllApi.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Api\Abstracts;
|
||||
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
|
||||
use Gitea\Client;
|
||||
use Gitea\Collections\ApiItemCollection;
|
||||
use Gitea\Models\Repository;
|
||||
|
||||
use Gitea\Api\Abstracts\AbstractApi;
|
||||
use Gitea\Api\Interfaces\AllApiInterface;
|
||||
|
||||
abstract class AbstractAllApi extends AbstractApi implements AllApiInterface
|
||||
{
|
||||
/**
|
||||
* The maximum number of pages to process when
|
||||
* retrieving all repositories
|
||||
*
|
||||
* NOTE: Each page will contain the number of items
|
||||
* set in the $maxPageCount property (50 by default).
|
||||
* So you can get the number of records that this will equate to
|
||||
* by multiplying this number by the $maxPageCount
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $maxPageCount = 25;
|
||||
|
||||
/**
|
||||
* The number of items per page
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $itemsPerPage = 50;
|
||||
|
||||
/**
|
||||
* Get a page of items from the API route
|
||||
* that will provide all items
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @param integer $page The page of items to return
|
||||
* @param integer $limit Maximum number of items per page
|
||||
* @param array $extraOptions An array of extra options to pass the API reoute
|
||||
* @return ApiItemCollection
|
||||
*/
|
||||
public function getPageOfAllItems(int $page = 1, int $limit = null, array $extraOptions = array()) {
|
||||
trigger_error("The abstract 'requestAllItems()' method must be overwritten");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a collection of all items from an API route
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @return ApiItemCollection
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$allItems = array();
|
||||
$maxPageCount = $this->getMaxPageCount();
|
||||
$itemsPerPage = $this->getItemsPerPage();
|
||||
// Loop over pages until the $maxPageCount is reached
|
||||
for ($pageNum=1; $pageNum < $maxPageCount; $pageNum++) {
|
||||
$searchItemsCollection = $this->getPageOfAllItems($pageNum, $itemsPerPage);
|
||||
if ($searchItemsCollection && $searchItemsCollection->count() > 0) {
|
||||
$searchItemsArray = $searchItemsCollection->toArray();
|
||||
$allItems = array_merge($allItems, $searchItemsArray);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new ApiItemCollection($allItems);
|
||||
}
|
||||
|
||||
public function getMaxPageCount() {
|
||||
return $this->maxPageCount;
|
||||
}
|
||||
|
||||
public function getItemsPerPage() {
|
||||
return $this->itemsPerPage;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,15 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Api;
|
||||
namespace Gitea\Api\Abstracts;
|
||||
|
||||
use Gitea\Client;
|
||||
|
||||
use Gitea\Api\Interfaces\ApiInterface;
|
||||
|
||||
/**
|
||||
* Abstract class for Api classes
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*/
|
||||
abstract class AbstractApi
|
||||
abstract class AbstractApi implements ApiInterface
|
||||
{
|
||||
/**
|
||||
* The client
|
||||
@ -167,28 +169,13 @@ abstract class AbstractApi
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a GET query and returns the response as a PSR-7 response object.
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param array $requestHeaders
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function getAsResponse($path, array $parameters = array(), $requestHeaders = array())
|
||||
{
|
||||
$path = $this->preparePath($path, $parameters);
|
||||
|
||||
return $this->client->getHttpClient()->get($path, $requestHeaders);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param array $requestHeaders
|
||||
* @return mixed
|
||||
*/
|
||||
protected function get($path, array $parameters = array(), $requestHeaders = array(), $debugRequest = false)
|
||||
public function get($path, array $parameters = array(), $requestHeaders = array(), $debugRequest = false)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$guzzleClient = $client->getGuzzleClient();
|
||||
@ -221,7 +208,7 @@ abstract class AbstractApi
|
||||
* @param array $requestHeaders
|
||||
* @return mixed
|
||||
*/
|
||||
protected function post($path, $body, $requestHeaders = array(), $debugRequest = false)
|
||||
public function post($path, $body, $requestHeaders = array(), $debugRequest = false)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$guzzleClient = $client->getGuzzleClient();
|
||||
@ -259,7 +246,7 @@ abstract class AbstractApi
|
||||
* @param array $requestHeaders
|
||||
* @return mixed
|
||||
*/
|
||||
protected function put($path, $body, $requestHeaders = array(), $debugRequest = false)
|
||||
public function put($path, $body, $requestHeaders = array(), $debugRequest = false)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$guzzleClient = $client->getGuzzleClient();
|
||||
@ -296,7 +283,7 @@ abstract class AbstractApi
|
||||
* @param array $requestHeaders
|
||||
* @return mixed
|
||||
*/
|
||||
protected function delete($path, $requestHeaders = array(), $debugRequest = false)
|
||||
public function delete($path, $requestHeaders = array(), $debugRequest = false)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
$guzzleClient = $client->getGuzzleClient();
|
||||
53
src/Api/Interfaces/AllApiInterface.php
Normal file
53
src/Api/Interfaces/AllApiInterface.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Api\Interfaces;
|
||||
|
||||
use Gitea\Client;
|
||||
|
||||
/**
|
||||
* Interface for API classes that support the all() method
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*/
|
||||
interface AllApiInterface
|
||||
{
|
||||
/**
|
||||
* The maximum number of pages to process when
|
||||
* retrieving all items
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
* @return int
|
||||
*/
|
||||
public function getMaxPageCount();
|
||||
|
||||
/**
|
||||
* Get the number of items that will be retrieved per
|
||||
* page when retrieving all items
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
* @return int
|
||||
*/
|
||||
public function getItemsPerPage();
|
||||
|
||||
/**
|
||||
* Get a page of items from the API route
|
||||
* that will provide all items
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @param integer $page The page of items to return
|
||||
* @param integer $limit Maximum number of items per page
|
||||
* @param array $extraOptions An array of extra options to pass the API reoute
|
||||
* @return ApiItemCollection
|
||||
*/
|
||||
public function getPageOfAllItems(int $page = 1, int $limit = null, array $extraOptions = array());
|
||||
|
||||
/**
|
||||
* Return a collection of all items
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @return ApiItemCollection
|
||||
*/
|
||||
public function all();
|
||||
}
|
||||
71
src/Api/Interfaces/ApiInterface.php
Normal file
71
src/Api/Interfaces/ApiInterface.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Api\Interfaces;
|
||||
|
||||
use Gitea\Client;
|
||||
|
||||
/**
|
||||
* Interface for API classes
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*/
|
||||
interface ApiInterface
|
||||
{
|
||||
/**
|
||||
* @param Client $client
|
||||
*/
|
||||
public function __construct(Client $client, $authToken);
|
||||
|
||||
public function getClient();
|
||||
|
||||
public function getAuthToken();
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function configure();
|
||||
|
||||
/**
|
||||
* Send a GET request using an underlying reqest library
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param array $requestHeaders
|
||||
* @param boolean $debugRequest
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($path, array $parameters = array(), $requestHeaders = array(), $debugRequest = false);
|
||||
|
||||
/**
|
||||
* Send a POST request using an underlying reqest library
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $body
|
||||
* @param array $requestHeaders
|
||||
* @param boolean $debugRequest
|
||||
* @return mixed
|
||||
*/
|
||||
public function post($path, $body, $requestHeaders = array(), $debugRequest = false);
|
||||
|
||||
/**
|
||||
* Send a PUT request using an underlying reqest library
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $body
|
||||
* @param array $requestHeaders
|
||||
* @param boolean $debugRequest
|
||||
* @return mixed
|
||||
*/
|
||||
public function put($path, $body, $requestHeaders = array(), $debugRequest = false);
|
||||
|
||||
/**
|
||||
* Send a DELETE request using an underlying reqest library
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $requestHeaders
|
||||
* @param boolean $debugRequest
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete($path, $requestHeaders = array(), $debugRequest = false);
|
||||
}
|
||||
@ -7,7 +7,7 @@ use GuzzleHttp\Psr7\Response;
|
||||
use Gitea\Client;
|
||||
use Gitea\Models\Organization;
|
||||
|
||||
use Gitea\Api\AbstractApi;
|
||||
use Gitea\Api\Abstracts\AbstractApi;
|
||||
|
||||
class Organizations extends AbstractApi
|
||||
{
|
||||
|
||||
@ -2,57 +2,29 @@
|
||||
|
||||
namespace Gitea\Api;
|
||||
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
|
||||
use Gitea\Client;
|
||||
use Gitea\Collections\ApiItemCollection;
|
||||
use Gitea\Models\Repository;
|
||||
|
||||
use Gitea\Api\AbstractApi;
|
||||
use Gitea\Api\Abstracts\AbstractAllApi;
|
||||
|
||||
class Repositories extends AbstractApi
|
||||
class Repositories extends AbstractAllApi
|
||||
{
|
||||
|
||||
/**
|
||||
* The maximum number of pages to process when
|
||||
* retrieving all repositories
|
||||
*
|
||||
* NOTE: Each page will contain the number of items
|
||||
* set in the $maxPageCount property (50 by default).
|
||||
* So you can get the number of records that this will equate to
|
||||
* by multiplying this number by the $maxPageCount
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $maxPageCount = 25;
|
||||
|
||||
/**
|
||||
* The number of items per page
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $itemsPerPage = 50;
|
||||
|
||||
/**
|
||||
* Return a collection of all the repositories
|
||||
* Get a page of items from the API route
|
||||
* that will provide all items
|
||||
*
|
||||
* @author Benjamin Blake (sitelease.ca)
|
||||
*
|
||||
* @param integer $page The page of items to return
|
||||
* @param integer $limit Maximum number of items per page
|
||||
* @param array $extraOptions An array of extra options to pass the API reoute
|
||||
* @return ApiItemCollection
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$allItems = array();
|
||||
$maxPageCount = $this->getMaxPageCount();
|
||||
// Loop over pages until the $maxPageCount is reached
|
||||
for ($pageNum=1; $pageNum < $maxPageCount; $pageNum++) {
|
||||
$searchItemsCollection = $this->search("", $pageNum);
|
||||
if ($searchItemsCollection && $searchItemsCollection->count() > 0) {
|
||||
$searchItemsArray = $searchItemsCollection->toArray();
|
||||
$allItems = array_merge($allItems, $searchItemsArray);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new ApiItemCollection($allItems);
|
||||
public function getPageOfAllItems(int $page = 1, int $limit = null, array $extraOptions = array()) {
|
||||
return $this->search("", $page, $limit, $extraOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,8 +39,6 @@ class Repositories extends AbstractApi
|
||||
* @param integer $page The page of items to return
|
||||
* @param integer $limit Maximum number of items per page
|
||||
* @param array $extraOptions An array of extra options to pass the API reoute
|
||||
* @return void
|
||||
*
|
||||
* @return ApiItemCollection
|
||||
*/
|
||||
public function search(string $keyword = "", int $page = 1, int $limit = null, array $extraOptions = array())
|
||||
@ -104,12 +74,4 @@ class Repositories extends AbstractApi
|
||||
}
|
||||
}
|
||||
|
||||
public function getMaxPageCount() {
|
||||
return $this->maxPageCount;
|
||||
}
|
||||
|
||||
public function getItemsPerPage() {
|
||||
return $this->itemsPerPage;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user