1 | <?php |
||
2 | |||
3 | namespace App\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||
6 | use Illuminate\Foundation\Auth\User as Authenticatable; |
||
7 | use Illuminate\Notifications\Notifiable; |
||
8 | |||
9 | /** |
||
10 | * @property array $preferences |
||
11 | * @property int $id |
||
12 | * @property bool $is_admin |
||
13 | * @property string $lastfm_session_key |
||
14 | * |
||
15 | * @method static self create(array $params) |
||
16 | * @method static int count() |
||
17 | */ |
||
18 | class User extends Authenticatable |
||
19 | { |
||
20 | use Notifiable; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
21 | |||
22 | /** |
||
23 | * The preferences that we don't want to show to the client. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private const HIDDEN_PREFERENCES = ['lastfm_session_key']; |
||
28 | |||
29 | protected $guarded = ['id']; |
||
30 | protected $casts = [ |
||
31 | 'id' => 'int', |
||
32 | 'is_admin' => 'bool', |
||
33 | ]; |
||
34 | protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at']; |
||
35 | |||
36 | 1 | public function playlists(): HasMany |
|
37 | { |
||
38 | 1 | return $this->hasMany(Playlist::class); |
|
39 | } |
||
40 | |||
41 | public function interactions(): HasMany |
||
42 | { |
||
43 | return $this->hasMany(Interaction::class); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return mixed|null |
||
48 | */ |
||
49 | 8 | public function getPreference(string $key) |
|
50 | { |
||
51 | // We can't use $this->preferences directly, since the data has been tampered |
||
52 | // by getPreferencesAttribute(). |
||
53 | 8 | return array_get((array) unserialize($this->attributes['preferences']), $key); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param mixed $val |
||
58 | */ |
||
59 | 5 | public function savePreference(string $key, $val): void |
|
60 | { |
||
61 | 5 | $preferences = $this->preferences; |
|
62 | 5 | $preferences[$key] = $val; |
|
63 | 5 | $this->preferences = $preferences; |
|
64 | |||
65 | 5 | $this->save(); |
|
66 | 5 | } |
|
67 | |||
68 | /** |
||
69 | * An alias to savePreference(). |
||
70 | * |
||
71 | * @param mixed $val |
||
72 | * |
||
73 | * @see self::savePreference |
||
74 | */ |
||
75 | 3 | public function setPreference(string $key, $val): void |
|
76 | { |
||
77 | 3 | $this->savePreference($key, $val); |
|
78 | 3 | } |
|
79 | |||
80 | 3 | public function deletePreference(string $key): void |
|
81 | { |
||
82 | 3 | $preferences = $this->preferences; |
|
83 | 3 | array_forget($preferences, $key); |
|
84 | |||
85 | 3 | $this->update(compact('preferences')); |
|
86 | 3 | } |
|
87 | |||
88 | /** |
||
89 | * Determine if the user is connected to Last.fm. |
||
90 | */ |
||
91 | 1 | public function connectedToLastfm(): bool |
|
92 | { |
||
93 | 1 | return (bool) $this->lastfm_session_key; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get the user's Last.fm session key. |
||
98 | * |
||
99 | * @return string|null The key if found, or null if user isn't connected to Last.fm |
||
100 | */ |
||
101 | 6 | public function getLastfmSessionKeyAttribute(): ?string |
|
102 | { |
||
103 | 6 | return $this->getPreference('lastfm_session_key'); |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * User preferences are stored as a serialized associative array. |
||
108 | * |
||
109 | * @param mixed[] $value |
||
110 | */ |
||
111 | 45 | public function setPreferencesAttribute(array $value): void |
|
112 | { |
||
113 | 45 | $this->attributes['preferences'] = serialize($value); |
|
114 | 45 | } |
|
115 | |||
116 | /** |
||
117 | * Unserialize the user preferences back to an array before returning. |
||
118 | * |
||
119 | * @return mixed[] |
||
120 | */ |
||
121 | 9 | public function getPreferencesAttribute(?string $value): array |
|
122 | { |
||
123 | 9 | $preferences = unserialize($value) ?: []; |
|
124 | |||
125 | // Hide sensitive data from returned preferences. |
||
126 | 9 | foreach (self::HIDDEN_PREFERENCES as $key) { |
|
127 | 9 | if (array_key_exists($key, $preferences)) { |
|
128 | 2 | $preferences[$key] = 'hidden'; |
|
129 | } |
||
130 | } |
||
131 | |||
132 | 9 | return $preferences; |
|
133 | } |
||
134 | } |
||
135 |