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