| Total Complexity | 44 |
| Total Lines | 308 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Track 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 Track, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 65 | class Track extends Entity { |
||
| 66 | public $title; |
||
| 67 | public $number; |
||
| 68 | public $disk; |
||
| 69 | public $year; |
||
| 70 | public $artistId; |
||
| 71 | public $albumId; |
||
| 72 | public $length; |
||
| 73 | public $fileId; |
||
| 74 | public $bitrate; |
||
| 75 | public $mimetype; |
||
| 76 | public $mbid; |
||
| 77 | public $starred; |
||
| 78 | public $rating; |
||
| 79 | public $genreId; |
||
| 80 | public $playCount; |
||
| 81 | public $lastPlayed; |
||
| 82 | public $dirty; |
||
| 83 | |||
| 84 | // not from the music_tracks table but still part of the standard content of this entity: |
||
| 85 | public $filename; |
||
| 86 | public $size; |
||
| 87 | public $fileModTime; |
||
| 88 | public $albumName; |
||
| 89 | public $artistName; |
||
| 90 | public $genreName; |
||
| 91 | public $folderId; |
||
| 92 | |||
| 93 | // the rest of the variables are injected separately when needed |
||
| 94 | private ?Album $album = null; |
||
| 95 | private ?int $numberOnPlaylist = null; |
||
| 96 | private ?string $folderPath = null; |
||
| 97 | private ?string $lyrics = null; |
||
| 98 | |||
| 99 | public function __construct() { |
||
| 100 | $this->addType('number', 'int'); |
||
| 101 | $this->addType('disk', 'int'); |
||
| 102 | $this->addType('year', 'int'); |
||
| 103 | $this->addType('artistId', 'int'); |
||
| 104 | $this->addType('albumId', 'int'); |
||
| 105 | $this->addType('length', 'int'); |
||
| 106 | $this->addType('bitrate', 'int'); |
||
| 107 | $this->addType('fileId', 'int'); |
||
| 108 | $this->addType('genreId', 'int'); |
||
| 109 | $this->addType('playCount', 'int'); |
||
| 110 | $this->addType('rating', 'int'); |
||
| 111 | $this->addType('dirty', 'int'); |
||
| 112 | $this->addType('size', 'int'); |
||
| 113 | $this->addType('fileModTime', 'int'); |
||
| 114 | $this->addType('folderId', 'int'); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getAlbum() : ?Album { |
||
| 118 | return $this->album; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function setAlbum(?Album $album) : void { |
||
| 122 | $this->album = $album; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getNumberOnPlaylist() : ?int { |
||
| 126 | return $this->numberOnPlaylist; |
||
| 127 | } |
||
| 128 | |||
| 129 | public function setNumberOnPlaylist(int $number) { |
||
| 130 | $this->numberOnPlaylist = $number; |
||
| 131 | } |
||
| 132 | |||
| 133 | public function setFolderPath(string $path) : void { |
||
| 134 | $this->folderPath = $path; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function setLyrics(?string $lyrics) : void { |
||
| 138 | $this->lyrics = $lyrics; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function getPath() : ?string { |
||
| 142 | return ($this->folderPath ?? '') . '/' . $this->filename; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getUri(IURLGenerator $urlGenerator) : string { |
||
| 146 | return $urlGenerator->linkToRoute( |
||
| 147 | 'music.shivaApi.track', |
||
| 148 | ['id' => $this->id] |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function getArtistWithUri(IURLGenerator $urlGenerator) : array { |
||
| 153 | return [ |
||
| 154 | 'id' => $this->artistId, |
||
| 155 | 'uri' => $urlGenerator->linkToRoute( |
||
| 156 | 'music.shivaApi.artist', |
||
| 157 | ['id' => $this->artistId] |
||
| 158 | ) |
||
| 159 | ]; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function getAlbumWithUri(IURLGenerator $urlGenerator) : array { |
||
| 168 | ) |
||
| 169 | ]; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function getArtistNameString(IL10N $l10n) : string { |
||
| 173 | return $this->getArtistName() ?: Artist::unknownNameString($l10n); |
||
| 174 | } |
||
| 175 | |||
| 176 | public function getAlbumNameString(IL10N $l10n) : string { |
||
| 177 | return $this->getAlbumName() ?: Album::unknownNameString($l10n); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getGenreNameString(IL10N $l10n) : string { |
||
| 181 | return $this->getGenreName() ?: Genre::unknownNameString($l10n); |
||
| 182 | } |
||
| 183 | |||
| 184 | public function toCollection() : array { |
||
| 185 | return [ |
||
| 186 | 'title' => $this->getTitle(), |
||
| 187 | 'number' => $this->getNumber(), |
||
| 188 | 'disk' => $this->getDisk(), |
||
| 189 | 'artistId' => $this->getArtistId(), |
||
| 190 | 'length' => $this->getLength(), |
||
| 191 | 'files' => [$this->getMimetype() => $this->getFileId()], |
||
| 192 | 'id' => $this->getId(), |
||
| 193 | ]; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function toShivaApi(IURLGenerator $urlGenerator) : array { |
||
| 197 | return [ |
||
| 198 | 'title' => $this->getTitle(), |
||
| 199 | 'ordinal' => $this->getAdjustedTrackNumber(), |
||
| 200 | 'artist' => $this->getArtistWithUri($urlGenerator), |
||
| 201 | 'album' => $this->getAlbumWithUri($urlGenerator), |
||
| 202 | 'length' => $this->getLength(), |
||
| 203 | 'files' => [$this->getMimetype() => $urlGenerator->linkToRoute( |
||
| 204 | 'music.musicApi.download', |
||
| 205 | ['fileId' => $this->getFileId()] |
||
| 206 | )], |
||
| 207 | 'bitrate' => $this->getBitrate(), |
||
| 208 | 'id' => $this->getId(), |
||
| 209 | 'slug' => $this->slugify('title'), |
||
| 210 | 'uri' => $this->getUri($urlGenerator) |
||
| 211 | ]; |
||
| 212 | } |
||
| 213 | |||
| 214 | public function toAmpacheApi( |
||
| 215 | IL10N $l10n, |
||
| 216 | callable $createPlayUrl, |
||
| 217 | callable $createImageUrl, |
||
| 218 | callable $renderAlbumOrArtistRef, |
||
| 219 | string $genreKey, |
||
| 220 | bool $includeArtists) : array { |
||
| 221 | $album = $this->getAlbum(); |
||
| 222 | |||
| 223 | $result = [ |
||
| 224 | 'id' => (string)$this->getId(), |
||
| 225 | 'title' => $this->getTitle() ?: '', |
||
| 226 | 'name' => $this->getTitle() ?: '', |
||
| 227 | 'artist' => $renderAlbumOrArtistRef($this->getArtistId() ?: 0, $this->getArtistNameString($l10n)), |
||
| 228 | 'albumartist' => $renderAlbumOrArtistRef($album->getAlbumArtistId() ?: 0, $album->getAlbumArtistNameString($l10n)), |
||
| 229 | 'album' => $renderAlbumOrArtistRef($album->getId() ?: 0, $album->getNameString($l10n)), |
||
| 230 | 'url' => $createPlayUrl($this), |
||
| 231 | 'time' => $this->getLength(), |
||
| 232 | 'year' => $this->getYear(), |
||
| 233 | 'track' => $this->getAdjustedTrackNumber(), // TODO: maybe there should be a user setting to select plain or adjusted number |
||
| 234 | 'playlisttrack' => $this->getAdjustedTrackNumber(), |
||
| 235 | 'disk' => $this->getDisk(), |
||
| 236 | 'filename' => $this->getFilename(), |
||
| 237 | 'format' => $this->getFileExtension(), |
||
| 238 | 'stream_format' => $this->getFileExtension(), |
||
| 239 | 'bitrate' => $this->getBitrate(), |
||
| 240 | 'stream_bitrate' => $this->getBitrate(), |
||
| 241 | 'mime' => $this->getMimetype(), |
||
| 242 | 'stream_mime' => $this->getMimetype(), |
||
| 243 | 'size' => $this->getSize(), |
||
| 244 | 'art' => $createImageUrl($this), |
||
| 245 | 'rating' => $this->getRating() ?? 0, |
||
| 246 | 'preciserating' => $this->getRating() ?? 0, |
||
| 247 | 'playcount' => $this->getPlayCount(), |
||
| 248 | 'flag' => !empty($this->getStarred()), |
||
| 249 | 'language' => null, |
||
| 250 | 'lyrics' => $this->lyrics, |
||
| 251 | 'mode' => null, // cbr/vbr |
||
| 252 | 'rate' => null, // sample rate [Hz] |
||
| 253 | 'replaygain_album_gain' => null, |
||
| 254 | 'replaygain_album_peak' => null, |
||
| 255 | 'replaygain_track_gain' => null, |
||
| 256 | 'replaygain_track_peak' => null, |
||
| 257 | 'r128_album_gain' => null, |
||
| 258 | 'r128_track_gain' => null, |
||
| 259 | ]; |
||
| 260 | |||
| 261 | $result['has_art'] = !empty($result['art']); |
||
| 262 | |||
| 263 | $genreId = $this->getGenreId(); |
||
| 264 | if ($genreId !== null) { |
||
| 265 | $result[$genreKey] = [[ |
||
| 266 | 'id' => (string)$genreId, |
||
| 267 | 'text' => $this->getGenreNameString($l10n), |
||
| 268 | 'count' => 1 |
||
| 269 | ]]; |
||
| 270 | } |
||
| 271 | |||
| 272 | if ($includeArtists) { |
||
| 273 | // Add another property `artists`. Apparently, it exists to support multiple artists per song |
||
| 274 | // but we don't have such possibility and this is always just a 1-item array. |
||
| 275 | $result['artists'] = [$result['artist']]; |
||
| 276 | } |
||
| 277 | |||
| 278 | return $result; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * The same API format is used both on "old" and "new" API methods. The "new" API adds some |
||
| 283 | * new fields for the songs, but providing some extra fields shouldn't be a problem for the |
||
| 284 | * older clients. The $track entity must have the Album reference injected prior to calling this. |
||
| 285 | * |
||
| 286 | * @param string[] $ignoredArticles |
||
| 287 | */ |
||
| 288 | public function toSubsonicApi(IL10N $l10n, array $ignoredArticles) : array { |
||
| 289 | $albumId = $this->getAlbumId(); |
||
| 290 | $album = $this->getAlbum(); |
||
| 291 | $hasCoverArt = ($album !== null && !empty($album->getCoverFileId())); |
||
| 292 | |||
| 293 | return [ |
||
| 294 | 'id' => 'track-' . $this->getId(), |
||
| 295 | 'parent' => 'album-' . $albumId, |
||
| 296 | 'discNumber' => $this->getDisk(), |
||
| 297 | 'title' => $this->getTitle(), |
||
| 298 | 'artist' => $this->getArtistNameString($l10n), |
||
| 299 | 'isDir' => false, |
||
| 300 | 'album' => $this->getAlbumNameString($l10n), |
||
| 301 | 'year' => $this->getYear(), |
||
| 302 | 'size' => $this->getSize(), |
||
| 303 | 'contentType' => $this->getMimetype(), |
||
| 304 | 'suffix' => $this->getFileExtension(), |
||
| 305 | 'duration' => $this->getLength() ?? 0, |
||
| 306 | 'bitRate' => empty($this->getBitrate()) ? null : (int)\round($this->getBitrate()/1000), // convert bps to kbps |
||
| 307 | 'path' => $this->getPath(), |
||
| 308 | 'isVideo' => false, |
||
| 309 | 'albumId' => 'album-' . $albumId, |
||
| 310 | 'artistId' => 'artist-' . $this->getArtistId(), |
||
| 311 | 'type' => 'music', |
||
| 312 | 'created' => Util::formatZuluDateTime($this->getCreated()), |
||
| 313 | 'track' => $this->getAdjustedTrackNumber(false), // DSub would get confused of playlist numbering, https://github.com/owncloud/music/issues/994 |
||
| 314 | 'starred' => Util::formatZuluDateTime($this->getStarred()), |
||
| 315 | 'userRating' => $this->getRating() ?: null, |
||
| 316 | 'averageRating' => $this->getRating() ?: null, |
||
| 317 | 'genre' => empty($this->getGenreId()) ? null : $this->getGenreNameString($l10n), |
||
| 318 | 'coverArt' => !$hasCoverArt ? null : 'album-' . $albumId, |
||
| 319 | 'playCount' => $this->getPlayCount(), |
||
| 320 | 'played' => Util::formatZuluDateTime($this->getLastPlayed()) ?? '', // OpenSubsonic |
||
| 321 | 'sortName' => Util::splitPrefixAndBasename($this->getTitle(), $ignoredArticles)['basename'], // OpenSubsonic |
||
| 322 | ]; |
||
| 323 | } |
||
| 324 | |||
| 325 | public function getAdjustedTrackNumber(bool $enablePlaylistNumbering=true) : ?int { |
||
| 345 | } |
||
| 346 | |||
| 347 | public function getFileExtension() : string { |
||
| 348 | $parts = Util::explode('.', $this->getFilename()); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get an instance which has all the mandatory fields set to valid but empty values |
||
| 354 | */ |
||
| 355 | public static function emptyInstance() : Track { |
||
| 356 | $track = new self(); |
||
| 357 | |||
| 358 | $track->id = -1; |
||
| 359 | $track->title = ''; |
||
| 360 | $track->artistId = -1; |
||
| 361 | $track->albumId = -1; |
||
| 362 | $track->fileId = -1; |
||
| 363 | $track->mimetype = ''; |
||
| 373 | } |
||
| 374 | } |
||
| 375 |