Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
object.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage GitHub
5  *
6  * @copyright Copyright (C) 2005 - 2013 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 object class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage GitHub
17  * @since 11.3
18  */
19 abstract class JGithubObject
20 {
21  /**
22  * @var JRegistry Options for the GitHub object.
23  * @since 11.3
24  */
25  protected $options;
26 
27  /**
28  * @var JGithubHttp The HTTP client object to use in sending HTTP requests.
29  * @since 11.3
30  */
31  protected $client;
32 
33  /**
34  * Constructor.
35  *
36  * @param JRegistry $options GitHub options object.
37  * @param JGithubHttp $client The HTTP client object.
38  *
39  * @since 11.3
40  */
41  public function __construct(JRegistry $options = null, JGithubHttp $client = null)
42  {
43  $this->options = isset($options) ? $options : new JRegistry;
44  $this->client = isset($client) ? $client : new JGithubHttp($this->options);
45  }
46 
47  /**
48  * Method to build and return a full request URL for the request. This method will
49  * add appropriate pagination details if necessary and also prepend the API url
50  * to have a complete URL for the request.
51  *
52  * @param string $path URL to inflect
53  * @param integer $page Page to request
54  * @param integer $limit Number of results to return per page
55  *
56  * @return string The request URL.
57  *
58  * @since 11.3
59  */
60  protected function fetchUrl($path, $page = 0, $limit = 0)
61  {
62  // Get a new JUri object fousing the api url and given path.
63  $uri = new JUri($this->options->get('api.url') . $path);
64 
65  if ($this->options->get('gh.token', false))
66  {
67  // Use oAuth authentication - @todo set in request header ?
68  $uri->setVar('access_token', $this->options->get('gh.token'));
69  }
70  else
71  {
72  // Use basic authentication
73  if ($this->options->get('api.username', false))
74  {
75  $uri->setUser($this->options->get('api.username'));
76  }
77 
78  if ($this->options->get('api.password', false))
79  {
80  $uri->setPass($this->options->get('api.password'));
81  }
82  }
83 
84  // If we have a defined page number add it to the JUri object.
85  if ($page > 0)
86  {
87  $uri->setVar('page', (int) $page);
88  }
89 
90  // If we have a defined items per page add it to the JUri object.
91  if ($limit > 0)
92  {
93  $uri->setVar('per_page', (int) $limit);
94  }
95 
96  return (string) $uri;
97  }
98 
99  /**
100  * Process the response and decode it.
101  *
102  * @param JHttpResponse $response The response.
103  * @param integer $expectedCode The expected "good" code.
104  *
105  * @throws DomainException
106  *
107  * @return mixed
108  */
109  protected function processResponse(JHttpResponse $response, $expectedCode = 200)
110  {
111  // Validate the response code.
112  if ($response->code != $expectedCode)
113  {
114  // Decode the error response and throw an exception.
115  $error = json_decode($response->body);
116  throw new DomainException($error->message, $response->code);
117  }
118 
119  return json_decode($response->body);
120  }
121 }