Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3769368
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/app/Phabricator/PhabricatorAPI.php b/app/Phabricator/PhabricatorAPI.php
index 1e375c9..34d917c 100644
--- a/app/Phabricator/PhabricatorAPI.php
+++ b/app/Phabricator/PhabricatorAPI.php
@@ -1,160 +1,160 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
use Nasqueron\Notifications\Contracts\APIClient;
use Services;
class PhabricatorAPI implements APIClient {
///
/// Private members
///
/**
* The Phabricator main URL
*
* @var string
*/
private $endPoint;
/**
* The token generated at /settings/panel/apitokens/ to query the API
*
* @var string
*/
private $apiToken;
///
/// Constructors
///
/**
* Initializes a new instance of the Phabricator API class
*
* @param string $endPoint The Phabricator main URL, without trailing slash
* @param string $apiToken The token generated at /settings/panel/apitokens/
*/
public function __construct ($endPoint, $apiToken) {
$this->endPoint = $endPoint;
$this->apiToken = $apiToken;
}
/**
* Gets an API instance for the specific instance
*
* @param string $instance The name of the instance (this matches that parameter in credentials.json)
- * @return PhabricatorAPI|null A PhabricatorAPI instance for the project if found; otherwise, null.
+ * @return PhabricatorAPI A PhabricatorAPI instance for the project if found; otherwise, null.
*/
public static function forInstance ($instance) {
$service = Services::findServiceByProperty('Phabricator', 'instance', $instance);
if ($service === null) {
throw new \RuntimeException("No credentials for Phabricator instance $instance.");
}
return new self($service->instance, $service->secret);
}
/**
* Gets an API instance for the specific project
*
* @param string $project The name of the project (this matches the door parameter in credentials.json)
- * @return PhabricatorAPI|null A PhabricatorAPI instance for the project if found; otherwise, null.
+ * @return PhabricatorAPI A PhabricatorAPI instance for the project if found; otherwise, null.
*/
public static function forProject ($project) {
$service = Services::findServiceByDoor('Phabricator', $project);
if ($service === null) {
- return null;
+ throw new \RuntimeException("No credentials for Phabricator project $project.");
}
return new self($service->instance, $service->secret);
}
///
/// 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 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->endPoint . '/api/' . $method;
$arguments['api.token'] = $this->apiToken;
$reply = json_decode(static::post($url, $arguments));
if ($reply->error_code !== null) {
throw new PhabricatorAPIException(
$reply->error_code,
$reply->error_info
);
}
return $reply->result;
}
///
/// Helper methods
///
/**
* Gets the first result of an API reply
*
* @param Traversable|array $reply
* @return mixed
*/
public static function getFirstResult ($reply) {
if (is_object($reply) && property_exists($reply, 'data')) {
$reply = $reply->data;
}
foreach ($reply as $value) {
return $value;
}
}
///
/// CURL session
///
protected static function getPostFields ($arguments) {
$items = [];
foreach ($arguments as $key => $value) {
$items[] = urlencode($key) . '=' . urlencode($value);
}
return implode('&', $items);
}
protected static function post ($url, $arguments) {
$options = [
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_POSTFIELDS => static::getPostFields($arguments),
];
$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
if ($result === false) {
throw new \RuntimeException("Can't reach Phabricator API endpoint: $url");
}
return $result;
}
}
diff --git a/tests/Phabricator/PhabricatorAPITest.php b/tests/Phabricator/PhabricatorAPITest.php
new file mode 100644
index 0000000..282e0b3
--- /dev/null
+++ b/tests/Phabricator/PhabricatorAPITest.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Nasqueron\Notifications\Tests\Phabricator;
+
+use Nasqueron\Notifications\Phabricator\PhabricatorAPI;
+use Nasqueron\Notifications\Tests\TestCase;
+
+class PhabricatorAPITest extends TestCase {
+ public function testForInstance () {
+ $this->assertInstanceOf(
+ '\Nasqueron\Notifications\Phabricator\PhabricatorAPI',
+ PhabricatorAPI::forInstance("https://phabricator.acme.tld")
+ );
+ }
+
+ public function testForProject () {
+ $this->assertInstanceOf(
+ '\Nasqueron\Notifications\Phabricator\PhabricatorAPI',
+ PhabricatorAPI::forInstance("https://phabricator.acme.tld")
+ );
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testForInstanceWhere () {
+ PhabricatorAPI::forInstance("https://notfound.acme.tld");
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testForProjectWhenProjectDoesNotExist () {
+ PhabricatorAPI::forProject("NotFound");
+ }
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 14:24 (1 d, 5 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260354
Default Alt Text
(6 KB)
Attached To
Mode
rNOTIF Notifications center
Attached
Detach File
Event Timeline
Log In to Comment