From d9cc7462179d2888d57ff00c566049d3b28cc561 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sat, 16 May 2015 14:20:07 +0100 Subject: [PATCH] Fix CS issues --- src/Thujohn/Analytics/Analytics.php | 163 ++++++++++-------- src/Thujohn/Analytics/AnalyticsFacade.php | 26 +-- .../Analytics/AnalyticsServiceProvider.php | 116 ++++++------- src/config/config.php | 92 +++++----- 4 files changed, 209 insertions(+), 188 deletions(-) diff --git a/src/Thujohn/Analytics/Analytics.php b/src/Thujohn/Analytics/Analytics.php index 86c1a8e..4127f76 100644 --- a/src/Thujohn/Analytics/Analytics.php +++ b/src/Thujohn/Analytics/Analytics.php @@ -1,50 +1,62 @@ -setClient($client); - $this->setService($client); - } +class Analytics +{ + protected $client; + protected $service; + private $site_ids = []; - public function getClient() { - return $this->client; - } + public function __construct(\Google_Client $client) + { + $this->setClient($client); + $this->setService($client); + } - public function setClient(\Google_Client $client) { - $this->client = $client; + public function getClient() + { + return $this->client; + } - return $this; - } + public function setClient(\Google_Client $client) + { + $this->client = $client; - public function getService() { - return $this->service; - } + return $this; + } - public function setService(\Google_Client $client) { - $this->service = new \Google_Service_Analytics($client); + public function getService() + { + return $this->service; + } - return $this; - } + public function setService(\Google_Client $client) + { + $this->service = new \Google_Service_Analytics($client); - public function query($id, $start_date, $end_date, $metrics, $others = array()) { - return $this->service->data_ga->get($id, $start_date, $end_date, $metrics, $others); - } + return $this; + } + + public function query($id, $start_date, $end_date, $metrics, $others = []) + { + return $this->service->data_ga->get($id, $start_date, $end_date, $metrics, $others); + } /** * Runs analytics query calls in batch mode * It accepts an array of queries as specified by the parameters of the Analytics::query function - * With an additional optional parameter named key, which is used to identify the results for a specific object + * With an additional optional parameter named key, which is used to identify the results for a specific object. * * Returns an array with object keys as response-KEY where KEY is the key you specified or a random key returned * from analytics. + * * @param array $queries + * * @return array|null */ - public function batchQueries(array $queries) { + public function batchQueries(array $queries) + { /* * Set the client to use batch mode @@ -60,7 +72,7 @@ public function batchQueries(array $queries) { $key = array_pull($query, 'key'); // call the original query method to get the request object - $req = call_user_func_array(__NAMESPACE__ .'\Analytics::query' ,$query); + $req = call_user_func_array(__NAMESPACE__.'\Analytics::query', $query); $batch->add($req, $key); } @@ -73,46 +85,53 @@ public function batchQueries(array $queries) { return $results; } - public function segments() { - return $this->service->management_segments; - } - - public function accounts() { - return $this->service->management_accounts; - } - - public function goals() { - return $this->service->management_goals; - } - - public function profiles() { - return $this->service->management_profiles; - } - - public function webproperties() { - return $this->service->management_webproperties; - } - - public function getAllSitesIds() { - if (empty($this->site_ids)) { - $sites = $this->service->management_profiles->listManagementProfiles("~all", "~all"); - foreach($sites['items'] as $site) { - $this->site_ids[$site['websiteUrl']] = 'ga:' . $site['id']; - } - } - - return $this->site_ids; - } - - public function getSiteIdByUrl($url) { - if (!isset($this->site_ids[$url])) { - $this->getAllSitesIds(); - } - - if (isset($this->site_ids[$url])) { - return $this->site_ids[$url]; - } - - throw new \Exception("Site $url is not present in your Analytics account."); - } -} \ No newline at end of file + public function segments() + { + return $this->service->management_segments; + } + + public function accounts() + { + return $this->service->management_accounts; + } + + public function goals() + { + return $this->service->management_goals; + } + + public function profiles() + { + return $this->service->management_profiles; + } + + public function webproperties() + { + return $this->service->management_webproperties; + } + + public function getAllSitesIds() + { + if (empty($this->site_ids)) { + $sites = $this->service->management_profiles->listManagementProfiles('~all', '~all'); + foreach ($sites['items'] as $site) { + $this->site_ids[$site['websiteUrl']] = 'ga:'.$site['id']; + } + } + + return $this->site_ids; + } + + public function getSiteIdByUrl($url) + { + if (!isset($this->site_ids[$url])) { + $this->getAllSitesIds(); + } + + if (isset($this->site_ids[$url])) { + return $this->site_ids[$url]; + } + + throw new \Exception("Site $url is not present in your Analytics account."); + } +} diff --git a/src/Thujohn/Analytics/AnalyticsFacade.php b/src/Thujohn/Analytics/AnalyticsFacade.php index 277448b..722d19b 100644 --- a/src/Thujohn/Analytics/AnalyticsFacade.php +++ b/src/Thujohn/Analytics/AnalyticsFacade.php @@ -1,14 +1,18 @@ -package('thujohn/analytics'); - } +class AnalyticsServiceProvider extends ServiceProvider +{ + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->app->bind('Thujohn\Analytics\Analytics', function ($app) { - if(!\File::exists($app['config']->get('analytics::certificate_path'))) - { - throw new \Exception("Can't find the .p12 certificate in: " . $app['config']->get('analytics::certificate_path')); - } + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + $this->package('thujohn/analytics'); + } - $config = array( - 'oauth2_client_id' => $app['config']->get('analytics::client_id'), - 'use_objects' => $app['config']->get('analytics::use_objects'), - ); + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->bind('Thujohn\Analytics\Analytics', function ($app) { + if (!\File::exists($app['config']->get('analytics::certificate_path'))) { + throw new \Exception("Can't find the .p12 certificate in: ".$app['config']->get('analytics::certificate_path')); + } - $client = new \Google_Client($config); + $config = [ + 'oauth2_client_id' => $app['config']->get('analytics::client_id'), + 'use_objects' => $app['config']->get('analytics::use_objects'), + ]; - $client->setAccessType('offline'); + $client = new \Google_Client($config); - $client->setAssertionCredentials( - new \Google_Auth_AssertionCredentials( - $app['config']->get('analytics::service_email'), - array('https://www.googleapis.com/auth/analytics.readonly'), - file_get_contents($app['config']->get('analytics::certificate_path')) - ) - ); + $client->setAccessType('offline'); - return new Analytics($client); - }); + $client->setAssertionCredentials( + new \Google_Auth_AssertionCredentials( + $app['config']->get('analytics::service_email'), + ['https://www.googleapis.com/auth/analytics.readonly'], + file_get_contents($app['config']->get('analytics::certificate_path')) + ) + ); - $this->app->singleton('analytics', 'Thujohn\Analytics\Analytics'); - } + return new Analytics($client); + }); - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array('analytics'); - } + $this->app->singleton('analytics', 'Thujohn\Analytics\Analytics'); + } -} \ No newline at end of file + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return ['analytics']; + } +} diff --git a/src/config/config.php b/src/config/config.php index daecb43..7ef3e78 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -1,4 +1,5 @@ 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com', - +return [ - /* - |-------------------------------------------------------------------------- - | Service Account Name - |-------------------------------------------------------------------------- - | - | Set your service account name, it should look like: - | xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com - | - */ - 'service_email' => 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com', + /* + |-------------------------------------------------------------------------- + | Client ID + |-------------------------------------------------------------------------- + | + | Set your client id, it should look like: + | xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com + | + */ + 'client_id' => 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com', - /* - |-------------------------------------------------------------------------- - | Path to the .p12 certificate - |-------------------------------------------------------------------------- - | - | You need to download this from the Google API Console when the - | service account was created. - | - | Make sure you keep your key.p12 file in a secure location, and isn't - | readable by others. - | - */ + /* + |-------------------------------------------------------------------------- + | Service Account Name + |-------------------------------------------------------------------------- + | + | Set your service account name, it should look like: + | xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com + | + */ + 'service_email' => 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com', - 'certificate_path' => __DIR__ . '/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12', + /* + |-------------------------------------------------------------------------- + | Path to the .p12 certificate + |-------------------------------------------------------------------------- + | + | You need to download this from the Google API Console when the + | service account was created. + | + | Make sure you keep your key.p12 file in a secure location, and isn't + | readable by others. + | + */ + 'certificate_path' => __DIR__.'/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12', - /* - |-------------------------------------------------------------------------- - | Returns objects - |-------------------------------------------------------------------------- - | - | Returns objects of the Google API Service instead of associative arrays - | - */ + /* + |-------------------------------------------------------------------------- + | Returns objects + |-------------------------------------------------------------------------- + | + | Returns objects of the Google API Service instead of associative arrays + | + */ - 'use_objects' => true, -); + 'use_objects' => true, +];