1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  GitHub
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 /**
 13  * GitHub API References class for the Joomla Platform.
 14  *
 15  * @documentation https://developer.github.com/v3/repos/statuses
 16  *
 17  * @since       12.3
 18  * @deprecated  4.0  Use the `joomla/github` package via Composer instead
 19  */
 20 class JGithubPackageRepositoriesStatuses extends JGithubPackage
 21 {
 22     /**
 23      * Method to create a status.
 24      *
 25      * @param   string  $user         The name of the owner of the GitHub repository.
 26      * @param   string  $repo         The name of the GitHub repository.
 27      * @param   string  $sha          The SHA1 value for which to set the status.
 28      * @param   string  $state        The state (pending, success, error or failure).
 29      * @param   string  $targetUrl    Optional target URL.
 30      * @param   string  $description  Optional description for the status.
 31      *
 32      * @throws InvalidArgumentException
 33      * @throws DomainException
 34      *
 35      * @since   12.3
 36      *
 37      * @return  object
 38      */
 39     public function create($user, $repo, $sha, $state, $targetUrl = null, $description = null)
 40     {
 41         // Build the request path.
 42         $path = '/repos/' . $user . '/' . $repo . '/statuses/' . $sha;
 43 
 44         if (!in_array($state, array('pending', 'success', 'error', 'failure')))
 45         {
 46             throw new InvalidArgumentException('State must be one of pending, success, error or failure.');
 47         }
 48 
 49         // Build the request data.
 50         $data = array(
 51             'state' => $state,
 52         );
 53 
 54         if (!is_null($targetUrl))
 55         {
 56             $data['target_url'] = $targetUrl;
 57         }
 58 
 59         if (!is_null($description))
 60         {
 61             $data['description'] = $description;
 62         }
 63 
 64         // Send the request.
 65         $response = $this->client->post($this->fetchUrl($path), json_encode($data));
 66 
 67         // Validate the response code.
 68         if ($response->code != 201)
 69         {
 70             // Decode the error response and throw an exception.
 71             $error = json_decode($response->body);
 72             throw new DomainException($error->message, $response->code);
 73         }
 74 
 75         return json_decode($response->body);
 76     }
 77 
 78     /**
 79      * Method to list statuses for an SHA.
 80      *
 81      * @param   string  $user  The name of the owner of the GitHub repository.
 82      * @param   string  $repo  The name of the GitHub repository.
 83      * @param   string  $sha   SHA1 for which to get the statuses.
 84      *
 85      * @return  array
 86      *
 87      * @since   12.3
 88      */
 89     public function getList($user, $repo, $sha)
 90     {
 91         // Build the request path.
 92         $path = '/repos/' . $user . '/' . $repo . '/statuses/' . $sha;
 93 
 94         // Send the request.
 95         $response = $this->client->get($this->fetchUrl($path));
 96 
 97         // Validate the response code.
 98         if ($response->code != 200)
 99         {
100             // Decode the error response and throw an exception.
101             $error = json_decode($response->body);
102             throw new DomainException($error->message, $response->code);
103         }
104 
105         return json_decode($response->body);
106     }
107 }
108