| Total Complexity | 63 |
| Total Lines | 477 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TrackBusinessLayer 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 TrackBusinessLayer, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 40 | class TrackBusinessLayer extends BusinessLayer { |
||
| 41 | private Logger $logger; |
||
| 42 | |||
| 43 | public function __construct(TrackMapper $trackMapper, Logger $logger) { |
||
| 44 | parent::__construct($trackMapper); |
||
| 45 | $this->logger = $logger; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Returns all tracks filtered by artist (both album and track artists are considered) |
||
| 50 | * @param int|int[] $artistId |
||
| 51 | * @return Track[] |
||
| 52 | */ |
||
| 53 | public function findAllByArtist(/*mixed*/ $artistId, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 54 | if (empty($artistId)) { |
||
| 55 | return []; |
||
| 56 | } else { |
||
| 57 | if (!\is_array($artistId)) { |
||
| 58 | $artistId = [$artistId]; |
||
| 59 | } |
||
| 60 | return $this->mapper->findAllByArtist($artistId, $userId, $limit, $offset); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Returns all tracks filtered by album. Optionally, filter also by the performing artist. |
||
| 66 | * @param int|int[] $albumId |
||
| 67 | * @return Track[] |
||
| 68 | */ |
||
| 69 | public function findAllByAlbum(/*mixed*/ $albumId, string $userId, ?int $artistId=null, ?int $limit=null, ?int $offset=null) : array { |
||
| 70 | if (empty($albumId)) { |
||
| 71 | return []; |
||
| 72 | } else { |
||
| 73 | if (!\is_array($albumId)) { |
||
| 74 | $albumId = [$albumId]; |
||
| 75 | } |
||
| 76 | return $this->mapper->findAllByAlbum($albumId, $userId, $artistId, $limit, $offset); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns all tracks filtered by parent folder |
||
| 82 | * @return Track[] |
||
| 83 | */ |
||
| 84 | public function findAllByFolder(int $folderId, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 85 | return $this->mapper->findAllByFolder($folderId, $userId, $limit, $offset); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns all tracks filtered by genre |
||
| 90 | * @return Track[] |
||
| 91 | */ |
||
| 92 | public function findAllByGenre(int $genreId, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 93 | return $this->mapper->findAllByGenre($genreId, $userId, $limit, $offset); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns all tracks filtered by name (of track/album/artist) |
||
| 98 | * @param string $name the name of the track/album/artist |
||
| 99 | * @param string $userId the name of the user |
||
| 100 | * @return Track[] |
||
| 101 | */ |
||
| 102 | public function findAllByNameRecursive(string $name, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 103 | $name = \trim($name); |
||
| 104 | return $this->mapper->findAllByNameRecursive($name, $userId, $limit, $offset); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns all tracks specified by name, artist name, and/or album name |
||
| 109 | * @return Track[] Tracks matching the criteria |
||
| 110 | */ |
||
| 111 | public function findAllByNameArtistOrAlbum(?string $name, ?string $artistName, ?string $albumName, string $userId) : array { |
||
| 112 | if ($name !== null) { |
||
| 113 | $name = \trim($name); |
||
| 114 | } |
||
| 115 | if ($artistName !== null) { |
||
| 116 | $artistName = \trim($artistName); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $this->mapper->findAllByNameArtistOrAlbum($name, $artistName, $albumName, $userId); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Find most frequently played tracks |
||
| 124 | * @return Track[] |
||
| 125 | */ |
||
| 126 | public function findFrequentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 127 | return $this->mapper->findFrequentPlay($userId, $limit, $offset); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Find most recently played tracks |
||
| 132 | * @return Track[] |
||
| 133 | */ |
||
| 134 | public function findRecentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 135 | return $this->mapper->findRecentPlay($userId, $limit, $offset); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Find least recently played tracks |
||
| 140 | * @return Track[] |
||
| 141 | */ |
||
| 142 | public function findNotRecentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 143 | return $this->mapper->findNotRecentPlay($userId, $limit, $offset); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the track for a file id |
||
| 148 | * @return Track|null |
||
| 149 | */ |
||
| 150 | public function findByFileId(int $fileId, string $userId) : ?Track { |
||
| 151 | try { |
||
| 152 | return $this->mapper->findByFileId($fileId, $userId); |
||
| 153 | } catch (DoesNotExistException $e) { |
||
| 154 | return null; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns file IDs of all indexed tracks of the user. |
||
| 160 | * Optionally, limit the search to files residing (directly or indirectly) in the given folder. |
||
| 161 | * @return int[] |
||
| 162 | */ |
||
| 163 | public function findAllFileIds(string $userId, ?int $folderId=null) : array { |
||
| 164 | $parentIds = ($folderId !== null) ? $this->findAllDescendantFolders($folderId) : null; |
||
| 165 | return $this->mapper->findAllFileIds($userId, $parentIds); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns file IDs of all indexed tracks of the user which should be rescanned to ensure that the library details are up-to-date. |
||
| 170 | * The track may be considered "dirty" for one of two reasons: |
||
| 171 | * - its 'modified' time in the file system (actually in the cloud's file cache) is later than the 'updated' field of the entity in the database |
||
| 172 | * - it has been specifically marked as dirty, maybe in response to being moved to another directory |
||
| 173 | * Optionally, limit the search to files residing (directly or indirectly) in the given folder. |
||
| 174 | * @return int[] |
||
| 175 | */ |
||
| 176 | public function findDirtyFileIds(string $userId, ?int $folderId=null) : array { |
||
| 177 | $parentIds = ($folderId !== null) ? $this->findAllDescendantFolders($folderId) : null; |
||
| 178 | return $this->mapper->findDirtyFileIds($userId, $parentIds); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns all folders of the user containing indexed tracks, along with the contained track IDs |
||
| 183 | * @return array of entries like {id: int, name: string, parent: ?int, trackIds: int[]} |
||
| 184 | */ |
||
| 185 | public function findAllFolders(string $userId, Folder $musicFolder) : array { |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param Track[] $tracks (in|out) |
||
| 198 | */ |
||
| 199 | public function injectFolderPathsToTracks(array $tracks, string $userId, Folder $musicFolder) : void { |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get folder info lookup table, for the given tracks. The table will contain all the predecessor folders |
||
| 227 | * between those tracks and the root music folder (inclusive). |
||
| 228 | * |
||
| 229 | * @param array $trackIdsByFolder Keys are folder IDs and values are arrays of track IDs |
||
| 230 | * @return array Keys are folder IDs and values are arrays like ['name' : string, 'parent' : int, 'trackIds' : int[]] |
||
| 231 | */ |
||
| 232 | private function getFoldersLut(array $trackIdsByFolder, string $userId, Folder $musicFolder) : array { |
||
| 233 | // Get the folder names and direct parent folder IDs directly from the DB. |
||
| 234 | // This is significantly more efficient than using the Files API because we need to |
||
| 235 | // run only single DB query instead of one per folder. |
||
| 236 | $folderNamesAndParents = $this->mapper->findNodeNamesAndParents(\array_keys($trackIdsByFolder)); |
||
| 237 | |||
| 238 | // Compile the look-up-table entries from our two intermediary arrays |
||
| 239 | $lut = []; |
||
| 240 | foreach ($trackIdsByFolder as $folderId => $trackIds) { |
||
| 241 | // $folderId is not found from $folderNamesAndParents if it's a dummy ID created as placeholder on a malformed playlist |
||
| 242 | $nameAndParent = $folderNamesAndParents[$folderId] ?? ['name' => '', 'parent' => null]; |
||
| 243 | $lut[$folderId] = \array_merge($nameAndParent, ['trackIds' => $trackIds]); |
||
| 244 | } |
||
| 245 | |||
| 246 | // the root folder should have null parent; here we also ensure it's included |
||
| 247 | $rootFolderId = $musicFolder->getId(); |
||
| 248 | $rootTracks = $lut[$rootFolderId]['trackIds'] ?? []; |
||
| 249 | $lut[$rootFolderId] = ['name' => '', 'parent' => null, 'trackIds' => $rootTracks]; |
||
| 250 | |||
| 251 | // External mounts and shared files/folders need some special handling. But if there are any, they should be found |
||
| 252 | // right under the top-level folder. |
||
| 253 | $this->addExternalMountsToFoldersLut($lut, $userId, $musicFolder); |
||
| 254 | |||
| 255 | // Add the intermediate folders which do not directly contain any tracks |
||
| 256 | $this->addMissingParentsToFoldersLut($lut); |
||
| 257 | |||
| 258 | return $lut; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Add externally mounted folders and shared files and folders to the folder LUT if there are any under the $musicFolder |
||
| 263 | * |
||
| 264 | * @param array $lut (in|out) Keys are folder IDs and values are arrays like ['name' : string, 'parent' : int, 'trackIds' : int[]] |
||
| 265 | */ |
||
| 266 | private function addExternalMountsToFoldersLut(array &$lut, string $userId, Folder $musicFolder) : void { |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Add any missing intermediary folder to the LUT. For this function to work correctly, the pre-condition is that the LUT contains |
||
| 309 | * a root node which is predecessor of all other contained nodes and has 'parent' set as null. |
||
| 310 | * |
||
| 311 | * @param array $lut (in|out) Keys are folder IDs and values are arrays like ['name' : string, 'parent' : int, 'trackIds' : int[]] |
||
| 312 | */ |
||
| 313 | private function addMissingParentsToFoldersLut(array &$lut) : void { |
||
| 326 | } |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Find all direct and indirect sub folders of the given folder. The result will include also the start folder. |
||
| 332 | * NOTE: This does not return the mounted or shared folders even in case the $folderId points to user home directory. |
||
| 333 | * @return int[] |
||
| 334 | */ |
||
| 335 | private function findAllDescendantFolders(int $folderId) : array { |
||
| 336 | $descendants = []; |
||
| 337 | $foldersToProcess = [$folderId]; |
||
| 338 | |||
| 339 | while(\count($foldersToProcess)) { |
||
| 340 | $descendants = \array_merge($descendants, $foldersToProcess); |
||
| 341 | $foldersToProcess = $this->mapper->findSubFolderIds($foldersToProcess); |
||
| 342 | } |
||
| 343 | |||
| 344 | return $descendants; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns all genre IDs associated with the given artist |
||
| 349 | * @return int[] |
||
| 350 | */ |
||
| 351 | public function getGenresByArtistId(int $artistId, string $userId) : array { |
||
| 352 | return $this->mapper->getGenresByArtistId($artistId, $userId); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns file IDs of the tracks which do not have genre scanned. This is not the same |
||
| 357 | * thing as unknown genre, which is stored as empty string and means that the genre has |
||
| 358 | * been scanned but was not found from the track metadata. |
||
| 359 | * @return int[] |
||
| 360 | */ |
||
| 361 | public function findFilesWithoutScannedGenre(string $userId) : array { |
||
| 362 | return $this->mapper->findFilesWithoutScannedGenre($userId); |
||
| 363 | } |
||
| 364 | |||
| 365 | public function countByArtist(int $artistId) : int { |
||
| 366 | return $this->mapper->countByArtist($artistId); |
||
| 367 | } |
||
| 368 | |||
| 369 | public function countByAlbum(int $albumId) : int { |
||
| 370 | return $this->mapper->countByAlbum($albumId); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return integer Duration in seconds |
||
| 375 | */ |
||
| 376 | public function totalDurationOfAlbum(int $albumId) : int { |
||
| 377 | return $this->mapper->totalDurationOfAlbum($albumId); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return integer Duration in seconds |
||
| 382 | */ |
||
| 383 | public function totalDurationByArtist(int $artistId) : int { |
||
| 384 | return $this->mapper->totalDurationByArtist($artistId); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Update "last played" timestamp and increment the total play count of the track. |
||
| 389 | */ |
||
| 390 | public function recordTrackPlayed(int $trackId, string $userId, ?\DateTime $timeOfPlay = null) : void { |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Adds a track if it does not exist already or updates an existing track |
||
| 400 | * @param string $title the title of the track |
||
| 401 | * @param int|null $number the number of the track |
||
| 402 | * @param int|null $discNumber the number of the disc |
||
| 403 | * @param int|null $year the year of the release |
||
| 404 | * @param int $genreId the genre id of the track |
||
| 405 | * @param int $artistId the artist id of the track |
||
| 406 | * @param int $albumId the album id of the track |
||
| 407 | * @param int $fileId the file id of the track |
||
| 408 | * @param string $mimetype the mimetype of the track |
||
| 409 | * @param string $userId the name of the user |
||
| 410 | * @param int $length track length in seconds |
||
| 411 | * @param int $bitrate track bitrate in bits (not kbits) |
||
| 412 | * @return Track The added/updated track |
||
| 413 | */ |
||
| 414 | public function addOrUpdateTrack( |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Deletes tracks |
||
| 436 | * @param int[] $fileIds file IDs of the tracks to delete |
||
| 437 | * @param string[]|null $userIds the target users; if omitted, the tracks matching the |
||
| 438 | * $fileIds are deleted from all users |
||
| 439 | * @return array|false False is returned if no such track was found; otherwise array of six arrays |
||
| 440 | * (named 'deletedTracks', 'remainingAlbums', 'remainingArtists', 'obsoleteAlbums', |
||
| 441 | * 'obsoleteArtists', and 'affectedUsers'). These contain the track, album, artist, and |
||
| 442 | * user IDs of the deleted tracks. The 'obsolete' entities are such which no longer |
||
| 443 | * have any tracks while 'remaining' entities have some left. |
||
| 444 | */ |
||
| 445 | public function deleteTracks(array $fileIds, ?array $userIds=null) { |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Marks tracks as dirty, ultimately requesting the user to rescan them |
||
| 507 | * @param int[] $fileIds file IDs of the tracks to mark as dirty |
||
| 508 | * @param string[]|null $userIds the target users; if omitted, the tracks matching the |
||
| 509 | * $fileIds are marked for all users |
||
| 510 | */ |
||
| 511 | public function markTracksDirty(array $fileIds, ?array $userIds=null) : void { |
||
| 517 | } |
||
| 518 | } |
||
| 520 |