User::getLastLoginAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Torralbodavid\DuckFunkCore\Models\Arcturus;
4
5
use Carbon\Carbon;
6
use Illuminate\Auth\Passwords\CanResetPassword as CanResetPasswordTrait;
7
use Illuminate\Contracts\Auth\Authenticatable;
8
use Illuminate\Contracts\Auth\CanResetPassword;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Notifications\Notifiable;
11
use Spatie\Permission\Traits\HasRoles;
12
use Torralbodavid\DuckFunkCore\Events\UserEvent;
13
14
class User extends Model implements Authenticatable, CanResetPassword
15
{
16
    use HasRoles;
17
    use Notifiable;
18
    use CanResetPasswordTrait;
19
20
    protected $table = 'users';
21
    protected $guarded = [];
22
23
    /**
24
     * The event map for the model.
25
     *
26
     * @var array
27
     */
28
    protected $dispatchesEvents = [
29
        'saved' => UserEvent::class,
30
    ];
31
32
    /**
33
     * The attributes that are mass assignable.
34
     *
35
     * @var array
36
     */
37
    protected $fillable = [
38
        'username', 'mail', 'password', 'rank', 'account_created', 'ip_register', 'ip_current', 'last_login',
39
    ];
40
41
    /**
42
     * The attributes that should be hidden for arrays.
43
     *
44
     * @var array
45
     */
46
    protected $hidden = [
47
        'password', 'remember_token',
48
    ];
49
50
    /**
51
     * The attributes that should be cast to native types.
52
     *
53
     * @var array
54
     */
55
    protected $casts = [
56
        'email_verified_at' => 'datetime',
57
    ];
58
59
    public function getEmailAttribute()
60
    {
61
        return $this->mail;
62
    }
63
64
    public function getEmailForPasswordReset()
65
    {
66
        return $this->mail;
67
    }
68
69
    public function getEmailForVerification()
70
    {
71
        return $this->mail;
72
    }
73
74
    /**
75
     * Get the permissions for an avatar.
76
     */
77
    public function permissions()
78
    {
79
        return $this->hasOne(Permissions::class, 'id', 'rank');
80
    }
81
82
    /*
83
     * Get avatar settings
84
     */
85
    public function settings()
86
    {
87
        return $this->hasOne(UserSettings::class, 'user_id', 'id');
88
    }
89
90
    /*
91
     * Get the bans for an avatar
92
     */
93
    public function bans()
94
    {
95
        return $this->hasMany(Bans::class, 'id', 'user_id');
96
    }
97
98
    /**
99
     * Get friend requests.
100
     */
101
    public function friend_requests()
102
    {
103
        return $this->belongsToMany($this, 'messenger_friendrequests', 'user_to_id', 'user_from_id');
104
    }
105
106
    /**
107
     * Get friends.
108
     */
109
    public function friends()
110
    {
111
        return $this->belongsToMany($this, 'messenger_friendships', 'user_one_id', 'user_two_id');
112
    }
113
114
    public function getLastLoginAttribute()
115
    {
116
        return Carbon::createFromTimestamp($this->attributes['last_login'])->diffForHumans();
117
    }
118
119
    /**
120
     * Get the name of the unique identifier for the user.
121
     *
122
     * @return string
123
     */
124
    public function getAuthIdentifierName()
125
    {
126
        return 'mail';
127
    }
128
129
    /**
130
     * Get the unique identifier for the user.
131
     *
132
     * @return mixed
133
     */
134
    public function getAuthIdentifier()
135
    {
136
        return $this->{$this->getAuthIdentifierName()};
137
    }
138
139
    /**
140
     * Get the password for the user.
141
     *
142
     * @return string
143
     */
144
    public function getAuthPassword()
145
    {
146
        return $this->password;
147
    }
148
149
    /**
150
     * Get the token value for the "remember me" session.
151
     *
152
     * @return string
153
     */
154
    public function getRememberToken()
155
    {
156
        return auth()->user()->getRememberToken();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
157
    }
158
159
    /**
160
     * Set the token value for the "remember me" session.
161
     *
162
     * @param string $value
163
     * @return void
164
     */
165
    public function setRememberToken($value)
166
    {
167
        return auth()->user()->setRememberToken();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
168
    }
169
170
    /**
171
     * Get the column name for the "remember me" token.
172
     *
173
     * @return string
174
     */
175
    public function getRememberTokenName()
176
    {
177
        return auth()->user()->getRememberTokenName();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
178
    }
179
180
    /*
181
     * Custom methods
182
     */
183
    public static function randomNickname()
184
    {
185
        $username = preg_replace('/[^A-Za-z0-9 ]/', '',
186
            faker('firstName').faker('lastName').faker()->numberBetween(1, 99)
187
        );
188
189
        return (self::where('username', $username)->exists()) ? self::randomNickname() : $username;
190
    }
191
}
192