| Total Complexity | 68 |
| Total Lines | 498 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AlbumMapper 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 AlbumMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 26 | class AlbumMapper extends BaseMapper { |
||
| 27 | public function __construct(IDBConnection $db) { |
||
| 28 | parent::__construct($db, 'music_albums', Album::class, 'name'); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Override the base implementation to include data from multiple tables |
||
| 33 | * |
||
| 34 | * {@inheritdoc} |
||
| 35 | * @see BaseMapper::selectEntities() |
||
| 36 | */ |
||
| 37 | protected function selectEntities(string $condition, string $extension=null) : string { |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Overridden from \OCA\Music\Db\BaseMapper to add support for sorting by artist. |
||
| 47 | * |
||
| 48 | * {@inheritdoc} |
||
| 49 | * @see BaseMapper::formatSortingClause() |
||
| 50 | */ |
||
| 51 | protected function formatSortingClause(int $sortBy, bool $invertSort = false) : ?string { |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Overridden from the base implementation to provide support for table-specific rules |
||
| 63 | * |
||
| 64 | * {@inheritdoc} |
||
| 65 | * @see BaseMapper::advFormatSqlCondition() |
||
| 66 | */ |
||
| 67 | protected function advFormatSqlCondition(string $rule, string $sqlOp) : string { |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * returns artist IDs mapped to album IDs |
||
| 101 | * does not include album_artist_id |
||
| 102 | * |
||
| 103 | * @param integer[]|null $albumIds IDs of the albums; get all albums of the user if null given |
||
| 104 | * @param string $userId the user ID |
||
| 105 | * @return array int => int[], keys are albums IDs and values are arrays of artist IDs |
||
| 106 | */ |
||
| 107 | public function getPerformingArtistsByAlbumId(?array $albumIds, string $userId) : array { |
||
| 108 | $sql = 'SELECT DISTINCT `track`.`album_id`, `track`.`artist_id` |
||
| 109 | FROM `*PREFIX*music_tracks` `track` |
||
| 110 | WHERE `track`.`user_id` = ? '; |
||
| 111 | $params = [$userId]; |
||
| 112 | |||
| 113 | if ($albumIds !== null) { |
||
|
|
|||
| 114 | $sql .= 'AND `track`.`album_id` IN ' . $this->questionMarks(\count($albumIds)); |
||
| 115 | $params = \array_merge($params, $albumIds); |
||
| 116 | } |
||
| 117 | |||
| 118 | $result = $this->execute($sql, $params); |
||
| 119 | $artistIds = []; |
||
| 120 | while ($row = $result->fetch()) { |
||
| 121 | $artistIds[$row['album_id']][] = (int)$row['artist_id']; |
||
| 122 | } |
||
| 123 | return $artistIds; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * returns release years mapped to album IDs |
||
| 128 | * |
||
| 129 | * @param integer[]|null $albumIds IDs of the albums; get all albums of the user if null given |
||
| 130 | * @param string $userId the user ID |
||
| 131 | * @return array int => int[], keys are albums IDs and values are arrays of years |
||
| 132 | */ |
||
| 133 | public function getYearsByAlbumId(?array $albumIds, string $userId) : array { |
||
| 134 | $sql = 'SELECT DISTINCT `track`.`album_id`, `track`.`year` |
||
| 135 | FROM `*PREFIX*music_tracks` `track` |
||
| 136 | WHERE `track`.`user_id` = ? |
||
| 137 | AND `track`.`year` IS NOT NULL '; |
||
| 138 | $params = [$userId]; |
||
| 139 | |||
| 140 | if ($albumIds !== null) { |
||
| 141 | $sql .= 'AND `track`.`album_id` IN ' . $this->questionMarks(\count($albumIds)); |
||
| 142 | $params = \array_merge($params, $albumIds); |
||
| 143 | } |
||
| 144 | |||
| 145 | $result = $this->execute($sql, $params); |
||
| 146 | $years = []; |
||
| 147 | while ($row = $result->fetch()) { |
||
| 148 | $years[$row['album_id']][] = (int)$row['year']; |
||
| 149 | } |
||
| 150 | return $years; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * returns genres mapped to album IDs |
||
| 155 | * |
||
| 156 | * @param integer[]|null $albumIds IDs of the albums; get all albums of the user if null given |
||
| 157 | * @param string $userId the user ID |
||
| 158 | * @return array int => Genre[], keys are albums IDs and values are arrays of *partial* Genre objects (only id and name properties set) |
||
| 159 | */ |
||
| 160 | public function getGenresByAlbumId(?array $albumIds, string $userId) : array { |
||
| 161 | $sql = 'SELECT DISTINCT `album_id`, `genre_id`, `*PREFIX*music_genres`.`name` AS `genre_name` |
||
| 162 | FROM `*PREFIX*music_tracks` |
||
| 163 | LEFT JOIN `*PREFIX*music_genres` |
||
| 164 | ON `genre_id` = `*PREFIX*music_genres`.`id` |
||
| 165 | WHERE `*PREFIX*music_tracks`.`user_id` = ? |
||
| 166 | AND `genre_id` IS NOT NULL '; |
||
| 167 | $params = [$userId]; |
||
| 168 | |||
| 169 | if ($albumIds !== null) { |
||
| 170 | $sql .= 'AND `album_id` IN ' . $this->questionMarks(\count($albumIds)); |
||
| 171 | $params = \array_merge($params, $albumIds); |
||
| 172 | } |
||
| 173 | |||
| 174 | $result = $this->execute($sql, $params); |
||
| 175 | $genres = []; |
||
| 176 | while ($row = $result->fetch()) { |
||
| 177 | $genre = new Genre(); |
||
| 178 | $genre->setUserId($userId); |
||
| 179 | $genre->setId((int)$row['genre_id']); |
||
| 180 | $genre->setName($row['genre_name']); |
||
| 181 | $genres[$row['album_id']][] = $genre; |
||
| 182 | } |
||
| 183 | return $genres; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * returns number of disks per album ID |
||
| 188 | * |
||
| 189 | * @param integer[]|null $albumIds IDs of the albums; get all albums of the user if null given |
||
| 190 | * @param string $userId the user ID |
||
| 191 | * @return array int => int, keys are albums IDs and values are disk counts |
||
| 192 | */ |
||
| 193 | public function getDiscCountByAlbumId(?array $albumIds, string $userId) : array { |
||
| 194 | $sql = 'SELECT `album_id`, MAX(`disk`) AS `disc_count` |
||
| 195 | FROM `*PREFIX*music_tracks` |
||
| 196 | WHERE `user_id` = ? |
||
| 197 | GROUP BY `album_id` '; |
||
| 198 | $params = [$userId]; |
||
| 199 | |||
| 200 | if ($albumIds !== null) { |
||
| 201 | $sql .= 'HAVING `album_id` IN ' . $this->questionMarks(\count($albumIds)); |
||
| 202 | $params = \array_merge($params, $albumIds); |
||
| 203 | } |
||
| 204 | |||
| 205 | $result = $this->execute($sql, $params); |
||
| 206 | $diskCountByAlbum = []; |
||
| 207 | while ($row = $result->fetch()) { |
||
| 208 | $diskCountByAlbum[$row['album_id']] = (int)$row['disc_count']; |
||
| 209 | } |
||
| 210 | return $diskCountByAlbum; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * returns summed track play counts of each album of the user, omittig albums which have never been played |
||
| 215 | * |
||
| 216 | * @return array [int => int], keys are album IDs and values are play count sums; ordered largest counts first |
||
| 217 | */ |
||
| 218 | public function getAlbumTracksPlayCount(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * returns the latest play time of each album of the user, omittig albums which have never been played |
||
| 235 | * |
||
| 236 | * @return array [int => string], keys are album IDs and values are date-times; ordered latest times first |
||
| 237 | */ |
||
| 238 | public function getLatestAlbumPlayTimes(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 239 | $sql = 'SELECT `album_id`, MAX(`last_played`) AS `latest_time` |
||
| 240 | FROM `*PREFIX*music_tracks` |
||
| 241 | WHERE `user_id` = ? AND `last_played` IS NOT NULL |
||
| 242 | GROUP BY `album_id` |
||
| 243 | ORDER BY `latest_time` DESC'; |
||
| 244 | |||
| 245 | $result = $this->execute($sql, [$userId], $limit, $offset); |
||
| 246 | $latestTimeByAlbum = []; |
||
| 247 | while ($row = $result->fetch()) { |
||
| 248 | $latestTimeByAlbum[$row['album_id']] = $row['latest_time']; |
||
| 249 | } |
||
| 250 | return $latestTimeByAlbum; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * returns the latest play time of each album of the user, including albums which have never been played |
||
| 255 | * |
||
| 256 | * @return array [int => ?string], keys are album IDs and values are date-times (or null for never played); |
||
| 257 | * ordered furthest times first |
||
| 258 | */ |
||
| 259 | public function getFurthestAlbumPlayTimes(string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 260 | $sql = 'SELECT `album_id`, MAX(`last_played`) AS `latest_time` |
||
| 261 | FROM `*PREFIX*music_tracks` |
||
| 262 | WHERE `user_id` = ? |
||
| 263 | GROUP BY `album_id` |
||
| 264 | ORDER BY `latest_time` ASC'; |
||
| 265 | |||
| 266 | $result = $this->execute($sql, [$userId], $limit, $offset); |
||
| 267 | $latestTimeByAlbum = []; |
||
| 268 | while ($row = $result->fetch()) { |
||
| 269 | $latestTimeByAlbum[$row['album_id']] = $row['latest_time']; |
||
| 270 | } |
||
| 271 | return $latestTimeByAlbum; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return Album[] |
||
| 276 | */ |
||
| 277 | public function findAllByNameRecursive(string $name, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 278 | $condition = '( LOWER(`artist`.`name`) LIKE LOWER(?) OR |
||
| 279 | LOWER(`*PREFIX*music_albums`.`name`) LIKE LOWER(?) )'; |
||
| 280 | $sql = $this->selectUserEntities($condition, 'ORDER BY LOWER(`*PREFIX*music_albums`.`name`)'); |
||
| 281 | $name = BaseMapper::prepareSubstringSearchPattern($name); |
||
| 282 | $params = [$userId, $name, $name]; |
||
| 283 | return $this->findEntities($sql, $params, $limit, $offset); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * returns albums of a specified artist |
||
| 288 | * The artist may be an album_artist or the artist of a track |
||
| 289 | * |
||
| 290 | * @param integer $artistId ID of the artist |
||
| 291 | * @param string $userId the user ID |
||
| 292 | * @return Album[] |
||
| 293 | */ |
||
| 294 | public function findAllByArtist(int $artistId, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * returns albums of a specified artist |
||
| 312 | * The artist must album_artist on the album, artists of individual tracks are not considered |
||
| 313 | * |
||
| 314 | * @param integer $artistId ID of the artist |
||
| 315 | * @param string $userId the user ID |
||
| 316 | * @return Album[] |
||
| 317 | */ |
||
| 318 | public function findAllByAlbumArtist(int $artistId, string $userId, ?int $limit=null, ?int $offset=null) : array { |
||
| 319 | $sql = $this->selectUserEntities('`album_artist_id` = ?'); |
||
| 320 | $params = [$userId, $artistId]; |
||
| 321 | return $this->findEntities($sql, $params, $limit, $offset); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return Album[] |
||
| 326 | */ |
||
| 327 | public function findAllByGenre(int $genreId, string $userId, int $limit=null, int $offset=null) : array { |
||
| 328 | $sql = $this->selectUserEntities('EXISTS '. |
||
| 329 | '(SELECT 1 FROM `*PREFIX*music_tracks` `track` |
||
| 330 | WHERE `*PREFIX*music_albums`.`id` = `track`.`album_id` |
||
| 331 | AND `track`.`genre_id` = ?)'); |
||
| 332 | |||
| 333 | $params = [$userId, $genreId]; |
||
| 334 | return $this->findEntities($sql, $params, $limit, $offset); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return boolean True if one or more albums were influenced |
||
| 339 | */ |
||
| 340 | public function updateFolderCover(int $coverFileId, int $folderId) : bool { |
||
| 341 | $sql = 'SELECT DISTINCT `tracks`.`album_id` |
||
| 342 | FROM `*PREFIX*music_tracks` `tracks` |
||
| 343 | JOIN `*PREFIX*filecache` `files` ON `tracks`.`file_id` = `files`.`fileid` |
||
| 344 | WHERE `files`.`parent` = ?'; |
||
| 345 | $params = [$folderId]; |
||
| 346 | $result = $this->execute($sql, $params); |
||
| 347 | |||
| 348 | $updated = false; |
||
| 349 | if ($result->rowCount()) { |
||
| 350 | $sql = 'UPDATE `*PREFIX*music_albums` |
||
| 351 | SET `cover_file_id` = ? |
||
| 352 | WHERE `cover_file_id` IS NULL AND `id` IN (?)'; |
||
| 353 | $params = [$coverFileId, \join(",", $result->fetchAll(\PDO::FETCH_COLUMN))]; |
||
| 354 | $result = $this->execute($sql, $params); |
||
| 355 | $updated = $result->rowCount() > 0; |
||
| 356 | } |
||
| 357 | |||
| 358 | return $updated; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Set file ID to be used as cover for an album |
||
| 363 | */ |
||
| 364 | public function setCover(?int $coverFileId, int $albumId) : void { |
||
| 365 | $sql = 'UPDATE `*PREFIX*music_albums` |
||
| 366 | SET `cover_file_id` = ? |
||
| 367 | WHERE `id` = ?'; |
||
| 368 | $params = [$coverFileId, $albumId]; |
||
| 369 | $this->execute($sql, $params); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param integer[] $coverFileIds |
||
| 374 | * @param string[]|null $userIds the users whose music library is targeted; all users are targeted if omitted |
||
| 375 | * @return Album[] albums which got modified (with incomplete data, only id and user are valid), |
||
| 376 | * empty array if none |
||
| 377 | */ |
||
| 378 | public function removeCovers(array $coverFileIds, array $userIds=null) : array { |
||
| 379 | // find albums using the given file as cover |
||
| 380 | $sql = 'SELECT `id`, `user_id` FROM `*PREFIX*music_albums` WHERE `cover_file_id` IN ' . |
||
| 381 | $this->questionMarks(\count($coverFileIds)); |
||
| 382 | $params = $coverFileIds; |
||
| 383 | if ($userIds !== null) { |
||
| 384 | $sql .= ' AND `user_id` IN ' . $this->questionMarks(\count($userIds)); |
||
| 385 | $params = \array_merge($params, $userIds); |
||
| 386 | } |
||
| 387 | $albums = $this->findEntities($sql, $params); |
||
| 388 | |||
| 389 | // if any albums found, remove the cover from those |
||
| 390 | $count = \count($albums); |
||
| 391 | if ($count) { |
||
| 392 | $sql = 'UPDATE `*PREFIX*music_albums` |
||
| 393 | SET `cover_file_id` = NULL |
||
| 394 | WHERE `id` IN ' . $this->questionMarks($count); |
||
| 395 | $params = Util::extractIds($albums); |
||
| 396 | $this->execute($sql, $params); |
||
| 397 | } |
||
| 398 | |||
| 399 | return $albums; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param string|null $userId target user; omit to target all users |
||
| 404 | * @return array of dictionaries with keys [albumId, userId, parentFolderId] |
||
| 405 | */ |
||
| 406 | public function getAlbumsWithoutCover(string $userId = null) : array { |
||
| 407 | $sql = 'SELECT DISTINCT `albums`.`id`, `albums`.`user_id`, `files`.`parent` |
||
| 408 | FROM `*PREFIX*music_albums` `albums` |
||
| 409 | JOIN `*PREFIX*music_tracks` `tracks` ON `albums`.`id` = `tracks`.`album_id` |
||
| 410 | JOIN `*PREFIX*filecache` `files` ON `tracks`.`file_id` = `files`.`fileid` |
||
| 411 | WHERE `albums`.`cover_file_id` IS NULL'; |
||
| 412 | $params = []; |
||
| 413 | if ($userId !== null) { |
||
| 414 | $sql .= ' AND `albums`.`user_id` = ?'; |
||
| 415 | $params[] = $userId; |
||
| 416 | } |
||
| 417 | $result = $this->execute($sql, $params); |
||
| 418 | $return = []; |
||
| 419 | while ($row = $result->fetch()) { |
||
| 420 | $return[] = [ |
||
| 421 | 'albumId' => (int)$row['id'], |
||
| 422 | 'userId' => $row['user_id'], |
||
| 423 | 'parentFolderId' => (int)$row['parent'] |
||
| 424 | ]; |
||
| 425 | } |
||
| 426 | return $return; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return boolean True if a cover image was found and added for the album |
||
| 431 | */ |
||
| 432 | public function findAlbumCover(int $albumId, int $parentFolderId) : bool { |
||
| 433 | $return = false; |
||
| 434 | $imagesSql = 'SELECT `fileid`, `name` |
||
| 435 | FROM `*PREFIX*filecache` |
||
| 436 | JOIN `*PREFIX*mimetypes` ON `*PREFIX*mimetypes`.`id` = `*PREFIX*filecache`.`mimetype` |
||
| 437 | WHERE `parent` = ? AND `*PREFIX*mimetypes`.`mimetype` LIKE \'image%\''; |
||
| 438 | $params = [$parentFolderId]; |
||
| 439 | $result = $this->execute($imagesSql, $params); |
||
| 440 | $images = $result->fetchAll(); |
||
| 441 | if (\count($images) > 0) { |
||
| 442 | $getImageRank = function($imageName) { |
||
| 443 | $coverNames = ['cover', 'albumart', 'album', 'front', 'folder']; |
||
| 444 | foreach ($coverNames as $i => $coverName) { |
||
| 445 | if (Util::startsWith($imageName, $coverName, /*$ignoreCase=*/true)) { |
||
| 446 | return $i; |
||
| 447 | } |
||
| 448 | } |
||
| 449 | return \count($coverNames); |
||
| 450 | }; |
||
| 451 | |||
| 452 | \usort($images, function ($imageA, $imageB) use ($getImageRank) { |
||
| 453 | return $getImageRank($imageA['name']) <=> $getImageRank($imageB['name']); |
||
| 454 | }); |
||
| 455 | $imageId = (int)$images[0]['fileid']; |
||
| 456 | $this->setCover($imageId, $albumId); |
||
| 457 | $return = true; |
||
| 458 | } |
||
| 459 | return $return; |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Given an array of track IDs, find corresponding uniqu album IDs, including only |
||
| 464 | * those album which have a cover art set. |
||
| 465 | * @param int[] $trackIds |
||
| 466 | * @return Album[] |
||
| 467 | */ |
||
| 468 | public function findAlbumsWithCoversForTracks(array $trackIds, string $userId, int $limit) : array { |
||
| 469 | $sql = 'SELECT DISTINCT `albums`.* |
||
| 470 | FROM `*PREFIX*music_albums` `albums` |
||
| 471 | JOIN `*PREFIX*music_tracks` `tracks` ON `albums`.`id` = `tracks`.`album_id` |
||
| 472 | WHERE `albums`.`cover_file_id` IS NOT NULL |
||
| 473 | AND `albums`.`user_id` = ? |
||
| 474 | AND `tracks`.`id` IN ' . $this->questionMarks(\count($trackIds)); |
||
| 475 | $params = \array_merge([$userId], $trackIds); |
||
| 476 | |||
| 477 | return $this->findEntities($sql, $params, $limit); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Returns the count of albums where the given Artist is featured in |
||
| 482 | * @param integer $artistId |
||
| 483 | * @return integer |
||
| 484 | */ |
||
| 485 | public function countByArtist(int $artistId) : int { |
||
| 486 | $sql = 'SELECT COUNT(*) AS count FROM ( |
||
| 487 | SELECT DISTINCT `track`.`album_id` |
||
| 488 | FROM `*PREFIX*music_tracks` `track` |
||
| 489 | WHERE `track`.`artist_id` = ? |
||
| 490 | UNION |
||
| 491 | SELECT `album`.`id` |
||
| 492 | FROM `*PREFIX*music_albums` `album` |
||
| 493 | WHERE `album`.`album_artist_id` = ? |
||
| 494 | ) tmp'; |
||
| 495 | $params = [$artistId, $artistId]; |
||
| 496 | $result = $this->execute($sql, $params); |
||
| 497 | $row = $result->fetch(); |
||
| 498 | return (int)$row['count']; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns the count of albums where the given artist is the album artist |
||
| 503 | * @param integer $artistId |
||
| 504 | * @return integer |
||
| 505 | */ |
||
| 506 | public function countByAlbumArtist(int $artistId) : int { |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @see \OCA\Music\Db\BaseMapper::findUniqueEntity() |
||
| 518 | * @param Album $album |
||
| 519 | * @return Album |
||
| 520 | */ |
||
| 521 | protected function findUniqueEntity(Entity $album) : Entity { |
||
| 524 | } |
||
| 525 | } |
||
| 526 |