| Total Complexity | 52 |
| Total Lines | 407 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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); |
||
| 36 | class TrackBusinessLayer extends BusinessLayer { |
||
| 37 | protected $mapper; // eclipse the definition from the base class, to help IDE and Scrutinizer to know the actual type |
||
| 38 | private $logger; |
||
| 39 | |||
| 40 | public function __construct(TrackMapper $trackMapper, Logger $logger) { |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Returns all tracks filtered by artist (both album and track artists are considered) |
||
| 48 | * @param int|int[] $artistId |
||
| 49 | * @return Track[] |
||
| 50 | */ |
||
| 51 | public function findAllByArtist(/*mixed*/ $artistId, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 52 | if (empty($artistId)) { |
||
| 53 | return []; |
||
| 54 | } else { |
||
| 55 | if (!\is_array($artistId)) { |
||
| 56 | $artistId = [$artistId]; |
||
| 57 | } |
||
| 58 | return $this->mapper->findAllByArtist($artistId, $userId, $limit, $offset); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns all tracks filtered by album. Optionally, filter also by the performing artist. |
||
| 64 | * @param int|int[] $albumId |
||
| 65 | * @return Track[] |
||
| 66 | */ |
||
| 67 | public function findAllByAlbum(/*mixed*/ $albumId, string $userId, ?int $artistId=null, ?int $limit=null, ?int $offset=null) : array { |
||
| 68 | if (empty($albumId)) { |
||
| 69 | return []; |
||
| 70 | } else { |
||
| 71 | if (!\is_array($albumId)) { |
||
| 72 | $albumId = [$albumId]; |
||
| 73 | } |
||
| 74 | return $this->mapper->findAllByAlbum($albumId, $userId, $artistId, $limit, $offset); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns all tracks filtered by parent folder |
||
| 80 | * @return Track[] |
||
| 81 | */ |
||
| 82 | public function findAllByFolder(int $folderId, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 83 | return $this->mapper->findAllByFolder($folderId, $userId, $limit, $offset); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns all tracks filtered by genre |
||
| 88 | * @return Track[] |
||
| 89 | */ |
||
| 90 | public function findAllByGenre(int $genreId, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 91 | return $this->mapper->findAllByGenre($genreId, $userId, $limit, $offset); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns all tracks filtered by name (of track/album/artist) |
||
| 96 | * @param string $name the name of the track/album/artist |
||
| 97 | * @param string $userId the name of the user |
||
| 98 | * @return Track[] |
||
| 99 | */ |
||
| 100 | public function findAllByNameRecursive(string $name, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 101 | $name = \trim($name); |
||
| 102 | return $this->mapper->findAllByNameRecursive($name, $userId, $limit, $offset); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns all tracks specified by name and/or artist name |
||
| 107 | * @return Track[] Tracks matching the criteria |
||
| 108 | */ |
||
| 109 | public function findAllByNameAndArtistName(?string $name, ?string $artistName, string $userId) : array { |
||
| 110 | if ($name !== null) { |
||
| 111 | $name = \trim($name); |
||
| 112 | } |
||
| 113 | if ($artistName !== null) { |
||
| 114 | $artistName = \trim($artistName); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $this->mapper->findAllByNameAndArtistName($name, $artistName, $userId); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns all tracks where the 'modified' time in the file system (actually in the cloud's file cache) |
||
| 122 | * is later than the 'updated' field of the entity in the database. |
||
| 123 | * @return Track[] |
||
| 124 | */ |
||
| 125 | public function findAllDirty(string $userId) : array { |
||
| 126 | $tracks = $this->findAll($userId); |
||
| 127 | return \array_filter($tracks, function (Track $track) { |
||
| 128 | $dbModTime = new \DateTime($track->getUpdated()); |
||
| 129 | return ($dbModTime->getTimestamp() < $track->getFileModTime()); |
||
| 130 | }); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Find most frequently played tracks |
||
| 135 | * @return Track[] |
||
| 136 | */ |
||
| 137 | public function findFrequentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 138 | return $this->mapper->findFrequentPlay($userId, $limit, $offset); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Find most recently played tracks |
||
| 143 | * @return Track[] |
||
| 144 | */ |
||
| 145 | public function findRecentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 146 | return $this->mapper->findRecentPlay($userId, $limit, $offset); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Find least recently played tracks |
||
| 151 | * @return Track[] |
||
| 152 | */ |
||
| 153 | public function findNotRecentPlay(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 154 | return $this->mapper->findNotRecentPlay($userId, $limit, $offset); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns the track for a file id |
||
| 159 | * @return Track|null |
||
| 160 | */ |
||
| 161 | public function findByFileId(int $fileId, string $userId) : ?Track { |
||
| 162 | try { |
||
| 163 | return $this->mapper->findByFileId($fileId, $userId); |
||
| 164 | } catch (DoesNotExistException $e) { |
||
| 165 | return null; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns file IDs of all indexed tracks of the user |
||
| 171 | * @return int[] |
||
| 172 | */ |
||
| 173 | public function findAllFileIds(string $userId) : array { |
||
| 174 | return $this->mapper->findAllFileIds($userId); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Returns all folders of the user containing indexed tracks, along with the contained track IDs |
||
| 179 | * @return array of entries like {id: int, name: string, path: string, parent: ?int, trackIds: int[]} |
||
| 180 | */ |
||
| 181 | public function findAllFolders(string $userId, Folder $musicFolder) : array { |
||
| 221 | } |
||
| 222 | |||
| 223 | private function recursivelyAddMissingParentFolders(array $foldersToProcess, array &$alreadyFoundFolders, Folder $musicFolder) : void { |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | private static function getFolderEntry(array $folderNamesAndParents, int $folderId, array $trackIds, Folder $musicFolder) : ?array { |
||
| 247 | $libRootId = $musicFolder->getId(); |
||
| 248 | |||
| 249 | if (isset($folderNamesAndParents[$folderId])) { |
||
| 250 | // normal folder within the user home storage |
||
| 251 | $entry = $folderNamesAndParents[$folderId]; |
||
| 252 | // special handling for the root folder |
||
| 253 | if ($folderId === $libRootId) { |
||
| 254 | $entry = null; |
||
| 255 | } |
||
| 256 | } else { |
||
| 257 | // shared folder or parent folder of a shared file or an externally mounted folder |
||
| 258 | $folderNode = $musicFolder->getById($folderId)[0] ?? null; |
||
| 259 | if ($folderNode === null) { |
||
| 260 | // other user's folder with files shared with this user (mapped under root) |
||
| 261 | $entry = null; |
||
| 262 | } else { |
||
| 263 | $parentId = $folderNode->getParent()->getId(); |
||
| 264 | // On NC28, the externally mounted folders have parentId=-1. Map such folders manually under the root folder. |
||
| 265 | if ($parentId == -1) { |
||
| 266 | $parentId = $libRootId; |
||
| 267 | } |
||
| 268 | $entry = [ |
||
| 269 | 'name' => $folderNode->getName(), |
||
| 270 | 'parent' => $parentId |
||
| 271 | ]; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | if ($entry) { |
||
| 276 | $entry['trackIds'] = $trackIds; |
||
| 277 | $entry['id'] = $folderId; |
||
| 278 | |||
| 279 | if ($entry['id'] == $libRootId) { |
||
| 280 | // the library root should be reported without a parent folder as that parent does not belong to the library |
||
| 281 | $entry['parent'] = null; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | return $entry; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Returns all genre IDs associated with the given artist |
||
| 290 | * @return int[] |
||
| 291 | */ |
||
| 292 | public function getGenresByArtistId(int $artistId, string $userId) : array { |
||
| 293 | return $this->mapper->getGenresByArtistId($artistId, $userId); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns file IDs of the tracks which do not have genre scanned. This is not the same |
||
| 298 | * thing as unknown genre, which is stored as empty string and means that the genre has |
||
| 299 | * been scanned but was not found from the track metadata. |
||
| 300 | * @return int[] |
||
| 301 | */ |
||
| 302 | public function findFilesWithoutScannedGenre(string $userId) : array { |
||
| 303 | return $this->mapper->findFilesWithoutScannedGenre($userId); |
||
| 304 | } |
||
| 305 | |||
| 306 | public function countByArtist(int $artistId) : int { |
||
| 307 | return $this->mapper->countByArtist($artistId); |
||
| 308 | } |
||
| 309 | |||
| 310 | public function countByAlbum(int $albumId) : int { |
||
| 311 | return $this->mapper->countByAlbum($albumId); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return integer Duration in seconds |
||
| 316 | */ |
||
| 317 | public function totalDurationOfAlbum(int $albumId) : int { |
||
| 318 | return $this->mapper->totalDurationOfAlbum($albumId); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return integer Duration in seconds |
||
| 323 | */ |
||
| 324 | public function totalDurationByArtist(int $artistId) : int { |
||
| 325 | return $this->mapper->totalDurationByArtist($artistId); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Update "last played" timestamp and increment the total play count of the track. |
||
| 330 | */ |
||
| 331 | public function recordTrackPlayed(int $trackId, string $userId, ?\DateTime $timeOfPlay = null) : void { |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Adds a track if it does not exist already or updates an existing track |
||
| 341 | * @param string $title the title of the track |
||
| 342 | * @param int|null $number the number of the track |
||
| 343 | * @param int|null $discNumber the number of the disc |
||
| 344 | * @param int|null $year the year of the release |
||
| 345 | * @param int $genreId the genre id of the track |
||
| 346 | * @param int $artistId the artist id of the track |
||
| 347 | * @param int $albumId the album id of the track |
||
| 348 | * @param int $fileId the file id of the track |
||
| 349 | * @param string $mimetype the mimetype of the track |
||
| 350 | * @param string $userId the name of the user |
||
| 351 | * @param int $length track length in seconds |
||
| 352 | * @param int $bitrate track bitrate in bits (not kbits) |
||
| 353 | * @return Track The added/updated track |
||
| 354 | */ |
||
| 355 | public function addOrUpdateTrack( |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Deletes a track |
||
| 376 | * @param int[] $fileIds file IDs of the tracks to delete |
||
| 377 | * @param string[]|null $userIds the target users; if omitted, the tracks matching the |
||
| 378 | * $fileIds are deleted from all users |
||
| 379 | * @return array|false False is returned if no such track was found; otherwise array of six arrays |
||
| 380 | * (named 'deletedTracks', 'remainingAlbums', 'remainingArtists', 'obsoleteAlbums', |
||
| 381 | * 'obsoleteArtists', and 'affectedUsers'). These contain the track, album, artist, and |
||
| 382 | * user IDs of the deleted tracks. The 'obsolete' entities are such which no longer |
||
| 383 | * have any tracks while 'remaining' entities have some left. |
||
| 384 | */ |
||
| 385 | public function deleteTracks(array $fileIds, ?array $userIds=null) { |
||
| 443 | } |
||
| 444 | } |
||
| 445 |