diff --git a/app/Contracts/APIClient.php b/app/Contracts/APIClient.php new file mode 100644 --- /dev/null +++ b/app/Contracts/APIClient.php @@ -0,0 +1,24 @@ +<?php + +namespace Nasqueron\Notifications\Contracts; + +interface APIClient { + + /** + * Sets API end point + * + * @param string $url The API end point URL + * @return void + */ + public function setEndPoint ($url); + + /** + * Calls an API method + * + * @param string $method The method to call + * @param array $arguments The arguments to use + * @return mixed The API result + */ + public function call ($method, $arguments = []); + +} diff --git a/app/Contracts/APIFactory.php b/app/Contracts/APIFactory.php new file mode 100644 --- /dev/null +++ b/app/Contracts/APIFactory.php @@ -0,0 +1,15 @@ +<?php + +namespace Nasqueron\Notifications\Contracts; + +interface APIFactory { + + /** + * Gets an instance of the API client class + * + * @param string $endPoint The API end point + * @return Nasqueron\Notifications\Contracts\APIClient + */ + public function get ($endPoint); + +} diff --git a/app/Phabricator/PhabricatorAPI.php b/app/Phabricator/PhabricatorAPI.php --- a/app/Phabricator/PhabricatorAPI.php +++ b/app/Phabricator/PhabricatorAPI.php @@ -2,9 +2,11 @@ namespace Nasqueron\Notifications\Phabricator; +use Nasqueron\Notifications\Contracts\APIClient; + use Services; -class PhabricatorAPI { +class PhabricatorAPI implements APIClient { /// /// Private members @@ -15,7 +17,7 @@ * * @var string */ - private $instance; + private $endPoint; /** * The token generated at /settings/panel/apitokens/ to query the API @@ -31,11 +33,11 @@ /** * Initializes a new instance of the Phabricator API class * - * @param string $instance The Phabricator main URL, without trailing slash + * @param string $endPoint The Phabricator main URL, without trailing slash * @param string $apiToken The token generated at /settings/panel/apitokens/ */ - public function __construct ($instance, $apiToken) { - $this->instance = $instance; + public function __construct ($endPoint, $apiToken) { + $this->endPoint = $endPoint; $this->apiToken = $apiToken; } @@ -68,17 +70,27 @@ } /// - /// Public methods + /// APIClient implementation /// /** + * Sets API end point + * + * @param string $url The API end point URL + */ + public function setEndPoint ($url) { + $this->endPoint = $url; + } + + /** * Calls a Conduit API method * - * @param $method The method to call (e.g. repository.create) - * @param $arguments The arguments to use + * @param string $method The method to call (e.g. repository.create) + * @param array $arguments The arguments to use + * @return mixed The API result */ public function call ($method, $arguments = []) { - $url = $this->instance . '/api/' . $method; + $url = $this->endPoint . '/api/' . $method; $arguments['api.token'] = $this->apiToken; $reply = json_decode(static::post($url, $arguments)); @@ -93,6 +105,10 @@ return $reply->result; } + /// + /// Helper methods + /// + /** * Gets the first result of an API reply * diff --git a/app/Phabricator/PhabricatorAPIFactory.php b/app/Phabricator/PhabricatorAPIFactory.php --- a/app/Phabricator/PhabricatorAPIFactory.php +++ b/app/Phabricator/PhabricatorAPIFactory.php @@ -2,7 +2,9 @@ namespace Nasqueron\Notifications\Phabricator; -class PhabricatorAPIFactory { +use Nasqueron\Notifications\Contracts\APIFactory; + +class PhabricatorAPIFactory implements APIFactory { /** * Gets an instance of the Phabricator API client class diff --git a/app/Phabricator/ProjectsMap.php b/app/Phabricator/ProjectsMap.php --- a/app/Phabricator/ProjectsMap.php +++ b/app/Phabricator/ProjectsMap.php @@ -2,6 +2,9 @@ namespace Nasqueron\Notifications\Phabricator; +use Nasqueron\Notifications\Phabricator\PhabricatorAPIClient as ApiClient; + +use App; use Cache; class ProjectsMap implements \IteratorAggregate, \ArrayAccess { @@ -30,6 +33,12 @@ private $instance; /** + * + * @var Nasqueron\Notifications\Contracts\APIClient + */ + private $apiClient; + + /** * The source of the map * * @var string @@ -143,10 +152,28 @@ /// /** + * @return Nasqueron\Notifications\Contracts\APIClient + */ + public function getAPIClient () { + if ($this->apiClient === null) { + $factory = App::make('phabricator-api'); + $this->apiClient = $factory->get($this->instance); + } + return $this->apiClient; + } + + /** + * @param Nasqueron\Notifications\Contracts\APIClient $apiClient + */ + public function setAPIClient (APIClient $apiClient) { + $this->apiClient = $apiClient; + } + + /** * Fetches the projects' map from the Phabricator API */ private function fetchFromAPI () { - $reply = \PhabricatorAPI::get($this->instance)->call( + $reply = $this->getAPIClient()->call( 'project.query', [ 'limit' => self::LIMIT ] );