Test Setup Failed
Branch master (096eb7)
by Phan
03:51
created

User::deletePreference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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