PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Joomla/Twitter/Object.php

https://github.com/piotr-cz/joomla-framework
PHP | 228 lines | 97 code | 26 blank | 105 comment | 11 complexity | 2183146fadd73a0f717b135c9cf40f5e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Part of the Joomla Framework Twitter Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Twitter;
  9. use Joomla\Uri\Uri;
  10. use Joomla\Http\Http;
  11. /**
  12. * Twitter API object class for the Joomla Framework.
  13. *
  14. * @since 1.0
  15. */
  16. abstract class Object
  17. {
  18. /**
  19. * @var array Options for the Twitter object.
  20. * @since 1.0
  21. */
  22. protected $options;
  23. /**
  24. * @var Http The HTTP client object to use in sending HTTP requests.
  25. * @since 1.0
  26. */
  27. protected $client;
  28. /**
  29. * @var OAuth The OAuth client.
  30. * @since 1.0
  31. */
  32. protected $oauth;
  33. /**
  34. * Constructor.
  35. *
  36. * @param array &$options Twitter options array.
  37. * @param Http $client The HTTP client object.
  38. * @param OAuth $oauth The OAuth client.
  39. *
  40. * @since 1.0
  41. */
  42. public function __construct(&$options, Http $client, OAuth $oauth)
  43. {
  44. $this->options = $options;
  45. $this->client = $client;
  46. $this->oauth = $oauth;
  47. }
  48. /**
  49. * Method to check the rate limit for the requesting IP address
  50. *
  51. * @param string $resource A resource or a comma-separated list of resource families you want to know the current rate limit disposition for.
  52. * @param string $action An action for the specified resource, if only one resource is specified.
  53. *
  54. * @return void
  55. *
  56. * @since 1.0
  57. * @throws \RuntimeException
  58. */
  59. public function checkRateLimit($resource = null, $action = null)
  60. {
  61. // Check the rate limit for remaining hits
  62. $rate_limit = $this->getRateLimit($resource);
  63. $property = '/' . $resource;
  64. if (!is_null($action))
  65. {
  66. $property .= '/' . $action;
  67. }
  68. if ($rate_limit->resources->$resource->$property->remaining == 0)
  69. {
  70. // The IP has exceeded the Twitter API rate limit
  71. throw new \RuntimeException('This server has exceed the Twitter API rate limit for the given period. The limit will reset at '
  72. . $rate_limit->resources->$resource->$property->reset
  73. );
  74. }
  75. }
  76. /**
  77. * Method to build and return a full request URL for the request. This method will
  78. * add appropriate pagination details if necessary and also prepend the API url
  79. * to have a complete URL for the request.
  80. *
  81. * @param string $path URL to inflect
  82. * @param array $parameters The parameters passed in the URL.
  83. *
  84. * @return string The request URL.
  85. *
  86. * @since 1.0
  87. */
  88. public function fetchUrl($path, $parameters = null)
  89. {
  90. if ($parameters)
  91. {
  92. foreach ($parameters as $key => $value)
  93. {
  94. if (strpos($path, '?') === false)
  95. {
  96. $path .= '?' . $key . '=' . $value;
  97. }
  98. else
  99. {
  100. $path .= '&' . $key . '=' . $value;
  101. }
  102. }
  103. }
  104. // Get a new Uri object using the api url and given path.
  105. if (strpos($path, 'http://search.twitter.com/search.json') === false)
  106. {
  107. $apiUrl = isset($this->options['api.url']) ? $this->options['api.url'] : null;
  108. $uri = new Uri($apiUrl . $path);
  109. }
  110. else
  111. {
  112. $uri = new Uri($path);
  113. }
  114. return (string) $uri;
  115. }
  116. /**
  117. * Method to retrieve the rate limit for the requesting IP address
  118. *
  119. * @param string $resource A resource or a comma-separated list of resource families you want to know the current rate limit disposition for.
  120. *
  121. * @return array The JSON response decoded
  122. *
  123. * @since 1.0
  124. */
  125. public function getRateLimit($resource)
  126. {
  127. // Build the request path.
  128. $path = '/application/rate_limit_status.json';
  129. if (!is_null($resource))
  130. {
  131. return $this->sendRequest($path, 'GET', array('resources' => $resource));
  132. }
  133. return $this->sendRequest($path);
  134. }
  135. /**
  136. * Method to send the request.
  137. *
  138. * @param string $path The path of the request to make
  139. * @param string $method The request method.
  140. * @param mixed $data Either an associative array or a string to be sent with the post request.
  141. * @param array $headers An array of name-value pairs to include in the header of the request
  142. *
  143. * @return array The decoded JSON response
  144. *
  145. * @since 1.0
  146. * @throws \RuntimeException
  147. */
  148. public function sendRequest($path, $method = 'GET', $data = array(), $headers = array())
  149. {
  150. // Get the access token.
  151. $token = $this->oauth->getToken();
  152. // Set parameters.
  153. $parameters['oauth_token'] = $token['key'];
  154. // Send the request.
  155. $response = $this->oauth->oauthRequest($this->fetchUrl($path), $method, $parameters, $data, $headers);
  156. if (strpos($path, 'update_with_media') !== false)
  157. {
  158. // Check Media Rate Limit.
  159. $response_headers = $response->headers;
  160. if ($response_headers['x-mediaratelimit-remaining'] == 0)
  161. {
  162. // The IP has exceeded the Twitter API media rate limit
  163. throw new \RuntimeException('This server has exceed the Twitter API media rate limit for the given period. The limit will reset in '
  164. . $response_headers['x-mediaratelimit-reset'] . 'seconds.'
  165. );
  166. }
  167. }
  168. if (strpos($response->body, 'redirected') !== false)
  169. {
  170. return $response->headers['Location'];
  171. }
  172. return json_decode($response->body);
  173. }
  174. /**
  175. * Get an option from the Twitter Object instance.
  176. *
  177. * @param string $key The name of the option to get.
  178. *
  179. * @return mixed The option value.
  180. *
  181. * @since 1.0
  182. */
  183. public function getOption($key)
  184. {
  185. return isset($this->options[$key]) ? $this->options[$key] : null;
  186. }
  187. /**
  188. * Set an option for the Twitter Object instance.
  189. *
  190. * @param string $key The name of the option to set.
  191. * @param mixed $value The option value to set.
  192. *
  193. * @return Object This object for method chaining.
  194. *
  195. * @since 1.0
  196. */
  197. public function setOption($key, $value)
  198. {
  199. $this->options[$key] = $value;
  200. return $this;
  201. }
  202. }