Test Setup Failed
Pull Request — master (#630)
by
unknown
34:04
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
 */
17
class Artist extends Model
18
{
19
    use SupportsDeleteWhereIDsNotIn;
20
21
    const UNKNOWN_ID = 1;
22
    const UNKNOWN_NAME = '未知歌手';
23
    const VARIOUS_ID = 2;
24
    const VARIOUS_NAME = '多种歌手';
25
26
    protected $guarded = ['id'];
27
28
    protected $hidden = ['created_at', 'updated_at'];
29
30
    public function albums()
31
    {
32
        return $this->hasMany(Album::class);
33
    }
34
35
    public function songs()
36
    {
37
        return $this->hasManyThrough(Song::class, Album::class);
38
    }
39
40
    public function isUnknown()
41
    {
42
        return $this->id === self::UNKNOWN_ID;
43
    }
44
45
    public function isVarious()
46
    {
47
        return $this->id === self::VARIOUS_ID;
48
    }
49
50
    /**
51
     * Get the "Various Artists" object.
52
     *
53
     * @return Artist
54
     */
55
    public static function getVarious()
56
    {
57
        return self::find(self::VARIOUS_ID);
58
    }
59
60
    /**
61
     * Sometimes the tags extracted from getID3 are HTML entity encoded.
62
     * This makes sure they are always sane.
63
     *
64
     * @param $value
65
     *
66
     * @return string
67
     */
68
    public function getNameAttribute($value)
69
    {
70
        return html_entity_decode($value ?: self::UNKNOWN_NAME);
71
    }
72
73
    /**
74
     * Get an Artist object from their name.
75
     * If such is not found, a new artist will be created.
76
     *
77
     * @param string $name
78
     *
79
     * @return Artist
80
     */
81
    public static function get($name)
82
    {
83
        // Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
84
        if ($encoding = Util::detectUTFEncoding($name)) {
85
            $name = mb_convert_encoding($name, 'UTF-8', $encoding);
86
        }
87
88
        $name = trim($name) ?: self::UNKNOWN_NAME;
89
90
        return self::firstOrCreate(compact('name'), compact('name'));
91
    }
92
93
    /**
94
     * Get extra information about the artist from Last.fm.
95
     *
96
     * @return array|false
97
     */
98
    public function getInfo()
99
    {
100
        if ($this->isUnknown()) {
101
            return false;
102
        }
103
104
        $info = Lastfm::getArtistInfo($this->name);
105
106
        // If our current artist has no image, and Last.fm has one, copy the image for our local use.
107
        if (!$this->image &&
108
            is_string($image = array_get($info, 'image')) &&
109
            ini_get('allow_url_fopen')
110
        ) {
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