Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3767743
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
11 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/app/Phabricator/PhabricatorAPIException.php b/app/Phabricator/PhabricatorAPIException.php
index 5af0cf1..38dc96d 100644
--- a/app/Phabricator/PhabricatorAPIException.php
+++ b/app/Phabricator/PhabricatorAPIException.php
@@ -1,14 +1,16 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
class PhabricatorAPIException extends \RuntimeException {
+
/**
* @param int $code The error_code field for the API reply
* @param string $message The error_info field from the API reply
*/
public function __construct ($code, $message) {
$this->code = $code;
$this->message = $message;
}
+
}
diff --git a/app/Phabricator/PhabricatorAPIFactory.php b/app/Phabricator/PhabricatorAPIFactory.php
index f92a3e5..c00e58f 100644
--- a/app/Phabricator/PhabricatorAPIFactory.php
+++ b/app/Phabricator/PhabricatorAPIFactory.php
@@ -1,28 +1,28 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
use Nasqueron\Notifications\Contracts\APIFactory;
class PhabricatorAPIFactory implements APIFactory {
/**
- * Gets an instance of the Phabricator API client class
+ * Gets an instance of the Phabricator API client class.
*
* @param string $instance The Phabricator instance
* @return Nasqueron\Notifications\Phabricator\PhabricatorAPI
*/
public function get ($instance) {
return PhabricatorAPI::forInstance($instance);
}
/**
- * Gets an instance of the Phabricator API client class for a project
+ * Gets an instance of the Phabricator API client class for a project.
*
* @param string $project The Phabricator project name
* @return Nasqueron\Notifications\Phabricator\PhabricatorAPI
*/
public function getForProject ($project) {
return PhabricatorAPI::forProject($project);
}
}
diff --git a/app/Phabricator/ProjectsMap.php b/app/Phabricator/ProjectsMap.php
index 876053f..b0f8dbc 100644
--- a/app/Phabricator/ProjectsMap.php
+++ b/app/Phabricator/ProjectsMap.php
@@ -1,275 +1,278 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
use Nasqueron\Notifications\Phabricator\PhabricatorAPIClient as ApiClient;
use App;
use Cache;
class ProjectsMap implements \IteratorAggregate, \ArrayAccess {
///
/// Private properties and constants
///
/**
- * The maximum number of projects to fetch
- */
+ * The maximum number of projects to fetch
+ */
const LIMIT = 1000;
/**
- * The projects as an array with phid as keys, project names as $value
- *
- * @var string[]
- */
+ * The projects as an array with phid as keys, project names as $value
+ *
+ * @var string[]
+ */
private $map = [];
/**
- * The Phabricator instance for this projects map
- *
- * @var string
- */
+ * The Phabricator instance for this projects map
+ *
+ * @var string
+ */
private $instance;
/**
*
* @var Nasqueron\Notifications\Contracts\APIClient
*/
private $apiClient;
/**
* The source of the map
*
* @var string
*/
private $source = 'unloaded';
///
/// Constructor
///
/**
- * Initializes a new instance of ProjectsMap
- *
- * @param string $instance The Phabricator root URL without trailing slash
- */
+ * Initializes a new instance of ProjectsMap.
+ *
+ * @param string $instance The Phabricator root URL without trailing slash
+ */
public function __construct ($instance) {
$this->instance = $instance;
}
///
/// IteratorAggregate interface implementation
///
/**
- * Gets iterator
- *
- * @return Traversable
- */
+ * Gets iterator.
+ *
+ * @return Traversable
+ */
public function getIterator () {
return new \ArrayIterator($this->map);
}
///
/// ArrayAccess interface implementation
///
/**
- * Determines whether an offset exists
- *
- * @param mixed $offset The offset
- */
+ * Determines whether an offset exists.
+ *
+ * @param mixed $offset The offset
+ * @return bool
+ */
public function offsetExists ($offset) {
return array_key_exists($offset, $this->map);
}
/**
- * Gets an offset
- *
- * @param mixed $offset The offset
- * @return mixed The value
- */
+ * Gets the value at the specified offset.
+ *
+ * @param mixed $offset The offset.
+ * @return mixed The value
+ */
public function offsetGet ($offset) {
return $this->map[$offset];
}
/**
- * Assigns a value to the specified offset
- *
- * @param mixed $offset The offset
- * @param mixed $value The value to assign
- */
+ * Assigns a value to the specified offset.
+ *
+ * @param mixed $offset The offset
+ * @param mixed $value The value to assign
+ */
public function offsetSet ($offset, $value) {
$this->map[$offset] = $value;
}
/**
- * Unset an offset
- *
- * @param mixed $offset The offset
- */
+ * Unsets a value at the specified offset.
+ *
+ * @param mixed $offset The offset where to remove the value
+ */
public function offsetUnset ($offset) {
unset($this->map[$offset]);
}
///
/// Static constructors
///
/**
- * Gets a new ProjectsMap instance from cache or API when not cached
- *
- * @param string $phabricatorURL The Phabricator URL (e.g. http://secure.phabricator.com)
- * @return ProjectsMap
- */
+ * Gets a new ProjectsMap instance from cache or API when not cached.
+ *
+ * @param string $phabricatorURL The Phabricator URL (e.g. http://secure.phabricator.com)
+ * @return ProjectsMap
+ */
public static function load ($phabricatorURL) {
$instance = new self($phabricatorURL);
if ($instance->isCached()) {
$instance->loadFromCache();
} else {
$instance->fetchFromAPI();
}
return $instance;
}
/**
- * Gets a new ProjectsMap instance and queries Phabricator API to fill it
- *
- * @param string $phabricatorURL The Phabricator URL (e.g. http://secure.phabricator.com)
- * @param Nasqueron\Notifications\Contracts\APIClient $apiClient The Phabricator API client
- * @return ProjectsMap
- */
+ * Gets a new ProjectsMap instance and queries Phabricator API to fill it.
+ *
+ * @param string $phabricatorURL The Phabricator URL (e.g. http://secure.phabricator.com)
+ * @param Nasqueron\Notifications\Contracts\APIClient $apiClient The Phabricator API client
+ * @return ProjectsMap
+ */
public static function fetch ($phabricatorURL, APIClient $apiClient = null) {
$instance = new self($phabricatorURL);
$instance->setAPIClient($apiClient);
$instance->fetchFromAPI();
return $instance;
}
///
/// API
///
/**
* @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 = null) {
$this->apiClient = $apiClient;
}
/**
- * Fetches the projects' map from the Phabricator API
- */
+ * Fetches the projects' map from the Phabricator API.
+ *
+ * @throws \Exception when API reply is empty or invalid.
+ */
private function fetchFromAPI () {
$reply = $this->getAPIClient()->call(
'project.query',
[ 'limit' => self::LIMIT ]
);
if (!$reply) {
throw new \Exception("Empty reply calling project.query at $this->instance API.");
}
if (!property_exists($reply, 'data')) {
throw new \Exception("Invalid reply calling project.query at $this->instance API.");
}
foreach ($reply->data as $phid => $projectInfo) {
$this->offsetSet($phid, $projectInfo->name);
}
$this->source = 'api';
}
///
/// Cache
///
/**
- * Gets cache key
- *
- * @return string The cache key for the current projects map
- */
+ * Gets cache key.
+ *
+ * @return string The cache key for the current projects map
+ */
private function getCacheKey () {
return class_basename(get_class($this)) . '-' . md5($this->instance);
}
/**
- * Determines if the instance is cached
- *
- * @return bool true if cached; otherwise, false.
- */
+ * Determines if the instance is cached
+ *
+ * @return bool true if cached; otherwise, false.
+ */
public function isCached () {
return Cache::has($this->getCacheKey());
}
/**
- * Saves data to cache
- */
+ * Saves data to cache
+ */
public function saveToCache () {
Cache::forever($this->getCacheKey(), $this->map);
}
/**
- * Loads data from cache
- *
- * Populates 'map' and 'source' properties
- */
+ * Loads data from cache
+ *
+ * Populates 'map' and 'source' properties
+ */
public function loadFromCache () {
$cachedMap = Cache::get($this->getCacheKey());
if ($cachedMap !== null) {
$this->map = $cachedMap;
$this->source = 'cache';
}
}
///
/// Output
///
/**
- * Gets project name, refreshing the cache if needed
- *
- * @param string $projectPHID the PHID of the project to query the name
- * @return string
- */
+ * Gets project name, refreshing the cache if needed.
+ *
+ * @param string $projectPHID the PHID of the project to query the name
+ * @return string The name of the poject, or an empty string if not found
+ */
public function getProjectName ($projectPHID) {
if ($this->offsetExists($projectPHID)) {
return $this->offsetGet($projectPHID);
}
if ($this->source !== 'api') {
$this->fetchFromAPI();
return $this->getProjectName($projectPHID);
}
return "";
}
/**
- * Returns the projects map as an array, each row ['PHID', 'project name']
- *
- * @return array
- */
+ * Returns the projects map as an array.
+ *
+ * @return array An array, each row containing ['PHID', 'project name']
+ */
public function toArray () {
$array = [];
foreach ($this->map as $phid => $projectName) {
$array[] = [$phid, $projectName];
}
return $array;
}
}
diff --git a/app/Phabricator/ProjectsMapFactory.php b/app/Phabricator/ProjectsMapFactory.php
index 8f1950d..66c0a89 100644
--- a/app/Phabricator/ProjectsMapFactory.php
+++ b/app/Phabricator/ProjectsMapFactory.php
@@ -1,27 +1,27 @@
<?php
namespace Nasqueron\Notifications\Phabricator;
class ProjectsMapFactory {
/**
- * Loads projects map from cache or fetches it from API if not cached
+ * Loads projects map from cache or fetches it from API if not cached.
*
* @param string $instance The Phabricator instance
* @return Nasqueron\Notifications\Phabricator\ProjectsMap
*/
public function load ($instance) {
return ProjectsMap::load($instance);
}
/**
- * Fetches projects map from API
+ * Fetches projects map from API.
*
* @param string $instance The Phabricator instance
* @return Nasqueron\Notifications\Phabricator\ProjectsMap
*/
public function fetch ($instance) {
return ProjectsMap::fetch($instance);
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 03:15 (19 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2259426
Default Alt Text
(11 KB)
Attached To
Mode
rNOTIF Notifications center
Attached
Detach File
Event Timeline
Log In to Comment