phanan /
koel
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Models; |
||
| 4 | |||
| 5 | use App\Traits\SupportsDeleteWhereIDsNotIn; |
||
| 6 | use Illuminate\Database\Eloquent\Collection; |
||
| 7 | use Illuminate\Database\Eloquent\Model; |
||
| 8 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
| 9 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @property string $cover The album cover's file name |
||
| 13 | * @property string|null $cover_path The absolute path to the cover file |
||
| 14 | * @property bool $has_cover If the album has a cover image |
||
| 15 | * @property int $id |
||
| 16 | * @property string $name Name of the album |
||
| 17 | * @property bool $is_compilation If the album is a compilation from multiple artists |
||
| 18 | * @property Artist $artist The album's artist |
||
| 19 | * @property int $artist_id |
||
| 20 | * @property Collection $songs |
||
| 21 | * @property bool $is_unknown |
||
| 22 | * |
||
| 23 | * @method static self firstOrCreate(array $where, array $params = []) |
||
| 24 | */ |
||
| 25 | class Album extends Model |
||
| 26 | { |
||
| 27 | use SupportsDeleteWhereIDsNotIn; |
||
| 28 | |||
| 29 | const UNKNOWN_ID = 1; |
||
| 30 | const UNKNOWN_NAME = 'Unknown Album'; |
||
| 31 | const UNKNOWN_COVER = 'unknown-album.png'; |
||
| 32 | |||
| 33 | protected $guarded = ['id']; |
||
| 34 | protected $hidden = ['updated_at']; |
||
| 35 | protected $casts = ['artist_id' => 'integer']; |
||
| 36 | protected $appends = ['is_compilation']; |
||
| 37 | |||
| 38 | 50 | public function artist(): BelongsTo |
|
| 39 | { |
||
| 40 | 50 | return $this->belongsTo(Artist::class); |
|
| 41 | } |
||
| 42 | |||
| 43 | public function songs(): HasMany |
||
| 44 | { |
||
| 45 | return $this->hasMany(Song::class); |
||
| 46 | } |
||
| 47 | |||
| 48 | 1 | public function getIsUnknownAttribute(): bool |
|
| 49 | { |
||
| 50 | 1 | return $this->id === self::UNKNOWN_ID; |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get an album using some provided information. |
||
| 55 | * If such is not found, a new album will be created using the information. |
||
| 56 | */ |
||
| 57 | 17 | public static function get(Artist $artist, string $name, bool $isCompilation = false): self |
|
| 58 | { |
||
| 59 | // If this is a compilation album, its artist must be "Various Artists" |
||
| 60 | 17 | if ($isCompilation) { |
|
| 61 | 8 | $artist = Artist::getVariousArtist(); |
|
| 62 | } |
||
| 63 | |||
| 64 | 17 | return static::firstOrCreate([ |
|
| 65 | 17 | 'artist_id' => $artist->id, |
|
| 66 | 17 | 'name' => $name ?: self::UNKNOWN_NAME, |
|
| 67 | ]); |
||
| 68 | } |
||
| 69 | |||
| 70 | 132 | public function setCoverAttribute(?string $value): void |
|
| 71 | { |
||
| 72 | 132 | $this->attributes['cover'] = $value ?: self::UNKNOWN_COVER; |
|
| 73 | 132 | } |
|
| 74 | |||
| 75 | 9 | public function getCoverAttribute(?string $value): string |
|
| 76 | { |
||
| 77 | 9 | return app()->staticUrl('public/img/covers/'.($value ?: self::UNKNOWN_COVER)); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 78 | } |
||
| 79 | |||
| 80 | 8 | public function getHasCoverAttribute(): bool |
|
| 81 | { |
||
| 82 | 8 | $cover = array_get($this->attributes, 'cover'); |
|
| 83 | |||
| 84 | 8 | if (!$cover) { |
|
| 85 | 6 | return false; |
|
| 86 | } |
||
| 87 | |||
| 88 | 8 | if ($cover === self::UNKNOWN_COVER) { |
|
| 89 | 8 | return false; |
|
| 90 | } |
||
| 91 | |||
| 92 | 6 | return file_exists(public_path("/public/img/covers/$cover")); |
|
| 93 | } |
||
| 94 | |||
| 95 | public function getCoverPathAttribute(): ?string |
||
| 96 | { |
||
| 97 | $cover = array_get($this->attributes, 'cover'); |
||
| 98 | |||
| 99 | if (!$cover) { |
||
| 100 | return null; |
||
| 101 | } |
||
| 102 | |||
| 103 | return public_path("/public/img/covers/{$this->cover}"); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Sometimes the tags extracted from getID3 are HTML entity encoded. |
||
| 108 | * This makes sure they are always sane. |
||
| 109 | */ |
||
| 110 | 12 | public function getNameAttribute(string $value): string |
|
| 111 | { |
||
| 112 | 12 | return html_entity_decode($value); |
|
| 113 | } |
||
| 114 | |||
| 115 | 6 | public function getIsCompilationAttribute(): bool |
|
| 116 | { |
||
| 117 | 6 | return $this->artist_id === Artist::VARIOUS_ID; |
|
| 118 | } |
||
| 119 | } |
||
| 120 |