| Total Complexity | 44 |
| Total Lines | 390 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 | * @param int $artistId the id of the artist |
||
| 49 | * @param string $userId the name of the user |
||
| 50 | * @return array of tracks |
||
| 51 | */ |
||
| 52 | public function findAllByArtist($artistId, $userId) { |
||
| 53 | return $this->mapper->findAllByArtist($artistId, $userId); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns all tracks filtered by album. Optionally, filter also by the performing artist. |
||
| 58 | * @param int $albumId the id of the album |
||
| 59 | * @param string $userId the name of the user |
||
| 60 | * @return \OCA\Music\Db\Track[] tracks |
||
| 61 | */ |
||
| 62 | public function findAllByAlbum($albumId, $userId, $artistId = null) { |
||
| 63 | return $this->mapper->findAllByAlbum($albumId, $userId, $artistId); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns all tracks filtered by parent folder |
||
| 68 | * @param integer $folderId the id of the folder |
||
| 69 | * @param string $userId the name of the user |
||
| 70 | * @return \OCA\Music\Db\Track[] tracks |
||
| 71 | */ |
||
| 72 | public function findAllByFolder($folderId, $userId) { |
||
| 73 | return $this->mapper->findAllByFolder($folderId, $userId); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns all tracks filtered by genre |
||
| 78 | * @param int $genreId the genre to include |
||
| 79 | * @param string $userId the name of the user |
||
| 80 | * @param int|null $limit |
||
| 81 | * @param int|null $offset |
||
| 82 | * @return \OCA\Music\Db\Track[] tracks |
||
| 83 | */ |
||
| 84 | public function findAllByGenre($genreId, $userId, $limit=null, $offset=null) { |
||
| 85 | return $this->mapper->findAllByGenre($genreId, $userId, $limit, $offset); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns all tracks filtered by name (of track/album/artist) |
||
| 90 | * @param string $name the name of the track/album/artist |
||
| 91 | * @param string $userId the name of the user |
||
| 92 | * @return \OCA\Music\Db\Track[] tracks |
||
| 93 | */ |
||
| 94 | public function findAllByNameRecursive($name, $userId) { |
||
| 95 | $name = \trim($name); |
||
| 96 | return $this->mapper->findAllByNameRecursive($name, $userId); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns all tracks specified by name and/or artist name |
||
| 101 | * @param string|null $name the name of the track |
||
| 102 | * @param string|null $artistName the name of the artist |
||
| 103 | * @param string $userId the name of the user |
||
| 104 | * @return \OCA\Music\Db\Track[] Tracks matching the criteria |
||
| 105 | */ |
||
| 106 | public function findAllByNameAndArtistName(?string $name, ?string $artistName, string $userId) : array { |
||
| 107 | if ($name !== null) { |
||
| 108 | $name = \trim($name); |
||
| 109 | } |
||
| 110 | if ($artistName !== null) { |
||
| 111 | $artistName = \trim($artistName); |
||
| 112 | } |
||
| 113 | |||
| 114 | return $this->mapper->findAllByNameAndArtistName($name, $artistName, /*fuzzy=*/false, $userId); |
||
| 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 { |
||
| 130 | return $this->mapper->findRecentPlay($userId, $limit, $offset); |
||
| 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 { |
||
| 138 | return $this->mapper->findNotRecentPlay($userId, $limit, $offset); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns the track for a file id |
||
| 143 | * @param int $fileId the file id of the track |
||
| 144 | * @param string $userId the name of the user |
||
| 145 | * @return \OCA\Music\Db\Track|null track |
||
| 146 | */ |
||
| 147 | public function findByFileId($fileId, $userId) { |
||
| 148 | try { |
||
| 149 | return $this->mapper->findByFileId($fileId, $userId); |
||
| 150 | } catch (DoesNotExistException $e) { |
||
| 151 | return null; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns file IDs of all indexed tracks of the user |
||
| 157 | * @param string $userId |
||
| 158 | * @return int[] |
||
| 159 | */ |
||
| 160 | public function findAllFileIds($userId) { |
||
| 161 | return $this->mapper->findAllFileIds($userId); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns all folders of the user containing indexed tracks, along with the contained track IDs |
||
| 166 | * @return array of entries like {id: int, name: string, path: string, parent: ?int, trackIds: int[]} |
||
| 167 | */ |
||
| 168 | public function findAllFolders(string $userId, Folder $musicFolder) : array { |
||
| 208 | } |
||
| 209 | |||
| 210 | private function recursivelyGetMissingParentFolders(array $childEntries, array $existingEntries, Folder $musicFolder) : array { |
||
| 211 | $result = []; |
||
| 212 | |||
| 213 | $parentIds = \array_unique(\array_column($childEntries, 'parent')); |
||
| 214 | $parentIds = Util::arrayDiff($parentIds, \array_column($existingEntries, 'id')); |
||
| 215 | $parentInfo = $this->mapper->findNodeNamesAndParents($parentIds, $musicFolder->getStorage()->getId()); |
||
| 216 | foreach ($parentIds as $parentId) { |
||
| 217 | if ($parentId !== null) { |
||
| 218 | $result[] = self::getFolderEntry($parentInfo, $parentId, [], $musicFolder); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | if (\count($parentIds)) { |
||
| 223 | $result = \array_merge($result, $this->recursivelyGetMissingParentFolders($result, $existingEntries, $musicFolder)); |
||
| 224 | } |
||
| 225 | |||
| 226 | return $result; |
||
| 227 | } |
||
| 228 | |||
| 229 | private static function getFolderEntry(array $folderNamesAndParents, int $folderId, array $trackIds, Folder $musicFolder) : ?array { |
||
| 230 | if (isset($folderNamesAndParents[$folderId])) { |
||
| 231 | // normal folder within the user home storage |
||
| 232 | $entry = $folderNamesAndParents[$folderId]; |
||
| 233 | // special handling for the root folder |
||
| 234 | if ($folderId === $musicFolder->getId()) { |
||
| 235 | $entry = null; |
||
| 236 | } |
||
| 237 | } else { |
||
| 238 | // shared folder or parent folder of a shared file or an externally mounted folder |
||
| 239 | $folderNode = $musicFolder->getById($folderId)[0] ?? null; |
||
| 240 | if ($folderNode === null) { |
||
| 241 | // other user's folder with files shared with this user (mapped under root) |
||
| 242 | $entry = null; |
||
| 243 | } else { |
||
| 244 | $entry = [ |
||
| 245 | 'name' => $folderNode->getName(), |
||
| 246 | 'parent' => $folderNode->getParent()->getId() |
||
| 247 | ]; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | if ($entry) { |
||
| 252 | $entry['trackIds'] = $trackIds; |
||
| 253 | $entry['id'] = $folderId; |
||
| 254 | |||
| 255 | if ($entry['id'] == $musicFolder->getId()) { |
||
| 256 | // the library root should be reported without a parent folder as that parent does not belong to the library |
||
| 257 | $entry['parent'] = null; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | return $entry; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns all genre IDs associated with the given artist |
||
| 266 | * @param int $artistId |
||
| 267 | * @param string $userId |
||
| 268 | * @return int[] |
||
| 269 | */ |
||
| 270 | public function getGenresByArtistId($artistId, $userId) { |
||
| 271 | return $this->mapper->getGenresByArtistId($artistId, $userId); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns file IDs of the tracks which do not have genre scanned. This is not the same |
||
| 276 | * thing as unknown genre, which is stored as empty string and means that the genre has |
||
| 277 | * been scanned but was not found from the track metadata. |
||
| 278 | * @param string $userId |
||
| 279 | * @return int[] |
||
| 280 | */ |
||
| 281 | public function findFilesWithoutScannedGenre($userId) { |
||
| 282 | return $this->mapper->findFilesWithoutScannedGenre($userId); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param integer $artistId |
||
| 287 | * @return integer |
||
| 288 | */ |
||
| 289 | public function countByArtist($artistId) { |
||
| 290 | return $this->mapper->countByArtist($artistId); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param integer $albumId |
||
| 295 | * @return integer |
||
| 296 | */ |
||
| 297 | public function countByAlbum($albumId) { |
||
| 298 | return $this->mapper->countByAlbum($albumId); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param integer $albumId |
||
| 303 | * @return integer Duration in seconds |
||
| 304 | */ |
||
| 305 | public function totalDurationOfAlbum($albumId) { |
||
| 306 | return $this->mapper->totalDurationOfAlbum($albumId); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Update "last played" timestamp and increment the total play count of the track. |
||
| 311 | */ |
||
| 312 | public function recordTrackPlayed(int $trackId, string $userId, ?\DateTime $timeOfPlay = null) : void { |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Adds a track if it does not exist already or updates an existing track |
||
| 322 | * @param string $title the title of the track |
||
| 323 | * @param int|null $number the number of the track |
||
| 324 | * @param int|null $discNumber the number of the disc |
||
| 325 | * @param int|null $year the year of the release |
||
| 326 | * @param int $genreId the genre id of the track |
||
| 327 | * @param int $artistId the artist id of the track |
||
| 328 | * @param int $albumId the album id of the track |
||
| 329 | * @param int $fileId the file id of the track |
||
| 330 | * @param string $mimetype the mimetype of the track |
||
| 331 | * @param string $userId the name of the user |
||
| 332 | * @param int $length track length in seconds |
||
| 333 | * @param int $bitrate track bitrate in bits (not kbits) |
||
| 334 | * @return \OCA\Music\Db\Track The added/updated track |
||
| 335 | */ |
||
| 336 | public function addOrUpdateTrack( |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Deletes a track |
||
| 357 | * @param int[] $fileIds file IDs of the tracks to delete |
||
| 358 | * @param string[]|null $userIds the target users; if omitted, the tracks matching the |
||
| 359 | * $fileIds are deleted from all users |
||
| 360 | * @return array|false False is returned if no such track was found; otherwise array of six arrays |
||
| 361 | * (named 'deletedTracks', 'remainingAlbums', 'remainingArtists', 'obsoleteAlbums', |
||
| 362 | * 'obsoleteArtists', and 'affectedUsers'). These contain the track, album, artist, and |
||
| 363 | * user IDs of the deleted tracks. The 'obsolete' entities are such which no longer |
||
| 364 | * have any tracks while 'remaining' entities have some left. |
||
| 365 | */ |
||
| 366 | public function deleteTracks($fileIds, $userIds = null) { |
||
| 426 | } |
||
| 427 | } |
||
| 428 |