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