/**
  * @return Configuration
  */
 public function getConfiguration()
 {
     if ($this->configuration == null) {
         $this->configuration = $this->container->get('ex_google_analytics.configuration');
         $this->configuration->setStorageApi($this->storageApi);
     }
     return $this->configuration;
 }
 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();
     }
 }
 protected function setUp()
 {
     self::$client = static::createClient();
     $container = self::$client->getContainer();
     $sapiToken = $container->getParameter('storage_api.test.token');
     $sapiUrl = $container->getParameter('storage_api.test.url');
     self::$client->setServerParameters(array('HTTP_X-StorageApi-Token' => $sapiToken));
     $this->storageApi = new SapiClient(['token' => $sapiToken, 'url' => $sapiUrl, 'userAgent' => 'ex-google-analytics']);
     /** @var Encryptor $encryptor */
     $encryptor = $container->get('syrup.encryptor');
     $this->configuration = new Configuration($this->componentName, $encryptor);
     $this->configuration->setStorageApi($this->storageApi);
     try {
         $this->configuration->create();
     } catch (\Exception $e) {
         // bucket exists
     }
     // Cleanup
     $sysBucketId = $this->configuration->getSysBucketId();
     $tableId = $sysBucketId . '.' . 'test';
     if ($this->storageApi->tableExists($tableId)) {
         $this->storageApi->dropTable($tableId);
     }
 }
 protected function initConfiguration()
 {
     $this->configuration->setStorageApi($this->storageApi);
     return $this->configuration;
 }
 public function oauthCallbackAction()
 {
     /** @var Session $session */
     $session = $this->get('session');
     $googleApi = $this->getGoogleApi();
     $token = $session->get('token');
     $accountId = $session->get('account');
     $referrer = $session->get('referrer');
     $session->clear();
     $code = $this->get('request')->query->get('code');
     if (empty($token) || empty($accountId)) {
         throw new UserException('Auth session expired');
     }
     if (empty($code)) {
         throw new UserException('Could not read from Google API');
     }
     try {
         $storageApi = new StorageApi(['token' => $token, 'userAgent' => 'ex-google-analytics']);
         $tokenData = $storageApi->verifyToken();
         /** @var EncryptorInterface $encryptor */
         $encryptor = $this->get('syrup.encryptor');
         $configuration = new Configuration('ex-google-analytics', $encryptor);
         $configuration->setStorageApi($storageApi);
         $tokens = $googleApi->authorize($code, $this->container->get('router')->generate('keboola_google_analytics_oauth_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL));
         $googleApi->setCredentials($tokens['access_token'], $tokens['refresh_token']);
         $userData = json_decode($googleApi->request('/oauth2/v2/userinfo')->getBody(), true);
         /** @var Account $account */
         $account = $configuration->getAccountBy('accountId', $accountId);
         if (null == $account) {
             throw new ConfigurationException("Account doesn't exist");
         }
         $userName = isset($userData['name']) ? $userData['name'] : $userData['displayName'];
         $userEmail = isset($userData['email']) ? $userData['email'] : $userData['emails'][0]['value'];
         $account->setGoogleId($userData['id'])->setGoogleName($userName)->setEmail($userEmail)->setAccessToken($tokens['access_token'])->setRefreshToken($tokens['refresh_token'])->setOwner($tokenData['description']);
         if ($account->isExternal()) {
             if ($userEmail == $tokenData['description'] || !isset($tokenData['creatorToken'])) {
                 // user generated an external link for himself or is reauthorizing himself into his config which was external before
                 $account->setExternal(false);
             } else {
                 $account->setOwner($tokenData['creatorToken']['description']);
             }
         }
         $account->save();
         if ($referrer) {
             return new RedirectResponse($referrer);
         } else {
             return new JsonResponse(array('status' => 'ok'));
         }
     } catch (ClientException $e) {
         // SAPI exception - probably invalid access token
         throw new UserException($e->getMessage());
     } catch (\Exception $e) {
         // any other exception
         throw new ApplicationException('Error finishing authorization: ' . $e->getMessage(), $e);
     }
 }