| Total Complexity | 68 |
| Total Lines | 551 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SubsonicController 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 SubsonicController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class SubsonicController extends Controller { |
||
| 41 | const API_VERSION = '1.4.0'; |
||
| 42 | |||
| 43 | private $albumBusinessLayer; |
||
| 44 | private $artistBusinessLayer; |
||
| 45 | private $playlistBusinessLayer; |
||
| 46 | private $trackBusinessLayer; |
||
| 47 | private $library; |
||
| 48 | private $urlGenerator; |
||
| 49 | private $rootFolder; |
||
| 50 | private $l10n; |
||
| 51 | private $coverHelper; |
||
| 52 | private $logger; |
||
| 53 | private $userId; |
||
| 54 | private $format; |
||
| 55 | private $callback; |
||
| 56 | |||
| 57 | public function __construct($appname, |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Called by the middleware once the user credentials have been checked |
||
| 88 | * @param string $userId |
||
| 89 | */ |
||
| 90 | public function setAuthenticatedUser($userId) { |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @NoAdminRequired |
||
| 96 | * @PublicPage |
||
| 97 | * @NoCSRFRequired |
||
| 98 | * @SubsonicAPI |
||
| 99 | */ |
||
| 100 | public function handleRequest($method) { |
||
| 101 | $this->format = $this->request->getParam('f', 'xml'); |
||
| 102 | $this->callback = $this->request->getParam('callback'); |
||
| 103 | |||
| 104 | if ($this->format != 'json' && $this->format != 'xml' && $this->format != 'jsonp') { |
||
| 105 | throw new SubsonicException("Unsupported format {$this->format}", 0); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($this->format === 'jsonp' && $this->callback === null) { |
||
| 109 | $this->format = 'json'; |
||
| 110 | throw new SubsonicException("Argument 'callback' is required with jsonp format", 10); |
||
| 111 | } |
||
| 112 | |||
| 113 | // Allow calling all methods with or without the postfix ".view" |
||
| 114 | if (Util::endsWith($method, ".view")) { |
||
| 115 | $method = \substr($method, 0, -\strlen(".view")); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Allow calling any functions annotated to be part of the API, except for |
||
| 119 | // recursive call back to this dispatcher function. |
||
| 120 | if ($method !== 'handleRequest' && \method_exists($this, $method)) { |
||
| 121 | $annotationReader = new MethodAnnotationReader($this, $method); |
||
| 122 | if ($annotationReader->hasAnnotation('SubsonicAPI')) { |
||
| 123 | return $this->$method(); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->logger->log("Request $method not supported", 'warn'); |
||
| 128 | return $this->subsonicErrorResponse(70, "Requested action $method is not supported"); |
||
| 129 | } |
||
| 130 | |||
| 131 | /* ------------------------------------------------------------------------- |
||
| 132 | * REST API methods |
||
| 133 | *------------------------------------------------------------------------*/ |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @SubsonicAPI |
||
| 137 | */ |
||
| 138 | private function ping() { |
||
| 139 | return $this->subsonicResponse([]); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @SubsonicAPI |
||
| 144 | */ |
||
| 145 | private function getLicense() { |
||
| 146 | return $this->subsonicResponse([ |
||
| 147 | 'license' => [ |
||
| 148 | 'valid' => 'true', |
||
| 149 | 'email' => '', |
||
| 150 | 'licenseExpires' => 'never' |
||
| 151 | ] |
||
| 152 | ]); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @SubsonicAPI |
||
| 157 | */ |
||
| 158 | private function getMusicFolders() { |
||
| 159 | // Only single root folder is supported |
||
| 160 | return $this->subsonicResponse([ |
||
| 161 | 'musicFolders' => ['musicFolder' => [ |
||
| 162 | ['id' => 'root', |
||
| 163 | 'name' => $this->l10n->t('Music')] |
||
| 164 | ]] |
||
| 165 | ]); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @SubsonicAPI |
||
| 170 | */ |
||
| 171 | private function getIndexes() { |
||
| 172 | $artists = $this->artistBusinessLayer->findAllHavingAlbums($this->userId, SortBy::Name); |
||
| 173 | |||
| 174 | $indexes = []; |
||
| 175 | foreach ($artists as $artist) { |
||
| 176 | $indexes[$artist->getIndexingChar()][] = $this->artistAsChild($artist); |
||
| 177 | } |
||
| 178 | |||
| 179 | $result = []; |
||
| 180 | foreach ($indexes as $indexChar => $bucketArtists) { |
||
| 181 | $result[] = ['name' => $indexChar, 'artist' => $bucketArtists]; |
||
| 182 | } |
||
| 183 | |||
| 184 | return $this->subsonicResponse(['indexes' => ['index' => $result]]); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @SubsonicAPI |
||
| 189 | */ |
||
| 190 | private function getMusicDirectory() { |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @SubsonicAPI |
||
| 202 | */ |
||
| 203 | private function getAlbumList() { |
||
| 204 | $type = $this->getRequiredParam('type'); |
||
| 205 | $size = $this->request->getParam('size', 10); |
||
| 206 | $size = \min($size, 500); // the API spec limits the maximum amount to 500 |
||
| 207 | // $offset = $this->request->getParam('offset', 0); parameter not supported for now |
||
| 208 | |||
| 209 | $albums = []; |
||
| 210 | if ($type == 'random') { |
||
| 211 | $allAlbums = $this->albumBusinessLayer->findAll($this->userId); |
||
| 212 | $albums = self::randomItems($allAlbums, $size); |
||
| 213 | } |
||
| 214 | // TODO: support 'newest', 'highest', 'frequent', 'recent' |
||
| 215 | |||
| 216 | return $this->subsonicResponse(['albumList' => |
||
| 217 | ['album' => \array_map([$this, 'albumAsChild'], $albums)] |
||
| 218 | ]); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @SubsonicAPI |
||
| 223 | */ |
||
| 224 | private function getRandomSongs() { |
||
| 225 | $size = $this->request->getParam('size', 10); |
||
| 226 | $size = \min($size, 500); // the API spec limits the maximum amount to 500 |
||
| 227 | // $genre = $this->request->getParam('genre'); not supported |
||
| 228 | // $fromYear = $this->request->getParam('fromYear'); not supported |
||
| 229 | // $toYear = $this->request->getParam('genre'); not supported |
||
| 230 | |||
| 231 | $allTracks = $this->trackBusinessLayer->findAll($this->userId); |
||
| 232 | $tracks = self::randomItems($allTracks, $size); |
||
| 233 | |||
| 234 | return $this->subsonicResponse(['randomSongs' => |
||
| 235 | ['song' => \array_map([$this, 'trackAsChild'], $tracks)] |
||
| 236 | ]); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @SubsonicAPI |
||
| 241 | */ |
||
| 242 | private function getCoverArt() { |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @SubsonicAPI |
||
| 260 | */ |
||
| 261 | private function stream() { |
||
| 262 | // We don't support transcaoding, so 'stream' and 'download' act identically |
||
| 263 | return $this->download(); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @SubsonicAPI |
||
| 268 | */ |
||
| 269 | private function download() { |
||
| 270 | $id = $this->getRequiredParam('id'); |
||
| 271 | $trackId = self::ripIdPrefix($id); // get rid of 'track-' prefix |
||
| 272 | |||
| 273 | try { |
||
| 274 | $track = $this->trackBusinessLayer->find($trackId, $this->userId); |
||
| 275 | } catch (BusinessLayerException $e) { |
||
| 276 | return $this->subsonicErrorResponse(70, $e->getMessage()); |
||
| 277 | } |
||
| 278 | |||
| 279 | $files = $this->rootFolder->getUserFolder($this->userId)->getById($track->getFileId()); |
||
| 280 | |||
| 281 | if (\count($files) === 1) { |
||
| 282 | return new FileResponse($files[0]); |
||
| 283 | } else { |
||
| 284 | return $this->subsonicErrorResponse(70, 'file not found'); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @SubsonicAPI |
||
| 290 | */ |
||
| 291 | private function search2() { |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @SubsonicAPI |
||
| 324 | */ |
||
| 325 | private function getPlaylists() { |
||
| 326 | $playlists = $this->playlistBusinessLayer->findAll($this->userId); |
||
| 327 | |||
| 328 | return $this->subsonicResponse(['playlists' => |
||
| 329 | ['playlist' => \array_map([$this, 'playlistAsChild'], $playlists)] |
||
| 330 | ]); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @SubsonicAPI |
||
| 335 | */ |
||
| 336 | private function getPlaylist() { |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @SubsonicAPI |
||
| 349 | */ |
||
| 350 | private function createPlaylist() { |
||
| 351 | $name = $this->getRequiredParam('name'); |
||
| 352 | $songIds = $this->getRepeatedParam('songId'); |
||
| 353 | $songIds = \array_map('self::ripIdPrefix', $songIds); |
||
| 354 | |||
| 355 | $playlist = $this->playlistBusinessLayer->create($name, $this->userId); |
||
| 356 | $this->playlistBusinessLayer->addTracks($songIds, $playlist->getId(), $this->userId); |
||
| 357 | |||
| 358 | return $this->subsonicResponse([]); |
||
| 359 | } |
||
| 360 | |||
| 361 | /* ------------------------------------------------------------------------- |
||
| 362 | * Helper methods |
||
| 363 | *------------------------------------------------------------------------*/ |
||
| 364 | |||
| 365 | private function getRequiredParam($paramName) { |
||
| 366 | $param = $this->request->getParam($paramName); |
||
| 367 | |||
| 368 | if ($param === null) { |
||
| 369 | throw new SubsonicException("Required parameter '$paramName' missing", 10); |
||
| 370 | } |
||
| 371 | |||
| 372 | return $param; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get values for parameter which may be present multiple times in the query |
||
| 377 | * string or POST data. |
||
| 378 | * @param string $paramName |
||
| 379 | * @return string[] |
||
| 380 | */ |
||
| 381 | private function getRepeatedParam($paramName) { |
||
| 382 | // We can't use the IRequest object nor $_GET and $_POST to get the data |
||
| 383 | // because all of these are based to the idea of unique parameter names. |
||
| 384 | // If the same name is repeated, only the last value is saved. Hence, we |
||
| 385 | // need to parse the raw data manually. |
||
| 386 | |||
| 387 | // query string is always present (although it could be empty) |
||
| 388 | $values = $this->parseRepeatedKeyValues($paramName, $_SERVER['QUERY_STRING']); |
||
| 389 | |||
| 390 | // POST data is available if the method is POST |
||
| 391 | if ($this->request->getMethod() == 'POST') { |
||
| 392 | $values = \array_merge($values, |
||
| 393 | $this->parseRepeatedKeyValues($paramName, file_get_contents('php://input'))); |
||
| 394 | } |
||
| 395 | |||
| 396 | return $values; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Parse a string like "someKey=value1&someKey=value2&anotherKey=valueA&someKey=value3" |
||
| 401 | * and return an array of values for the given key |
||
| 402 | * @param string $key |
||
| 403 | * @param string $data |
||
| 404 | */ |
||
| 405 | private function parseRepeatedKeyValues($key, $data) { |
||
| 406 | $result = []; |
||
| 407 | |||
| 408 | $keyValuePairs = \explode('&', $data); |
||
| 409 | |||
| 410 | foreach ($keyValuePairs as $pair) { |
||
| 411 | $keyAndValue = \explode('=', $pair); |
||
| 412 | |||
| 413 | if ($keyAndValue[0] == $key) { |
||
| 414 | $result[] = $keyAndValue[1]; |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | return $result; |
||
| 419 | } |
||
| 420 | |||
| 421 | private function doGetMusicDirectoryForArtist($id) { |
||
| 439 | ] |
||
| 440 | ]); |
||
| 441 | } |
||
| 442 | |||
| 443 | private function doGetMusicDirectoryForAlbum($id) { |
||
| 444 | $albumId = self::ripIdPrefix($id); // get rid of 'album-' prefix |
||
| 445 | |||
| 446 | $album = $this->albumBusinessLayer->find($albumId, $this->userId); |
||
| 447 | $albumName = $album->getNameString($this->l10n); |
||
| 448 | $tracks = $this->trackBusinessLayer->findAllByAlbum($albumId, $this->userId); |
||
| 449 | |||
| 450 | return $this->subsonicResponse([ |
||
| 451 | 'directory' => [ |
||
| 452 | 'id' => $id, |
||
| 453 | 'parent' => 'artist-' . $album->getAlbumArtistId(), |
||
| 454 | 'name' => $albumName, |
||
| 455 | 'child' => \array_map([$this, 'trackAsChild'], $tracks) |
||
| 456 | ] |
||
| 457 | ]); |
||
| 458 | } |
||
| 459 | |||
| 460 | private function artistAsChild($artist) { |
||
| 461 | return [ |
||
| 462 | 'name' => $artist->getNameString($this->l10n), |
||
| 463 | 'id' => 'artist-' . $artist->getId() |
||
| 464 | ]; |
||
| 465 | } |
||
| 466 | |||
| 467 | private function albumAsChild($album, $artistName = null) { |
||
| 468 | $artistId = $album->getAlbumArtistId(); |
||
| 469 | |||
| 470 | if (empty($artistName)) { |
||
| 471 | $artist = $this->artistBusinessLayer->find($artistId, $this->userId); |
||
| 472 | $artistName = $artist->getNameString($this->l10n); |
||
| 473 | } |
||
| 474 | |||
| 475 | $result = [ |
||
| 476 | 'id' => 'album-' . $album->getId(), |
||
| 477 | 'parent' => 'artist-' . $artistId, |
||
| 478 | 'title' => $album->getNameString($this->l10n), |
||
| 479 | 'artist' => $artistName, |
||
| 480 | 'isDir' => true |
||
| 481 | ]; |
||
| 482 | |||
| 483 | if (!empty($album->getCoverFileId())) { |
||
| 484 | $result['coverArt'] = $album->getId(); |
||
| 485 | } |
||
| 486 | |||
| 487 | return $result; |
||
| 488 | } |
||
| 489 | |||
| 490 | private function trackAsChild($track, $album = null, $albumName = null) { |
||
| 526 | } |
||
| 527 | |||
| 528 | private function playlistAsChild($playlist) { |
||
| 529 | return [ |
||
| 530 | 'id' => $playlist->getId(), |
||
| 531 | 'name' => $playlist->getName(), |
||
| 532 | 'owner' => $this->userId, |
||
| 533 | 'public' => false, |
||
| 534 | 'songCount' => $playlist->getTrackCount(), |
||
| 535 | // comment => '', |
||
| 536 | // duration => '', |
||
| 537 | // created => '', |
||
| 538 | // coverArt => '' |
||
| 539 | ]; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Given a prefixed ID like 'artist-123' or 'track-45', return just the numeric part. |
||
| 544 | * @param string $id |
||
| 545 | * @return string |
||
| 546 | */ |
||
| 547 | private static function ripIdPrefix($id) { |
||
| 548 | return \explode('-', $id)[1]; |
||
| 549 | } |
||
| 550 | |||
| 551 | private static function randomItems($itemArray, $count) { |
||
| 552 | $count = \min($count, \count($itemArray)); // can't return more than all items |
||
| 553 | $indices = \array_rand($itemArray, $count); |
||
| 554 | if ($count == 1) { // return type is not array when randomizing a single index |
||
| 555 | $indices = [$indices]; |
||
| 556 | } |
||
| 557 | |||
| 558 | $result = []; |
||
| 559 | foreach ($indices as $index) { |
||
| 560 | $result[] = $itemArray[$index]; |
||
| 561 | } |
||
| 562 | |||
| 563 | return $result; |
||
| 564 | } |
||
| 565 | |||
| 566 | private function subsonicResponse($content, $status = 'ok') { |
||
| 582 | } |
||
| 583 | |||
| 584 | public function subsonicErrorResponse($errorCode, $errorMessage) { |
||
| 585 | return $this->subsonicResponse([ |
||
| 586 | 'error' => [ |
||
| 587 | 'code' => $errorCode, |
||
| 588 | 'message' => $errorMessage |
||
| 589 | ] |
||
| 591 | } |
||
| 592 | } |
||
| 593 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths