Issues (49)

app/Models/User.php (2 issues)

Labels
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Foundation\Auth\User as Authenticatable;
8
use Illuminate\Notifications\Notifiable;
9
10
/**
11
 * @property array  $preferences
12
 * @property int    $id
13
 * @property bool   $is_admin
14
 * @property string $lastfm_session_key
15
 * @property string $email
16
 * @property string $password
17
 *
18
 * @method static self create(array $params)
19
 * @method static int count()
20
 * @method static Collection where(string $key, $val)
21
 */
22
class User extends Authenticatable
23
{
24
    use Notifiable;
0 ignored issues
show
The trait Illuminate\Notifications\Notifiable requires the property $phone_number which is not provided by App\Models\User.
Loading history...
25
26
    /**
27
     * The preferences that we don't want to show to the client.
28
     *
29
     * @var array
30
     */
31
    private const HIDDEN_PREFERENCES = ['lastfm_session_key'];
32
33
    protected $guarded = ['id'];
34
    protected $casts = [
35
        'id' => 'int',
36
        'is_admin' => 'bool',
37
    ];
38
    protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at'];
39
40 1
    public function playlists(): HasMany
41
    {
42 1
        return $this->hasMany(Playlist::class);
43
    }
44
45
    public function interactions(): HasMany
46
    {
47
        return $this->hasMany(Interaction::class);
48
    }
49
50
    /**
51
     * @return mixed|null
52
     */
53 8
    public function getPreference(string $key)
54
    {
55
        // We can't use $this->preferences directly, since the data has been tampered
56
        // by getPreferencesAttribute().
57 8
        return array_get((array) unserialize($this->attributes['preferences']), $key);
58
    }
59
60
    /**
61
     * @param mixed $val
62
     */
63 5
    public function savePreference(string $key, $val): void
64
    {
65 5
        $preferences = $this->preferences;
66 5
        $preferences[$key] = $val;
67 5
        $this->preferences = $preferences;
68
69 5
        $this->save();
70 5
    }
71
72
    /**
73
     * An alias to savePreference().
74
     *
75
     * @param mixed $val
76
     *
77
     * @see self::savePreference
78
     */
79 3
    public function setPreference(string $key, $val): void
80
    {
81 3
        $this->savePreference($key, $val);
82 3
    }
83
84 3
    public function deletePreference(string $key): void
85
    {
86 3
        $preferences = $this->preferences;
87 3
        array_forget($preferences, $key);
0 ignored issues
show
$preferences of type string is incompatible with the type array expected by parameter $array of array_forget(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        array_forget(/** @scrutinizer ignore-type */ $preferences, $key);
Loading history...
88
89 3
        $this->update(compact('preferences'));
90 3
    }
91
92
    /**
93
     * Determine if the user is connected to Last.fm.
94
     */
95 1
    public function connectedToLastfm(): bool
96
    {
97 1
        return (bool) $this->lastfm_session_key;
98
    }
99
100
    /**
101
     * Get the user's Last.fm session key.
102
     *
103
     * @return string|null The key if found, or null if user isn't connected to Last.fm
104
     */
105 6
    public function getLastfmSessionKeyAttribute(): ?string
106
    {
107 6
        return $this->getPreference('lastfm_session_key');
108
    }
109
110
    /**
111
     * User preferences are stored as a serialized associative array.
112
     *
113
     * @param mixed[] $value
114
     */
115 45
    public function setPreferencesAttribute(array $value): void
116
    {
117 45
        $this->attributes['preferences'] = serialize($value);
118 45
    }
119
120
    /**
121
     * Unserialize the user preferences back to an array before returning.
122
     *
123
     * @return mixed[]
124
     */
125 9
    public function getPreferencesAttribute(?string $value): array
126
    {
127 9
        $preferences = unserialize($value) ?: [];
128
129
        // Hide sensitive data from returned preferences.
130 9
        foreach (self::HIDDEN_PREFERENCES as $key) {
131 9
            if (array_key_exists($key, $preferences)) {
132 2
                $preferences[$key] = 'hidden';
133
            }
134
        }
135
136 9
        return $preferences;
137
    }
138
}
139