Permalink
Browse files

Tweaks across the board for JGoogle.

  • Loading branch information...
1 parent fa52de8 commit 5d3872fa7a1f90c6db0284913e7c690ecd1c65b7 @aaronschmitz aaronschmitz committed with LouisLandry Aug 14, 2012
@@ -14,13 +14,13 @@
*
* @package Joomla.Platform
* @subpackage Google
- * @since 1234
+ * @since 12.2
*/
abstract class JGoogleAuth
{
/**
* @var JRegistry Options for the Google authentication object.
- * @since 1234
+ * @since 12.2
*/
protected $options;
@@ -29,7 +29,7 @@
*
* @return bool True on success.
*
- * @since 1234
+ * @since 12.2
*/
abstract public function auth();
@@ -38,7 +38,7 @@
*
* @return bool Is authenticated
*
- * @since 1234
+ * @since 12.2
*/
abstract public function isAuth();
@@ -52,9 +52,9 @@
*
* @return mixed Data from Google.
*
- * @since 1234
+ * @since 12.2
*/
- abstract public function query($url, $data = null, $headers = null, $method = 'post');
+ abstract public function query($url, $data = null, $headers = null, $method = 'get');
/**
* Get an option from the JGoogleAuth object.
@@ -63,7 +63,7 @@
*
* @return mixed The option value.
*
- * @since 1234
+ * @since 12.2
*/
public function getOption($key)
{
@@ -78,7 +78,7 @@ public function getOption($key)
*
* @return JGoogleAuth This object for method chaining.
*
- * @since 1234
+ * @since 12.2
*/
public function setOption($key, $value)
{
@@ -15,13 +15,13 @@
*
* @package Joomla.Platform
* @subpackage Google
- * @since 1234
+ * @since 12.2
*/
class JGoogleAuthOauth2 extends JGoogleAuth
{
/**
* @var JOauthV2client OAuth client for the Google authentication object.
- * @since 1234
+ * @since 12.2
*/
protected $client;
@@ -31,7 +31,7 @@ class JGoogleAuthOauth2 extends JGoogleAuth
* @param JRegistry $options JGoogleAuth options object.
* @param JOauthV2client $client OAuth client for Google authentication.
*
- * @since 1234
+ * @since 12.2
*/
public function __construct(JRegistry $options = null, JOauthV2client $client = null)
{
@@ -44,7 +44,7 @@ public function __construct(JRegistry $options = null, JOauthV2client $client =
*
* @return bool True on success.
*
- * @since 1234
+ * @since 12.2
*/
public function auth()
{
@@ -57,7 +57,7 @@ public function auth()
*
* @return bool Is authenticated
*
- * @since 1234
+ * @since 12.2
*/
public function isAuth()
{
@@ -74,7 +74,7 @@ public function isAuth()
*
* @return mixed Data from Google.
*
- * @since 1234
+ * @since 12.2
*/
public function query($url, $data = null, $headers = null, $method = 'get')
{
@@ -87,7 +87,7 @@ public function query($url, $data = null, $headers = null, $method = 'get')
*
* @return JOauthV2client Google-configured Oauth2 client.
*
- * @since 1234
+ * @since 12.2
*/
protected function googlize()
{
@@ -14,19 +14,19 @@
*
* @package Joomla.Platform
* @subpackage Google
- * @since 1234
+ * @since 12.2
*/
abstract class JGoogleData
{
/**
* @var JRegistry Options for the Google data object.
- * @since 1234
+ * @since 12.2
*/
protected $options;
/**
* @var JGoogleAuth Authentication client for the Google data object.
- * @since 1234
+ * @since 12.2
*/
protected $auth;
@@ -36,7 +36,7 @@
* @param JRegistry $options Google options object.
* @param JGoogleAuth $auth Google data http client object.
*
- * @since 1234
+ * @since 12.2
*/
public function __construct(JRegistry $options = null, JGoogleAuth $auth = null)
{
@@ -49,7 +49,7 @@ public function __construct(JRegistry $options = null, JGoogleAuth $auth = null)
*
* @return bool True on success.
*
- * @since 1234
+ * @since 12.2
*/
public function auth()
{
@@ -61,7 +61,7 @@ public function auth()
*
* @return bool True if authenticated.
*
- * @since 1234
+ * @since 12.2
*/
public function authenticated()
{
@@ -75,7 +75,7 @@ public function authenticated()
*
* @return SimpleXMLElement XMLElement of parsed data
*
- * @since 1234
+ * @since 12.2
* @throws UnexpectedValueException
*/
protected static function safeXML($data)
@@ -140,11 +140,11 @@ protected function listGetData($url, $maxpages = 1, $token = null)
*
* @return mixed Data from Google.
*
- * @since 1234
+ * @since 12.2
*/
- protected function query($url, $data = null, $headers = null, $method = 'post')
+ protected function query($url, $data = null, $headers = null, $method = 'get')
{
- $this->client->query($url, $data, $headers, $method);
+ return $this->auth->query($url, $data, $headers, $method);
}
/**
@@ -154,7 +154,7 @@ protected function query($url, $data = null, $headers = null, $method = 'post')
*
* @return mixed The option value.
*
- * @since 1234
+ * @since 12.2
*/
public function getOption($key)
{
@@ -169,7 +169,7 @@ public function getOption($key)
*
* @return JGoogleData This object for method chaining.
*
- * @since 1234
+ * @since 12.2
*/
public function setOption($key, $value)
{
@@ -28,11 +28,56 @@ class JGoogleDataAdsense extends JGoogleData
*/
public function __construct(JRegistry $options = null, JGoogleAuth $auth = null)
{
+ $options = isset($options) ? $options : new JRegistry;
+ if (!$options->get('scope'))
+ {
+ $options->set('scope', 'https://www.googleapis.com/auth/adsense');
+ }
+ if (isset($auth) && !$auth->getOption('scope'))
+ {
+ $auth->setOption('scope', 'https://www.googleapis.com/auth/adsense');
+ }
+
parent::__construct($options, $auth);
+ }
- if (isset($this->auth) && !$this->auth->getOption('scope'))
+ /**
+ * Method to retrieve a list of AdSense data
+ *
+ * @param array $url URL to GET
+ * @param int $maxpages Maximum number of pages of accounts to return
+ * @param string $token Next page token
+ *
+ * @return mixed Data from Google
+ *
+ * @since 12.2
+ * @throws UnexpectedValueException
+ */
+ protected function listGetData($url, $maxpages = 1, $token = null)
+ {
+ $qurl = $url;
+ if (strpos($url, '&'))
+ {
+ $qurl .= '&pageToken=' . $token;
+ }
+ else
{
- $this->auth->setOption('scope', 'https://www.googleapis.com/auth/adsense');
+ $qurl .= 'pageToken=' . $token;
+ }
+ $jdata = $this->query($qurl);
+ $data = json_decode($jdata->body, true);
+
+ if ($data && array_key_exists('items', $data))
+ {
+ if ($maxpages != 1 && array_key_exists('nextPageToken', $data))
+ {
+ $data['items'] = array_merge($data['items'], $this->listGetData($url, $maxpages - 1, $data['nextPageToken']));
+ }
+ return $data['items'];
+ }
+ else
+ {
+ throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
}
}
@@ -84,7 +129,7 @@ public function listAccounts($options = array(), $maxpages = 1)
{
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
- $url = 'https://www.googleapis.com/adsense/v1.1/accounts?' . http_build_query($options);
+ $url = 'https://www.googleapis.com/adsense/v1.1/accounts?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
@@ -111,7 +156,7 @@ public function listClients($accountID, $options = array(), $maxpages = 1)
{
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
- $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients?' . http_build_query($options);
+ $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
@@ -173,7 +218,7 @@ public function listUnitChannels($accountID, $adclientID, $adunitID, $options =
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID;
- $url .= '/adclients/' . $adclientID . '/adunits/' . $adunitID . '/customchannels?' . http_build_query($options);
+ $url .= '/adclients/' . $adclientID . '/adunits/' . $adunitID . '/customchannels?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
@@ -234,7 +279,7 @@ public function listChannels($accountID, $adclientID, $options = array(), $maxpa
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients/' . $adclientID;
- $url .= '/customchannels?' . http_build_query($options);
+ $url .= '/customchannels?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
@@ -264,7 +309,7 @@ public function listChannelUnits($accountID, $adclientID, $channelID, $options =
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients/' . $adclientID;
- $url .= '/customchannels/' . $channelID . '/adunits?' . http_build_query($options);
+ $url .= '/customchannels/' . $channelID . '/adunits?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
@@ -293,7 +338,7 @@ public function listUrlChannels($accountID, $adclientID, $options = array(), $ma
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID;
- $url .= '/adclients/' . $adclientID . '/urlchannels?' . http_build_query($options);
+ $url .= '/adclients/' . $adclientID . '/urlchannels?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
@@ -347,7 +392,7 @@ public function generateReport($accountID, $start, $end = false, $options = arra
$endobj = new DateTime;
$endobj->setTimestamp($end);
}
- elseif (is_string($end))
+ elseif (is_string($start))
{
$endobj = new DateTime($end);
}
@@ -366,7 +411,7 @@ public function generateReport($accountID, $start, $end = false, $options = arra
$begin = array_key_exists('startIndex', $options) ? $options['startIndex'] : 0;
unset($options['startIndex']);
- $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/reports?' . http_build_query($options);
+ $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/reports?' . implode('&', $options);
if (strpos($url, '&'))
{
$url .= '&';
Oops, something went wrong.

0 comments on commit 5d3872f

Please sign in to comment.