koel/app/Models/Artist.php

145 lines
4.5 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
2015-12-17 17:56:48 +00:00
use App\Facades\Util;
2022-06-10 10:47:46 +00:00
use Illuminate\Contracts\Database\Query\Builder as BuilderContract;
2020-09-06 18:21:39 +00:00
use Illuminate\Database\Eloquent\Builder;
2022-07-18 11:00:37 +00:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2017-08-05 18:55:02 +00:00
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2015-12-17 18:00:42 +00:00
use Illuminate\Database\Eloquent\Model;
2017-08-05 18:55:02 +00:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2022-06-10 10:47:46 +00:00
use Illuminate\Database\Query\JoinClause;
2022-07-18 11:00:37 +00:00
use Illuminate\Support\Arr;
2022-06-10 10:47:46 +00:00
use Illuminate\Support\Facades\DB;
2020-12-23 10:53:00 +00:00
use Laravel\Scout\Searchable;
2015-12-13 04:42:28 +00:00
/**
2020-12-23 10:53:00 +00:00
* @property int $id
* @property string $name
* @property string|null $image Public URL to the artist's image
* @property bool $is_unknown If the artist is Unknown Artist
* @property bool $is_various If the artist is Various Artist
* @property Collection $songs
* @property bool $has_image If the artist has a (non-default) image
* @property string|null $image_path Absolute path to the artist's image
2019-08-05 10:57:36 +00:00
*
2019-08-05 10:56:48 +00:00
* @method static self find(int $id)
2019-11-29 21:23:30 +00:00
* @method static self firstOrCreate(array $where, array $params = [])
2020-09-06 18:21:39 +00:00
* @method static Builder where(...$params)
* @method static self first()
* @method static Builder whereName(string $name)
2020-12-22 23:01:49 +00:00
* @method static Builder orderBy(...$params)
2022-06-10 10:47:46 +00:00
* @method static Builder join(...$params)
2015-12-13 04:42:28 +00:00
*/
class Artist extends Model
{
use HasFactory;
2020-12-23 10:53:00 +00:00
use Searchable;
2016-09-26 07:32:16 +00:00
use SupportsDeleteWhereIDsNotIn;
2020-12-22 20:11:22 +00:00
public const UNKNOWN_ID = 1;
public const UNKNOWN_NAME = 'Unknown Artist';
public const VARIOUS_ID = 2;
public const VARIOUS_NAME = 'Various Artists';
2015-12-13 04:42:28 +00:00
protected $guarded = ['id'];
protected $hidden = ['created_at', 'updated_at'];
2021-06-05 10:47:56 +00:00
/**
* Get an Artist object from their name.
* If such is not found, a new artist will be created.
*/
public static function getOrCreate(?string $name = null): self
{
// Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
$encoding = Util::detectUTFEncoding($name);
if ($encoding) {
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
}
return static::firstOrCreate(['name' => trim($name) ?: self::UNKNOWN_NAME]);
}
2018-08-24 15:27:19 +00:00
public function albums(): HasMany
2015-12-13 04:42:28 +00:00
{
return $this->hasMany(Album::class);
}
2022-06-10 10:47:46 +00:00
public function songs(): HasMany
2016-06-04 11:57:27 +00:00
{
2022-06-10 10:47:46 +00:00
return $this->hasMany(Song::class);
2016-06-04 11:57:27 +00:00
}
2022-07-18 11:00:37 +00:00
protected function isUnknown(): Attribute
2015-12-21 13:49:00 +00:00
{
2022-07-18 11:00:37 +00:00
return Attribute::get(fn (): bool => $this->id === self::UNKNOWN_ID);
2015-12-21 13:49:00 +00:00
}
2022-07-18 11:00:37 +00:00
protected function isVarious(): Attribute
2016-04-17 15:38:06 +00:00
{
2022-07-18 11:00:37 +00:00
return Attribute::get(fn (): bool => $this->id === self::VARIOUS_ID);
2016-04-17 15:38:06 +00:00
}
2015-12-16 05:03:48 +00:00
/**
* Sometimes the tags extracted from getID3 are HTML entity encoded.
* This makes sure they are always sane.
*/
2022-07-18 11:00:37 +00:00
protected function name(): Attribute
2015-12-13 04:42:28 +00:00
{
2022-07-18 11:00:37 +00:00
return Attribute::get(static fn (string $value): string => html_entity_decode($value) ?: self::UNKNOWN_NAME);
2015-12-13 04:42:28 +00:00
}
2015-12-22 09:53:03 +00:00
/**
* Turn the image name into its absolute URL.
*/
2022-07-18 11:00:37 +00:00
protected function image(): Attribute
2015-12-22 09:53:03 +00:00
{
2022-07-18 11:00:37 +00:00
return Attribute::get(static fn (?string $value): ?string => artist_image_url($value));
2018-02-04 15:53:40 +00:00
}
2022-07-18 11:00:37 +00:00
protected function imagePath(): Attribute
{
2022-07-18 11:00:37 +00:00
return Attribute::get(fn (): ?string => artist_image_path(Arr::get($this->attributes, 'image')));
}
2022-07-18 11:00:37 +00:00
protected function hasImage(): Attribute
2018-02-04 15:53:40 +00:00
{
2022-07-18 11:00:37 +00:00
return Attribute::get(function (): bool {
$image = Arr::get($this->attributes, 'image');
2018-02-04 15:53:40 +00:00
2022-07-18 11:00:37 +00:00
return $image && file_exists(artist_image_path($image));
});
2015-12-22 09:53:03 +00:00
}
2020-12-23 10:53:00 +00:00
2022-06-10 10:47:46 +00:00
public function scopeIsStandard(Builder $query): Builder
{
return $query->whereNotIn('artists.id', [self::UNKNOWN_ID, self::VARIOUS_ID]);
}
public static function withMeta(User $scopedUser): BuilderContract
{
return static::query()
->leftJoin('songs', 'artists.id', '=', 'songs.artist_id')
->leftJoin('interactions', static function (JoinClause $join) use ($scopedUser): void {
$join->on('interactions.song_id', '=', 'songs.id')
->where('interactions.user_id', $scopedUser->id);
})
->groupBy('artists.id')
->select(['artists.*', DB::raw('CAST(SUM(interactions.play_count) AS INTEGER) AS play_count')])
->withCount('albums AS album_count', 'songs AS song_count')
->withSum('songs AS length', 'length');
}
2020-12-23 10:53:00 +00:00
/** @return array<mixed> */
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
2015-12-13 04:42:28 +00:00
}