User   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 121
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 13 2
A getAuthIdentifierName() 0 4 1
A getAuthIdentifier() 0 4 1
A getAuthPassword() 0 4 1
A getRememberToken() 0 6 2
A setRememberToken() 0 6 2
A getRememberTokenName() 0 4 1
A getAvatarAttribute() 0 6 2
A setPasswordAttribute() 0 4 1
1
<?php
2
3
namespace Oscer\Cms\Core\Models;
4
5
use Illuminate\Contracts\Auth\Access\Authorizable;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use Illuminate\Foundation\Auth\Access\Authorizable as AuthorizableTrait;
8
use Illuminate\Support\Carbon;
9
use Illuminate\Support\Facades\Mail;
10
use Illuminate\Support\Facades\Storage;
11
use Illuminate\Support\Str;
12
use Laravel\Sanctum\HasApiTokens;
13
use Oscer\Cms\Core\Mails\NewUserCreatedMail;
14
use Spatie\Permission\Traits\HasRoles;
15
16
/**
17
 * @property int id
18
 * @property string name
19
 * @property string email
20
 * @property string bio
21
 * @property string avatar
22
 * @property Carbon updated_at
23
 * @property Carbon created_at
24
 */
25
class User extends BaseModel implements Authenticatable, Authorizable
26
{
27
    use HasApiTokens;
28
    use HasRoles;
29
    use AuthorizableTrait;
30
31
    /**
32
     * The attributes excluded from the model's JSON form.
33
     *
34
     * @var array
35
     */
36
    protected $hidden = ['password', 'remember_token'];
37
38
    protected $attributes = [
39
        'language' => 'en',
40
    ];
41
42
    /**
43
     * The column name of the "remember me" token.
44
     *
45
     * @var string
46
     */
47
    protected $rememberTokenName = 'remember_token';
48
49
    protected static function boot()
50
    {
51
        parent::boot();
52
53
        static::creating(function (self $user) {
54
            if (! $user->password) {
55
                $password = Str::random();
56
                $user->password = $password;
57
58
                Mail::to($user->email)->send(new NewUserCreatedMail($password));
59
            }
60
        });
61
    }
62
63
    /**
64
     * Get the name of the unique identifier for the user.
65
     *
66
     * @return string
67
     */
68
    public function getAuthIdentifierName()
69
    {
70
        return $this->getKeyName();
71
    }
72
73
    /**
74
     * Get the unique identifier for the user.
75
     *
76
     * @return mixed
77
     */
78
    public function getAuthIdentifier()
79
    {
80
        return $this->{$this->getAuthIdentifierName()};
81
    }
82
83
    /**
84
     * Get the password for the user.
85
     *
86
     * @return string
87
     */
88
    public function getAuthPassword()
89
    {
90
        return $this->password;
91
    }
92
93
    /**
94
     * Get the token value for the "remember me" session.
95
     *
96
     * @return string|null
97
     */
98
    public function getRememberToken()
99
    {
100
        if (! empty($this->getRememberTokenName())) {
101
            return (string) $this->{$this->getRememberTokenName()};
102
        }
103
    }
104
105
    /**
106
     * Set the token value for the "remember me" session.
107
     *
108
     * @param string $value
109
     * @return void
110
     */
111
    public function setRememberToken($value)
112
    {
113
        if (! empty($this->getRememberTokenName())) {
114
            $this->{$this->getRememberTokenName()} = $value;
115
        }
116
    }
117
118
    /**
119
     * Get the column name for the "remember me" token.
120
     *
121
     * @return string
122
     */
123
    public function getRememberTokenName()
124
    {
125
        return $this->rememberTokenName;
126
    }
127
128
    /**
129
     * Get the author's avatar.
130
     *
131
     * @param string $value
132
     * @return string
133
     */
134
    public function getAvatarAttribute($value)
135
    {
136
        return $value
137
            ? Storage::url($value)
138
            : 'https://secure.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=80';
139
    }
140
141
    public function setPasswordAttribute($value)
142
    {
143
        $this->attributes['password'] = bcrypt($value);
144
    }
145
}
146