protected function execute(InputInterface $input, OutputInterface $output) { $sapiToken = $input->getArgument('sapiToken'); $storageApi = new Client(['token' => $sapiToken, 'userAgent' => $this->componentName]); $encryptionKey = $input->getArgument('encryptionKey'); $encryptor = new Encryptor($encryptionKey); // Init new SYS bucket $configuration = new Configuration($this->componentName, $encryptor); $configuration->setStorageApi($storageApi); try { $configuration->create(); } catch (\Exception $e) { // do nothing, bucket probably exists } // Get configuration $sysBucketId = 'sys.c-ex-google-analytics'; if (!$storageApi->bucketExists($sysBucketId)) { throw new \Exception("No SYS bucket found"); } $accounts = $configuration->getAccounts(); /** @var Account $account */ foreach ($accounts as $account) { $cfg = $this->renameItems($account->getConfiguration()); $account->setConfiguration($cfg); $account->save(); } }
/** * Accounts */ public function testPostAccount() { $this->createConfig(); self::$client->request('POST', '/ex-google-analytics/account/test', array(), array(), array(), json_encode(array('googleId' => '123456', 'googleName' => 'googleTestAccount', 'email' => '*****@*****.**', 'accessToken' => 'accessToken', 'refreshToken' => 'refreshToken', 'configuration' => ['New-Users' => ['name' => 'New-Users', 'metrics' => ['users'], 'dimensions' => []]]))); $responseJson = self::$client->getResponse()->getContent(); $response = json_decode($responseJson, true); $this->assertEquals('test', $response['id']); $accounts = $this->configuration->getAccounts(true); $account = $accounts['test']; $this->assertArrayHasKey('New-Users', $account['configuration']); $this->assertEquals('New-Users', $account['configuration']['New-Users']['name']); $this->assertAccount($account); }
public function run($options = null) { $accounts = $this->configuration->getAccounts(); $status = array(); $dateFrom = isset($options['since']) ? date('Y-m-d', strtotime($options['since'])) : date('Y-m-d', strtotime('-4 days')); $dateTo = isset($options['until']) ? date('Y-m-d', strtotime($options['until'])) : date('Y-m-d', strtotime('-1 day')); $dataset = isset($options['dataset']) ? $options['dataset'] : null; if (isset($options['account'])) { if (!isset($accounts[$options['account']])) { throw new UserException(sprintf("Account '%s' does not exist.", $options['account'])); } $accounts = array($options['account'] => $accounts[$options['account']]); } if (isset($options['config'])) { if (!isset($accounts[$options['config']])) { throw new UserException(sprintf("Config '%s' does not exist.", $options['config'])); } $accounts = array($options['config'] => $accounts[$options['config']]); } if ($dataset != null && !isset($options['config']) && !isset($options['account'])) { throw new UserException("Missing parameter 'config'"); } /** @var Account $account */ foreach ($accounts as $accountId => $account) { // check if account has been authorized if (null == $account->getAttribute('accessToken')) { continue; } $this->currAccountId = $accountId; if (null == $account->getAttribute('outputBucket')) { $this->configuration->initDataBucket($account->getAccountId()); } else { if (!$this->configuration->getStorageApi()->bucketExists($account->getAttribute('outputBucket'))) { $outBucketArr = explode('.', $account->getAttribute('outputBucket')); $this->configuration->getStorageApi()->createBucket(str_replace('c-', '', $outBucketArr[1]), $outBucketArr[0], 'Google Analytics data bucket'); } } $this->gaApi->getApi()->setCredentials($account->getAccessToken(), $account->getRefreshToken()); $this->gaApi->getApi()->setRefreshTokenCallback(array($this, 'refreshTokenCallback')); $tmpFileInfo = $this->temp->createFile("profiles-" . $accountId . "-" . microtime() . ".csv"); $profilesCsv = new CsvFile($tmpFileInfo->getPathname()); $profilesCsv->writeRow(array('id', 'name')); /** @var Profile $profile */ foreach ($account->getProfiles() as $profile) { $profilesCsv->writeRow(array($profile->getGoogleId(), $profile->getName())); try { $configuration = $account->getConfiguration(); // Download just the dataset specified in request if ($dataset != null) { if (!isset($configuration[$dataset])) { throw new UserException(sprintf("Dataset '%s' doesn't exist", $dataset)); } $configuration = [$dataset => $configuration[$dataset]]; } foreach ($configuration as $tableName => $cfg) { // Download dataset only for specific profile if (isset($cfg['profile']) && $cfg['profile'] != $profile->getGoogleId()) { continue; } $antisampling = isset($cfg['antisampling']) ? $cfg['antisampling'] : false; $this->getData($account, $profile, $tableName, $dateFrom, $dateTo, $antisampling); $status[$accountId][$profile->getName()][$tableName] = 'ok'; } } catch (RequestException $e) { if ($e->getCode() == 401) { throw new UserException("Expired or wrong credentials, please reauthorize.", $e); } if ($e->getCode() == 403) { if (strtolower($e->getResponse()->getReasonPhrase()) == 'forbidden') { $this->logger->warning("You don't have access to Google Analytics resource. Probably you don't have access to profile, or profile doesn't exists anymore."); continue; } else { throw new UserException("Reason: " . $e->getResponse()->getReasonPhrase(), $e); } } if ($e->getCode() == 400) { throw new UserException($e->getMessage()); } if ($e->getCode() == 503) { throw new UserException("Google API error: " . $e->getMessage(), $e); } throw new ApplicationException($e->getResponse()->getBody(), $e); } } $this->getDataManager()->uploadCsv($profilesCsv->getPathname(), $this->getOutputTable($account, 'profiles')); } return $status; }