public function process(File $file)
 {
     if (null == $file->getGoogleId() || $file->isOperationCreate()) {
         // create new file
         $response = $this->googleDriveApi->insertFile($file);
         // update file with googleId
         $file->setGoogleId($response['id']);
         $this->logger->info("File created", ['file' => $file->toArray(), 'response' => $response]);
     } else {
         // overwrite existing file
         try {
             $response = $this->googleDriveApi->updateFile($file);
             $this->logger->info("File updated", ['file' => $file->toArray(), 'response' => $response]);
         } catch (BadResponseException $e) {
             $statusCode = $e->getResponse()->getStatusCode();
             if ($statusCode == 404) {
                 // file not found - create new one and issue a warning
                 $response = $this->googleDriveApi->insertFile($file);
                 $file->setGoogleId($response['id']);
                 $this->logger->info("File not found, created new one", ['file' => $file->toArray(), 'response' => $response]);
             } else {
                 throw $e;
             }
         }
     }
     return $file;
 }
Пример #2
0
 public function remoteFileExists(File $file)
 {
     if ($file->getGoogleId() == null) {
         return false;
     }
     try {
         $remoteFile = $this->getFile($file->getGoogleId());
         if ($remoteFile['trashed'] === true) {
             return false;
         }
         return true;
     } catch (BadResponseException $e) {
         if ($e->getResponse()->getStatusCode() == 404) {
             return false;
         }
         throw $e;
     }
 }
 public function process(File $file)
 {
     if (null == $file->getGoogleId() || $file->isOperationCreate()) {
         // create new file
         $fileRes = $this->googleDriveApi->insertFile($file);
         // get list of worksheets in file, there shall be only one
         $sheets = $this->googleDriveApi->getWorksheets($fileRes['id']);
         $sheet = array_shift($sheets);
         // update file
         $file->setGoogleId($fileRes['id']);
         $file->setSheetId($sheet['wsid']);
         $this->logger->info("Sheet created", ['file' => $file->toArray()]);
     } else {
         if ($file->isOperationUpdate()) {
             if (null == $file->getSheetId()) {
                 // create new sheet in existing file
                 $response = $this->googleDriveApi->createWorksheet($file);
                 $crawler = new Crawler($response->getBody()->getContents());
                 $uriArr = explode('/', $crawler->filter('default|id')->text());
                 $file->setSheetId(array_pop($uriArr));
                 $this->logger->info("Sheet created in existing file", ['file' => $file->toArray()]);
             } else {
                 // update content of existing file
                 try {
                     // update metadata first - cols and rows count
                     $this->googleDriveApi->updateWorksheet($file);
                     $this->logger->debug("Worksheet metadata updated", ['file' => $file->toArray()]);
                     // update cells content
                     $timestart = microtime(true);
                     $status = $this->googleDriveApi->updateCells($file);
                     $timeend = microtime(true);
                     $apiCallDuration = $timeend - $timestart;
                     if (count($status['errors'])) {
                         $this->logger->warning("Some cells might not be imported properly", ['errors' => $status['errors']]);
                     }
                     $this->logger->debug("Cells updated", ['file' => $file->toArray(), 'apiCallDuration' => $apiCallDuration, 'status' => $status]);
                 } catch (RequestException $e) {
                     $statusCode = $e->getResponse()->getStatusCode();
                     if ($statusCode >= 500 && $statusCode < 600) {
                         throw new UserException(sprintf("Google Drive Server Error: %s - %s. Please try again later.", $statusCode, $e->getResponse()->getReasonPhrase()), $e, ['file' => $file->toArray()]);
                     }
                     throw new UserException("Cells update failed: " . $e->getMessage(), $e, ['file' => $file->toArray()]);
                 }
                 $this->logger->info("Sheet updated", ['file' => $file->toArray()]);
             }
         } else {
             // @TODO: append sheet
         }
     }
     return $file;
 }
 public function getCellsFeed(File $file)
 {
     return json_decode($this->api->request(sprintf(self::SPREADSHEET_CELL, $file->getGoogleId(), $file->getSheetId()) . '?alt=json', 'GET', ['Accept' => 'application/json', 'Content-Type' => 'application/atom+xml', 'GData-Version' => '3.0'])->getBody(), true);
 }