| Total Complexity | 42 |
| Total Lines | 354 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ApiController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApiController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | class ApiController extends Controller { |
||
| 65 | private TagService $tagService; |
||
| 66 | private IManager $shareManager; |
||
| 67 | private IPreview $previewManager; |
||
| 68 | private IUserSession $userSession; |
||
| 69 | private IConfig $config; |
||
| 70 | private Folder $userFolder; |
||
| 71 | private UserConfig $userConfig; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $appName |
||
| 75 | * @param IRequest $request |
||
| 76 | * @param IUserSession $userSession |
||
| 77 | * @param TagService $tagService |
||
| 78 | * @param IPreview $previewManager |
||
| 79 | * @param IManager $shareManager |
||
| 80 | * @param IConfig $config |
||
| 81 | * @param Folder $userFolder |
||
| 82 | */ |
||
| 83 | public function __construct($appName, |
||
| 84 | IRequest $request, |
||
| 85 | IUserSession $userSession, |
||
| 86 | TagService $tagService, |
||
| 87 | IPreview $previewManager, |
||
| 88 | IManager $shareManager, |
||
| 89 | IConfig $config, |
||
| 90 | Folder $userFolder, |
||
| 91 | UserConfig $userConfig) { |
||
| 92 | parent::__construct($appName, $request); |
||
| 93 | $this->userSession = $userSession; |
||
| 94 | $this->tagService = $tagService; |
||
| 95 | $this->previewManager = $previewManager; |
||
| 96 | $this->shareManager = $shareManager; |
||
| 97 | $this->config = $config; |
||
| 98 | $this->userFolder = $userFolder; |
||
| 99 | $this->userConfig = $userConfig; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Gets a thumbnail of the specified file |
||
| 104 | * |
||
| 105 | * @since API version 1.0 |
||
| 106 | * |
||
| 107 | * @NoAdminRequired |
||
| 108 | * @NoCSRFRequired |
||
| 109 | * @StrictCookieRequired |
||
| 110 | * |
||
| 111 | * @param int $x |
||
| 112 | * @param int $y |
||
| 113 | * @param string $file URL-encoded filename |
||
| 114 | * @return DataResponse|FileDisplayResponse |
||
| 115 | */ |
||
| 116 | public function getThumbnail($x, $y, $file) { |
||
| 117 | if ($x < 1 || $y < 1) { |
||
| 118 | return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); |
||
| 119 | } |
||
| 120 | |||
| 121 | try { |
||
| 122 | $file = $this->userFolder->get($file); |
||
| 123 | if ($file instanceof Folder) { |
||
| 124 | throw new NotFoundException(); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** @var File $file */ |
||
| 128 | $preview = $this->previewManager->getPreview($file, $x, $y, true); |
||
| 129 | |||
| 130 | return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]); |
||
| 131 | } catch (NotFoundException $e) { |
||
| 132 | return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); |
||
| 133 | } catch (\Exception $e) { |
||
| 134 | return new DataResponse([], Http::STATUS_BAD_REQUEST); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Updates the info of the specified file path |
||
| 140 | * The passed tags are absolute, which means they will |
||
| 141 | * replace the actual tag selection. |
||
| 142 | * |
||
| 143 | * @NoAdminRequired |
||
| 144 | * |
||
| 145 | * @param string $path path |
||
| 146 | * @param array|string $tags array of tags |
||
| 147 | * @return DataResponse |
||
| 148 | */ |
||
| 149 | public function updateFileTags($path, $tags = null) { |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param \OCP\Files\Node[] $nodes |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | private function formatNodes(array $nodes) { |
||
| 178 | $shareTypesForNodes = $this->getShareTypesForNodes($nodes); |
||
| 179 | return array_values(array_map(function (Node $node) use ($shareTypesForNodes) { |
||
| 180 | $shareTypes = $shareTypesForNodes[$node->getId()] ?? []; |
||
| 181 | $file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo()); |
||
| 182 | $file['hasPreview'] = $this->previewManager->isAvailable($node); |
||
| 183 | $parts = explode('/', dirname($node->getPath()), 4); |
||
| 184 | if (isset($parts[3])) { |
||
| 185 | $file['path'] = '/' . $parts[3]; |
||
| 186 | } else { |
||
| 187 | $file['path'] = '/'; |
||
| 188 | } |
||
| 189 | if (!empty($shareTypes)) { |
||
| 190 | $file['shareTypes'] = $shareTypes; |
||
| 191 | } |
||
| 192 | return $file; |
||
| 193 | }, $nodes)); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the share types for each node |
||
| 198 | * |
||
| 199 | * @param \OCP\Files\Node[] $nodes |
||
| 200 | * @return array<int, int[]> list of share types for each fileid |
||
| 201 | */ |
||
| 202 | private function getShareTypesForNodes(array $nodes): array { |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns a list of recently modified files. |
||
| 249 | * |
||
| 250 | * @NoAdminRequired |
||
| 251 | * |
||
| 252 | * @return DataResponse |
||
| 253 | */ |
||
| 254 | public function getRecentFiles() { |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the current logged-in user's storage stats. |
||
| 263 | * |
||
| 264 | * @NoAdminRequired |
||
| 265 | * |
||
| 266 | * @param ?string $dir the directory to get the storage stats from |
||
| 267 | * @return JSONResponse |
||
| 268 | */ |
||
| 269 | public function getStorageStats($dir = '/'): JSONResponse { |
||
| 270 | $storageInfo = \OC_Helper::getStorageInfo($dir ?: '/'); |
||
| 271 | return new JSONResponse(['message' => 'ok', 'data' => $storageInfo]); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Change the default sort mode |
||
| 276 | * |
||
| 277 | * @NoAdminRequired |
||
| 278 | * |
||
| 279 | * @param string $mode |
||
| 280 | * @param string $direction |
||
| 281 | * @return Response |
||
| 282 | * @throws \OCP\PreConditionNotMetException |
||
| 283 | */ |
||
| 284 | public function updateFileSorting($mode, $direction) { |
||
| 285 | $allowedMode = ['name', 'size', 'mtime']; |
||
| 286 | $allowedDirection = ['asc', 'desc']; |
||
| 287 | if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) { |
||
| 288 | $response = new Response(); |
||
| 289 | $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); |
||
| 290 | return $response; |
||
| 291 | } |
||
| 292 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode); |
||
| 293 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction); |
||
| 294 | return new Response(); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Toggle default files user config |
||
| 299 | * |
||
| 300 | * @NoAdminRequired |
||
| 301 | * |
||
| 302 | * @param string $key |
||
| 303 | * @param string|bool $value |
||
| 304 | * @return JSONResponse |
||
| 305 | */ |
||
| 306 | public function setConfig(string $key, $value): JSONResponse { |
||
| 307 | try { |
||
| 308 | $this->userConfig->setConfig($key, (string)$value); |
||
| 309 | } catch (\InvalidArgumentException $e) { |
||
| 310 | return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
||
| 311 | } |
||
| 312 | |||
| 313 | return new JSONResponse(['message' => 'ok', 'data' => ['key' => $key, 'value' => $value]]); |
||
| 314 | } |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Get the user config |
||
| 319 | * |
||
| 320 | * @NoAdminRequired |
||
| 321 | * |
||
| 322 | * @return JSONResponse |
||
| 323 | */ |
||
| 324 | public function getConfigs(): JSONResponse { |
||
| 325 | return new JSONResponse(['message' => 'ok', 'data' => $this->userConfig->getConfigs()]); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Toggle default for showing/hiding hidden files |
||
| 330 | * |
||
| 331 | * @NoAdminRequired |
||
| 332 | * |
||
| 333 | * @param bool $value |
||
| 334 | * @return Response |
||
| 335 | * @throws \OCP\PreConditionNotMetException |
||
| 336 | */ |
||
| 337 | public function showHiddenFiles(bool $value): Response { |
||
| 338 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', $value ? '1' : '0'); |
||
| 339 | return new Response(); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Toggle default for cropping preview images |
||
| 344 | * |
||
| 345 | * @NoAdminRequired |
||
| 346 | * |
||
| 347 | * @param bool $value |
||
| 348 | * @return Response |
||
| 349 | * @throws \OCP\PreConditionNotMetException |
||
| 350 | */ |
||
| 351 | public function cropImagePreviews(bool $value): Response { |
||
| 352 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', $value ? '1' : '0'); |
||
| 353 | return new Response(); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Toggle default for files grid view |
||
| 358 | * |
||
| 359 | * @NoAdminRequired |
||
| 360 | * |
||
| 361 | * @param bool $show |
||
| 362 | * @return Response |
||
| 363 | * @throws \OCP\PreConditionNotMetException |
||
| 364 | */ |
||
| 365 | public function showGridView(bool $show): Response { |
||
| 366 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_grid', $show ? '1' : '0'); |
||
| 367 | return new Response(); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get default settings for the grid view |
||
| 372 | * |
||
| 373 | * @NoAdminRequired |
||
| 374 | */ |
||
| 375 | public function getGridView() { |
||
| 376 | $status = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_grid', '0') === '1'; |
||
| 377 | return new JSONResponse(['gridview' => $status]); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Toggle default for showing/hiding xxx folder |
||
| 382 | * |
||
| 383 | * @NoAdminRequired |
||
| 384 | * |
||
| 385 | * @param int $show |
||
| 386 | * @param string $key the key of the folder |
||
| 387 | * |
||
| 388 | * @return Response |
||
| 389 | * @throws \OCP\PreConditionNotMetException |
||
| 390 | */ |
||
| 391 | public function toggleShowFolder(int $show, string $key): Response { |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get sorting-order for custom sorting |
||
| 408 | * |
||
| 409 | * @NoAdminRequired |
||
| 410 | * |
||
| 411 | * @param string $folderpath |
||
| 412 | * @return string |
||
| 413 | * @throws \OCP\Files\NotFoundException |
||
| 414 | */ |
||
| 415 | public function getNodeType($folderpath) { |
||
| 418 | } |
||
| 419 | } |
||
| 420 |