/** * @SuppressWarnings(PHPMD.StaticAccess) * * @param string $path * * @return string last folder's id * * @throws \Exception */ public function create($path) { $folders = explode('/', trim(preg_replace('#/+#', '/', $path), '/')); $lastFolderId = self::ROOT_DIR_ID; foreach ($folders as $name) { $command = new CreateFolder($name, $lastFolderId); $response = ResponseFactory::getResponse($this->contentClient, $command); if ($response instanceof SuccessResponse) { if ($response->getStatusCode() == 201) { $lastFolderId = $response->json()['id']; usleep(250); continue; } } elseif ($response instanceof ErrorResponse) { if ($response->getStatusCode() == 409) { $lastFolderId = reset($response->json()['context_info']['conflicts'])['id']; usleep(250); continue; } throw new \Exception($response->getStatusCode() . (string) $response->getBody()); } } if (is_null($lastFolderId)) { throw new \Exception(); } return $lastFolderId; }
/** * @SuppressWarnings(PHPMD.StaticAccess) * * @param string $path * * @return ErrorResponse|SuccessResponse */ protected function getSearchResponse($path) { $extendedRequest = new ExtendedRequest(); $extendedRequest->addQueryField('content_types', 'name'); $extendedRequest->addQueryField('limit', 20); $command = new SearchContent(basename($path), $extendedRequest); return ResponseFactory::getResponse($this->contentClient, $command); }
public function deleteFile($file) { $accessToken = $_SESSION['boxToken']; $contentClient = new ContentClient(new ApiClient($accessToken), new UploadClient($accessToken)); $er = new ExtendedRequest(); $command = new Content\File\DeleteFile($file); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { echo (string) $response->getStatusCode(); echo "<br>"; echo (string) $response->getReasonPhrase(); } elseif ($response instanceof ErrorResponse) { # ... } }
public function searchFile($keyword) { $contentClient = new ContentClient(new ApiClient($this->access_token), new UploadClient($this->access_token)); $command = new Content\Search\SearchContent(urlencode($keyword)); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { $response->getStatusCode(); $response->getReasonPhrase(); $response->getHeaders(); $data = (string) $response->getBody(); $entries = json_decode($data); $format = array(); foreach ($entries->entries as $entity) { $entity = (object) $entity; array_push($format, array('name' => $entity->name, 'path' => "" . ($entity->id == "0" ? null : $entity->type == "folder" ? "folder." . $entity->id : "file." . $entity->id), 'size' => $entity->size, 'mime_type' => null, 'is_dir' => $entity->type == "folder" ? 1 : 0, 'modified' => $entity->modified_at, 'shared' => false)); } return $format; } elseif ($response instanceof ErrorResponse) { return false; } }