phanan /
koel
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Models; |
||||
| 4 | |||||
| 5 | use App\Facades\Util; |
||||
| 6 | use App\Traits\SupportsDeleteWhereIDsNotIn; |
||||
| 7 | use Illuminate\Database\Eloquent\Collection; |
||||
| 8 | use Illuminate\Database\Eloquent\Model; |
||||
| 9 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||||
| 10 | use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
||||
| 11 | |||||
| 12 | /** |
||||
| 13 | * @property int $id |
||||
| 14 | * @property string $name |
||||
| 15 | * @property string $image |
||||
| 16 | * @property bool $is_unknown |
||||
| 17 | * @property bool $is_various |
||||
| 18 | * @property Collection $songs |
||||
| 19 | * @property bool $has_image |
||||
| 20 | * |
||||
| 21 | * @method static self find(int $id) |
||||
| 22 | * @method static self firstOrCreate(array $where, array $params = []) |
||||
| 23 | */ |
||||
| 24 | class Artist extends Model |
||||
| 25 | { |
||||
| 26 | use SupportsDeleteWhereIDsNotIn; |
||||
| 27 | |||||
| 28 | const UNKNOWN_ID = 1; |
||||
| 29 | const UNKNOWN_NAME = 'Unknown Artist'; |
||||
| 30 | const VARIOUS_ID = 2; |
||||
| 31 | const VARIOUS_NAME = 'Various Artists'; |
||||
| 32 | |||||
| 33 | protected $guarded = ['id']; |
||||
| 34 | protected $hidden = ['created_at', 'updated_at']; |
||||
| 35 | |||||
| 36 | public function albums(): HasMany |
||||
| 37 | { |
||||
| 38 | return $this->hasMany(Album::class); |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * An artist can have many songs. |
||||
| 43 | * Unless he is Rick Astley. |
||||
| 44 | */ |
||||
| 45 | public function songs(): HasManyThrough |
||||
| 46 | { |
||||
| 47 | return $this->hasManyThrough(Song::class, Album::class); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | 5 | public function getIsUnknownAttribute(): bool |
|||
| 51 | { |
||||
| 52 | 5 | return $this->id === self::UNKNOWN_ID; |
|||
| 53 | } |
||||
| 54 | |||||
| 55 | 1 | public function getIsVariousAttribute(): bool |
|||
| 56 | { |
||||
| 57 | 1 | return $this->id === self::VARIOUS_ID; |
|||
| 58 | } |
||||
| 59 | |||||
| 60 | 8 | public static function getVariousArtist(): self |
|||
| 61 | { |
||||
| 62 | 8 | return static::find(self::VARIOUS_ID); |
|||
| 63 | } |
||||
| 64 | |||||
| 65 | /** |
||||
| 66 | * Sometimes the tags extracted from getID3 are HTML entity encoded. |
||||
| 67 | * This makes sure they are always sane. |
||||
| 68 | */ |
||||
| 69 | 15 | public function getNameAttribute(string $value): string |
|||
| 70 | { |
||||
| 71 | 15 | return html_entity_decode($value ?: self::UNKNOWN_NAME); |
|||
| 72 | } |
||||
| 73 | |||||
| 74 | /** |
||||
| 75 | * Get an Artist object from their name. |
||||
| 76 | * If such is not found, a new artist will be created. |
||||
| 77 | */ |
||||
| 78 | 17 | public static function get(string $name): self |
|||
| 79 | { |
||||
| 80 | // Remove the BOM from UTF-8/16/32, as it will mess up the database constraints. |
||||
| 81 | 17 | if ($encoding = Util::detectUTFEncoding($name)) { |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 82 | 1 | $name = mb_convert_encoding($name, 'UTF-8', $encoding); |
|||
| 83 | } |
||||
| 84 | |||||
| 85 | 17 | $name = trim($name) ?: self::UNKNOWN_NAME; |
|||
| 86 | |||||
| 87 | 17 | return static::firstOrCreate(compact('name')); |
|||
| 88 | } |
||||
| 89 | |||||
| 90 | /** |
||||
| 91 | * Turn the image name into its absolute URL. |
||||
| 92 | */ |
||||
| 93 | 7 | public function getImageAttribute(?string $value): ?string |
|||
| 94 | { |
||||
| 95 | 7 | return $value ? app()->staticUrl("public/img/artists/$value") : null; |
|||
|
0 ignored issues
–
show
The method
staticUrl() does not exist on Illuminate\Container\Container. It seems like you code against a sub-type of Illuminate\Container\Container such as App\Application.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 96 | } |
||||
| 97 | |||||
| 98 | 1 | public function getHasImageAttribute(): bool |
|||
| 99 | { |
||||
| 100 | 1 | $image = array_get($this->attributes, 'image'); |
|||
| 101 | |||||
| 102 | 1 | if (!$image) { |
|||
| 103 | 1 | return false; |
|||
| 104 | } |
||||
| 105 | |||||
| 106 | return file_exists(public_path("public/img/artists/$image")); |
||||
| 107 | } |
||||
| 108 | } |
||||
| 109 |