Exemplo n.º 1
0
 public function testDeleteConfig()
 {
     $this->createConfig();
     self::$client->request('DELETE', $this->componentName . '/configs/test');
     /* @var Response $response */
     $response = self::$client->getResponse();
     $accounts = $this->configuration->getAccounts(true);
     $this->assertEquals(204, $response->getStatusCode());
     $this->assertArrayNotHasKey($this->accountId, $accounts);
 }
Exemplo n.º 2
0
 public function run($options = null)
 {
     $this->dataManager = new DataManager($this->configuration, $this->temp);
     $accounts = $this->configuration->getAccounts();
     if (isset($options['account']) || isset($options['config'])) {
         $accountId = isset($options['account']) ? $options['account'] : $options['config'];
         if (!isset($accounts[$accountId])) {
             throw new ConfigurationException("Account '" . $accountId . "' does not exist.");
         }
         $accounts = array($accountId => $accounts[$accountId]);
     }
     if (isset($options['sheetId'])) {
         if (!isset($options['config']) && !isset($options['account'])) {
             throw new UserException("Missing parameter 'config'");
         }
         if (!isset($options['googleId'])) {
             throw new UserException("Missing parameter 'googleId'");
         }
     }
     $status = array();
     /** @var Account $account */
     foreach ($accounts as $accountId => $account) {
         $this->currAccountId = $accountId;
         $this->driveApi->getApi()->setCredentials($account->getAccessToken(), $account->getRefreshToken());
         $this->driveApi->getApi()->setRefreshTokenCallback(array($this, 'refreshTokenCallback'));
         $sheets = $account->getSheets();
         if (isset($options['sheetId'])) {
             $sheets = [$account->getSheet($options['googleId'], $options['sheetId'])];
         }
         /** @var Sheet $sheet */
         foreach ($sheets as $sheet) {
             $this->logger->info('Importing sheet ' . $sheet->getSheetTitle());
             try {
                 $meta = $this->driveApi->getFile($sheet->getGoogleId());
             } catch (RequestException $e) {
                 if ($e->getResponse()->getStatusCode() == 404) {
                     throw new UserException(sprintf("File '%s' not found in Google Drive", $sheet->getTitle()), $e);
                 } else {
                     $userException = new UserException("Google Drive Error: " . $e->getMessage(), $e);
                     $userException->setData(array('message' => $e->getMessage(), 'reason' => $e->getResponse()->getReasonPhrase(), 'account' => $accountId, 'sheet' => $sheet->toArray()));
                     throw $userException;
                 }
             }
             if (!isset($meta['exportLinks'])) {
                 $e = new ApplicationException("ExportLinks missing in file resource");
                 $e->setData(['fileMetadata' => $meta]);
                 throw $e;
             }
             if (isset($meta['exportLinks']['text/csv'])) {
                 $exportLink = $meta['exportLinks']['text/csv'] . '&gid=' . $sheet->getSheetId();
             } else {
                 $exportLink = str_replace('pdf', 'csv', $meta['exportLinks']['application/pdf']) . '&gid=' . $sheet->getSheetId();
             }
             try {
                 $data = $this->driveApi->export($exportLink);
                 if ($data->getSize() > 0) {
                     $this->dataManager->save($data, $sheet);
                 } else {
                     $this->logger->warning(sprintf("Sheet is empty. File: '%s', Sheet: '%s'.", $sheet->getTitle(), $sheet->getSheetTitle()));
                     $status[$accountId][$sheet->getSheetTitle()] = "file is empty";
                 }
             } catch (RequestException $e) {
                 $userException = new UserException("Error importing file - sheet: '" . $sheet->getTitle() . " - " . $sheet->getSheetTitle() . "'. ", $e);
                 $userException->setData(array('message' => $e->getMessage(), 'reason' => $e->getResponse()->getReasonPhrase(), 'body' => substr($e->getResponse()->getBody(), 0, 300), 'account' => $accountId, 'sheet' => $sheet->toArray()));
                 throw $userException;
             }
         }
     }
     return array("status" => "ok", "sheets" => $status);
 }