Test Setup Failed
Push — master ( 57e282...3fccfa )
by Phan
03:47
created

Artist::getIsVariousAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use App\Facades\Lastfm;
6
use App\Facades\Util;
7
use App\Traits\SupportsDeleteWhereIDsNotIn;
8
use Exception;
9
use Illuminate\Database\Eloquent\Model;
10
use Log;
11
12
/**
13
 * @property int    id      The model ID
14
 * @property string name    The artist name
15
 * @property string image
16
 * @property bool   is_unknown
17
 * @property bool   is_various
18
 * @property \Illuminate\Database\Eloquent\Collection songs
19
 */
20
class Artist extends Model
21
{
22
    use SupportsDeleteWhereIDsNotIn;
23
24
    const UNKNOWN_ID = 1;
25
    const UNKNOWN_NAME = 'Unknown Artist';
26
    const VARIOUS_ID = 2;
27
    const VARIOUS_NAME = 'Various Artists';
28
29
    protected $guarded = ['id'];
30
31
    protected $hidden = ['created_at', 'updated_at'];
32
33
    public function albums()
34
    {
35
        return $this->hasMany(Album::class);
36
    }
37
38
    public function songs()
39
    {
40
        return $this->hasManyThrough(Song::class, Album::class);
41
    }
42
43
    public function getIsUnknownAttribute()
44
    {
45
        return $this->id === self::UNKNOWN_ID;
46
    }
47
48
    public function getIsVariousAttribute()
49
    {
50
        return $this->id === self::VARIOUS_ID;
51
    }
52
53
    /**
54
     * Get the "Various Artists" object.
55
     *
56
     * @return Artist
57
     */
58
    public static function getVariousArtist()
59
    {
60
        return self::find(self::VARIOUS_ID);
61
    }
62
63
    /**
64
     * Sometimes the tags extracted from getID3 are HTML entity encoded.
65
     * This makes sure they are always sane.
66
     *
67
     * @param $value
68
     *
69
     * @return string
70
     */
71
    public function getNameAttribute($value)
72
    {
73
        return html_entity_decode($value ?: self::UNKNOWN_NAME);
74
    }
75
76
    /**
77
     * Get an Artist object from their name.
78
     * If such is not found, a new artist will be created.
79
     *
80
     * @param string $name
81
     *
82
     * @return Artist
83
     */
84
    public static function get($name)
85
    {
86
        // Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
87
        if ($encoding = Util::detectUTFEncoding($name)) {
88
            $name = mb_convert_encoding($name, 'UTF-8', $encoding);
89
        }
90
91
        $name = trim($name) ?: self::UNKNOWN_NAME;
92
93
        return self::firstOrCreate(compact('name'), compact('name'));
94
    }
95
96
    /**
97
     * Get extra information about the artist from Last.fm.
98
     *
99
     * @return array|false
100
     */
101
    public function getInfo()
102
    {
103
        if ($this->is_unknown) {
104
            return false;
105
        }
106
107
        $info = Lastfm::getArtistInfo($this->name);
108
        $image = array_get($info, 'image');
109
110
        // If our current artist has no image, and Last.fm has one, copy the image for our local use.
111
        if (!$this->image && is_string($image) && ini_get('allow_url_fopen')) {
112
            try {
113
                $extension = explode('.', $image);
114
                $fileName = uniqid().'.'.trim(strtolower(last($extension)), '. ');
115
                $coverPath = app()->publicPath().'/public/img/artists/'.$fileName;
116
117
                file_put_contents($coverPath, file_get_contents($image));
118
119
                $this->update(['image' => $fileName]);
120
                $info['image'] = $this->image;
121
            } catch (Exception $e) {
122
                Log::error($e);
123
            }
124
        }
125
126
        return $info;
127
    }
128
129
    /**
130
     * Get songs *contributed* (in compilation albums) by the artist.
131
     *
132
     * @return \Illuminate\Database\Eloquent\Collection
133
     */
134
    public function getContributedSongs()
135
    {
136
        return Song::whereContributingArtistId($this->id)->get();
0 ignored issues
show
Bug introduced by
The method whereContributingArtistId() does not exist on App\Models\Song. Did you maybe mean artist()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
137
    }
138
139
    /**
140
     * Turn the image name into its absolute URL.
141
     *
142
     * @param mixed $value
143
     *
144
     * @return string|null
145
     */
146
    public function getImageAttribute($value)
147
    {
148
        return  $value ? app()->staticUrl("public/img/artists/$value") : null;
149
    }
150
}
151