/** * Get an album using some provided information. * * @param Artist $artist * @param string $name * @param bool $isCompilation * * @return self */ public static function get(Artist $artist, $name, $isCompilation = false) { // If this is a compilation album, its artist must be "Various Artists" if ($isCompilation) { $artist = Artist::getVarious(); } return self::firstOrCreate(['artist_id' => $artist->id, 'name' => $name ?: self::UNKNOWN_NAME]); }
/** * Update a single song's info. * * @param string $title * @param string $albumName * @param string $artistName * @param string $lyrics * @param int $track * @param int $compilationState * * @return self */ public function updateSingle($title, $albumName, $artistName, $lyrics, $track, $compilationState) { // If the artist name is "Various Artists", it's a compilation song no matter what. if ($artistName === Artist::VARIOUS_NAME) { $compilationState = 1; } // If the complitation state is "no change," we determine it via the current // "contributing_artist_id" field value. if ($compilationState === 2) { $compilationState = $this->contributing_artist_id ? 1 : 0; } $album = null; if ($compilationState === 0) { // Not a compilation song $this->contributing_artist_id = null; $albumArtist = Artist::get($artistName); $album = Album::get($albumArtist, $albumName, false); } else { $contributingArtist = Artist::get($artistName); $this->contributing_artist_id = $contributingArtist->id; $album = Album::get(Artist::getVarious(), $albumName, true); } $this->album_id = $album->id; $this->lyrics = $lyrics; $this->track = $track; $this->save(); // Get the updated record, with album and all. $updatedSong = self::with('album', 'album.artist', 'contributingArtist')->find($this->id); // Make sure lyrics is included in the returned JSON. $updatedSong->makeVisible('lyrics'); return $updatedSong; }