From 2c3ef557af727bf57349c3214aedf01c5044eee2 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Mon, 11 Jul 2016 11:25:45 +0100 Subject: [PATCH] #7: Implement CR changes --- app/Http/Controllers/AlbumsController.php | 2 +- app/Http/Controllers/PlaylistsController.php | 2 +- app/Models/Album.php | 14 +------------- app/Models/Playlist.php | 14 +------------- app/Models/Track.php | 15 ++++++++------- app/Models/TrackFile.php | 5 +++++ app/Traits/TrackCollection.php | 19 +++++++++++++++++++ 7 files changed, 36 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/AlbumsController.php b/app/Http/Controllers/AlbumsController.php index eed18935..45ace4fa 100644 --- a/app/Http/Controllers/AlbumsController.php +++ b/app/Http/Controllers/AlbumsController.php @@ -81,7 +81,7 @@ class AlbumsController extends Controller App::abort(404); } - if (!Album::hasLosslessTracks($album) && in_array($formatName, Track::$LosslessFormats)) { + if (!$album->hasLosslessTracksOnly() && in_array($formatName, Track::$LosslessFormats)) { App::abort(404); } diff --git a/app/Http/Controllers/PlaylistsController.php b/app/Http/Controllers/PlaylistsController.php index be1e1d58..bf7583c0 100644 --- a/app/Http/Controllers/PlaylistsController.php +++ b/app/Http/Controllers/PlaylistsController.php @@ -82,7 +82,7 @@ class PlaylistsController extends Controller App::abort(404); } - if (!Playlist::hasLosslessTracks($playlist) && in_array($formatName, Track::$LosslessFormats)) { + if (!$this->hasLosslessTracksOnly() && in_array($formatName, Track::$LosslessFormats)) { App::abort(404); } diff --git a/app/Models/Album.php b/app/Models/Album.php index 61ee75f7..cba028f9 100644 --- a/app/Models/Album.php +++ b/app/Models/Album.php @@ -134,18 +134,6 @@ class Album extends Model implements Searchable, Commentable, Favouritable return $this->morphMany(Activity::class, 'resource'); } - public static function hasLosslessTracks(Album $album) - { - $hasLosslessTracks = false; - foreach ($album->tracks as $track) { - if (!$track->isMasterLossy()) { - $hasLosslessTracks = true; - break; - } - } - return $hasLosslessTracks; - } - public static function mapPublicAlbumShow(Album $album) { $tracks = []; @@ -154,7 +142,7 @@ class Album extends Model implements Searchable, Commentable, Favouritable } $formats = []; - $hasLosslessTracks = Album::hasLosslessTracks($album); + $hasLosslessTracks = $album->hasLosslessTracksOnly(); foreach (Track::$Formats as $name => $format) { if (!$hasLosslessTracks && in_array($name, Track::$LosslessFormats)) { continue; diff --git a/app/Models/Playlist.php b/app/Models/Playlist.php index 809ad143..0059c843 100644 --- a/app/Models/Playlist.php +++ b/app/Models/Playlist.php @@ -107,18 +107,6 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable return !$query; } - public static function hasLosslessTracks(Playlist $playlist) - { - $hasLosslessTracks = false; - foreach ($playlist->tracks as $track) { - if (!$track->isMasterLossy()) { - $hasLosslessTracks = true; - break; - } - } - return $hasLosslessTracks; - } - public static function mapPublicPlaylistShow(Playlist $playlist) { $tracks = []; @@ -129,7 +117,7 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable } $formats = []; - $hasLosslessTracks = Playlist::hasLosslessTracks($playlist); + $hasLosslessTracks = $playlist->hasLosslessTracksOnly(); foreach (Track::$Formats as $name => $format) { if (!$hasLosslessTracks && in_array($name, Track::$LosslessFormats)) { continue; diff --git a/app/Models/Track.php b/app/Models/Track.php index 528816df..be5fc02f 100644 --- a/app/Models/Track.php +++ b/app/Models/Track.php @@ -633,14 +633,15 @@ class Track extends Model implements Searchable, Commentable, Favouritable return $this->published_at != null && $this->deleted_at == null; } - public function isMasterLossy() + + protected function getMasterTrackFile() : TrackFile { - return is_null( - $this->trackFiles->where('is_master', true) - ->first(function ($key, $trackFile) { - return in_array($trackFile->format, Track::$LosslessFormats); - }) - ); + return $this->trackFiles->where('is_master', true)->first(); + } + + public function isMasterLossy() : bool + { + return $this->getMasterTrackFile()->isLossy(); } public function getCoverUrl($type = Image::NORMAL) diff --git a/app/Models/TrackFile.php b/app/Models/TrackFile.php index ad6a0d10..a983f2b3 100644 --- a/app/Models/TrackFile.php +++ b/app/Models/TrackFile.php @@ -185,4 +185,9 @@ class TrackFile extends Model return $this->filesize; } + + public function isLossy() : bool + { + return !in_array($this->format, Track::$LosslessFormats); + } } diff --git a/app/Traits/TrackCollection.php b/app/Traits/TrackCollection.php index f9073b0b..2504c766 100644 --- a/app/Traits/TrackCollection.php +++ b/app/Traits/TrackCollection.php @@ -24,6 +24,7 @@ namespace Poniverse\Ponyfm\Traits; use File; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; use Poniverse\Ponyfm\Jobs\EncodeTrackFile; use Poniverse\Ponyfm\Models\TrackFile; @@ -123,4 +124,22 @@ trait TrackCollection } ])->where('format', $format)->get(); } + + /** + * Returns a boolean based on whether all (@link TrackFile)s + * for this (@link TrackCollection)'s tracks have lossless master files. + * + * @return bool + */ + public function hasLosslessTracksOnly() : bool + { + $hasLosslessTracksOnly = false; + foreach ($this->tracks as $track) { + if (!$track->isMasterLossy()) { + $hasLosslessTracksOnly = true; + break; + } + } + return $hasLosslessTracksOnly; + } }