diff --git a/src/DrupalClient.php b/src/DrupalClient.php index 27cccb3..dcc102e 100644 --- a/src/DrupalClient.php +++ b/src/DrupalClient.php @@ -8,8 +8,13 @@ use EclipseGc\DrupalOrg\Api\Resources\GetResource; +use EclipseGc\DrupalOrg\Api\Resources\ResourceList; use GuzzleHttp\Client; +/** + * Class DrupalClient + * @package EclipseGc\DrupalOrg\Api + */ class DrupalClient { use GetResource; @@ -41,6 +46,8 @@ public function __construct(FactoryInterface $factory) { } /** + * Returns a user. + * * @param $id * @return \EclipseGc\DrupalOrg\Api\Resources\User */ @@ -49,6 +56,8 @@ public function getUser($id) { } /** + * Returns a node. + * * @param $id * @return \EclipseGc\DrupalOrg\Api\Resources\Node\NodeInterface; */ @@ -56,4 +65,87 @@ public function getNode($id) { return $this->getResource('node', $id); } -} \ No newline at end of file + /** + * Returns a + * + * @param $moduleName + * Module name or node ID for a project. + * @return \EclipseGc\DrupalOrg\Api\Resources\Node\ProjectModule + */ + public function getModule($moduleName) { + if (is_integer($moduleName)) { + return $this->getNode($moduleName); + } + $moduleItems = $this->getPagedResource('node', ['field_project_machine_name' => $moduleName])->getItems(); + return reset($moduleItems); + } + + + /** + * Returns all of the issues for a project. + * + * @param $moduleName + * You can pass in an integer or a string. + * @return ResourceList + */ + public function getIssuesForProject($moduleName) { + $nid = $this->getModuleNodeId($moduleName); + $issues = $this->getPagedResource('node', ['field_project' => $nid]); + return $issues; + } + + /** + * Returns the releases for the specified module. + * + * @param $moduleName + * You can pass in an integer or a string. + * @return ResourceList + */ + public function getReleasesForProject($moduleName) { + $nid = $this->getModuleNodeId($moduleName); + $releases = $this->getPagedResource('node', ['type' => 'project_release', 'field_release_project' => $nid]); + return $releases; + } + + /** + * Resolves a node ID from a module name or node ID. + * + * @param $mixed string|int + * Node machine name or ID. + * @return int + */ + public function getModuleNodeId($mixed) { + return is_integer($mixed) ? $mixed : $this->getModule($mixed)->getNid(); + } + + /** + * Returns a module name from a string or a node ID. + * + * @param $mixed string|int + * Node machine name or ID. + * @return string + */ + public function getModuleName($mixed) { + if (!is_integer($mixed)) { + return $mixed; + } + else { + // @var $node ProjectModule + $node = $this->getNode($mixed); + return $node->getMachineName(); + } + } + + /** + * Get all of the projects, and optionally filter by some fields. + * + * @param array $options + * Here, you can place any extra fields by which to filter the results. + * @return ResourceList + * @see https://www.drupal.org/api + */ + public function getProjects(array $options = array()) { + $options = ['type' => 'project_module'] + $options; + return $this->getPagedResource('node', $options); + } +}