Test Setup Failed
Push — master ( 0a8e7c...aa7267 )
by Phan
03:42
created

Artist::getVariousArtist()   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
 */
19
class Artist extends Model
20
{
21
    use SupportsDeleteWhereIDsNotIn;
22
23
    const UNKNOWN_ID = 1;
24
    const UNKNOWN_NAME = 'Unknown Artist';
25
    const VARIOUS_ID = 2;
26
    const VARIOUS_NAME = 'Various Artists';
27
28
    protected $guarded = ['id'];
29
30
    protected $hidden = ['created_at', 'updated_at'];
31
32
    public function albums()
33
    {
34
        return $this->hasMany(Album::class);
35
    }
36
37
    public function songs()
38
    {
39
        return $this->hasManyThrough(Song::class, Album::class);
40
    }
41
42
    public function getIsUnknownAttribute()
43
    {
44
        return $this->id === self::UNKNOWN_ID;
45
    }
46
47
    public function getVariousAttribute()
48
    {
49
        return $this->id === self::VARIOUS_ID;
50
    }
51
52
    /**
53
     * Get the "Various Artists" object.
54
     *
55
     * @return Artist
56
     */
57
    public static function getVariousArtist()
58
    {
59
        return self::find(self::VARIOUS_ID);
60
    }
61
62
    /**
63
     * Sometimes the tags extracted from getID3 are HTML entity encoded.
64
     * This makes sure they are always sane.
65
     *
66
     * @param $value
67
     *
68
     * @return string
69
     */
70
    public function getNameAttribute($value)
71
    {
72
        return html_entity_decode($value ?: self::UNKNOWN_NAME);
73
    }
74
75
    /**
76
     * Get an Artist object from their name.
77
     * If such is not found, a new artist will be created.
78
     *
79
     * @param string $name
80
     *
81
     * @return Artist
82
     */
83
    public static function get($name)
84
    {
85
        // Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
86
        if ($encoding = Util::detectUTFEncoding($name)) {
87
            $name = mb_convert_encoding($name, 'UTF-8', $encoding);
88
        }
89
90
        $name = trim($name) ?: self::UNKNOWN_NAME;
91
92
        return self::firstOrCreate(compact('name'), compact('name'));
93
    }
94
95
    /**
96
     * Get extra information about the artist from Last.fm.
97
     *
98
     * @return array|false
99
     */
100
    public function getInfo()
101
    {
102
        if ($this->is_unknown) {
103
            return false;
104
        }
105
106
        $info = Lastfm::getArtistInfo($this->name);
107
        $image = array_get($info, 'image');
108
109
        // If our current artist has no image, and Last.fm has one, copy the image for our local use.
110
        if (!$this->image && is_string($image) && ini_get('allow_url_fopen')) {
111
            try {
112
                $extension = explode('.', $image);
113
                $fileName = uniqid().'.'.trim(strtolower(last($extension)), '. ');
114
                $coverPath = app()->publicPath().'/public/img/artists/'.$fileName;
115
116
                file_put_contents($coverPath, file_get_contents($image));
117
118
                $this->update(['image' => $fileName]);
119
                $info['image'] = $this->image;
120
            } catch (Exception $e) {
121
                Log::error($e);
122
            }
123
        }
124
125
        return $info;
126
    }
127
128
    /**
129
     * Get songs *contributed* (in compilation albums) by the artist.
130
     *
131
     * @return \Illuminate\Database\Eloquent\Collection
132
     */
133
    public function getContributedSongs()
134
    {
135
        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...
136
    }
137
138
    /**
139
     * Turn the image name into its absolute URL.
140
     *
141
     * @param mixed $value
142
     *
143
     * @return string|null
144
     */
145
    public function getImageAttribute($value)
146
    {
147
        return  $value ? app()->staticUrl("public/img/artists/$value") : null;
148
    }
149
}
150