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 use Joomla\Registry\Registry;
 13 
 14 /**
 15  * GitHub API object class for the Joomla Platform.
 16  *
 17  * @since       11.3
 18  * @deprecated  4.0  Use the `joomla/github` package via Composer instead
 19  */
 20 abstract class JGithubObject
 21 {
 22     /**
 23      * @var    Registry  Options for the GitHub object.
 24      * @since  11.3
 25      */
 26     protected $options;
 27 
 28     /**
 29      * @var    JGithubHttp  The HTTP client object to use in sending HTTP requests.
 30      * @since  11.3
 31      */
 32     protected $client;
 33 
 34     /**
 35      * Constructor.
 36      *
 37      * @param   Registry     $options  GitHub options object.
 38      * @param   JGithubHttp  $client   The HTTP client object.
 39      *
 40      * @since   11.3
 41      */
 42     public function __construct(Registry $options = null, JGithubHttp $client = null)
 43     {
 44         $this->options = isset($options) ? $options : new Registry;
 45         $this->client  = isset($client) ? $client : new JGithubHttp($this->options);
 46     }
 47 
 48     /**
 49      * Method to build and return a full request URL for the request.  This method will
 50      * add appropriate pagination details if necessary and also prepend the API url
 51      * to have a complete URL for the request.
 52      *
 53      * @param   string   $path   URL to inflect
 54      * @param   integer  $page   Page to request
 55      * @param   integer  $limit  Number of results to return per page
 56      *
 57      * @return  string   The request URL.
 58      *
 59      * @since   11.3
 60      */
 61     protected function fetchUrl($path, $page = 0, $limit = 0)
 62     {
 63         // Get a new JUri object fousing the api url and given path.
 64         $uri = new JUri($this->options->get('api.url') . $path);
 65 
 66         if ($this->options->get('gh.token', false))
 67         {
 68             // Use oAuth authentication - @todo set in request header ?
 69             $uri->setVar('access_token', $this->options->get('gh.token'));
 70         }
 71         else
 72         {
 73             // Use basic authentication
 74             if ($this->options->get('api.username', false))
 75             {
 76                 $username = $this->options->get('api.username');
 77                 $username = str_replace('@', '%40', $username);
 78                 $uri->setUser($username);
 79             }
 80 
 81             if ($this->options->get('api.password', false))
 82             {
 83                 $password = $this->options->get('api.password');
 84                 $password = str_replace('@', '%40', $password);
 85                 $uri->setPass($password);
 86             }
 87         }
 88 
 89         // If we have a defined page number add it to the JUri object.
 90         if ($page > 0)
 91         {
 92             $uri->setVar('page', (int) $page);
 93         }
 94 
 95         // If we have a defined items per page add it to the JUri object.
 96         if ($limit > 0)
 97         {
 98             $uri->setVar('per_page', (int) $limit);
 99         }
100 
101         return (string) $uri;
102     }
103 
104     /**
105      * Process the response and decode it.
106      *
107      * @param   JHttpResponse  $response      The response.
108      * @param   integer        $expectedCode  The expected "good" code.
109      * @param   boolean        $decode        If the should be response be JSON decoded.
110      *
111      * @throws DomainException
112      * @since  12.4
113      *
114      * @return mixed
115      */
116     protected function processResponse(JHttpResponse $response, $expectedCode = 200, $decode = true)
117     {
118         // Validate the response code.
119         if ($response->code == $expectedCode)
120         {
121             return ($decode) ? json_decode($response->body) : $response->body;
122         }
123 
124         // Decode the error response and throw an exception.
125         $error   = json_decode($response->body);
126         $message = (isset($error->message)) ? $error->message : 'Error: ' . $response->code;
127 
128         throw new DomainException($message, $response->code);
129     }
130 }
131