Completed
Push — master ( 0925b8...107387 )
by Sebastian
03:43
created

User::getNameAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services\Auth;
4
5
use App\Foundation\Models\Traits\Presentable;
6
use Carbon\Carbon;
7
use Illuminate\Auth\Authenticatable;
8
use Illuminate\Auth\Passwords\CanResetPassword;
9
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
10
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
11
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Foundation\Auth\Access\Authorizable;
14
15
/**
16
 * @property int $id
17
 * @property string $email
18
 * @property string $password
19
 * @property string $first_name
20
 * @property string $last_name
21
 * @property string $name
22
 * @property string $remember_token
23
 * @property string $locale
24
 * @property \Carbon\Carbon $last_activity
25
 * @property \Carbon\Carbon $created_at
26
 * @property \Carbon\Carbon $updated_at
27
 */
28
abstract class User extends Model implements AuthenticatableContract, CanResetPasswordContract, AuthorizableContract
29
{
30
    use Authenticatable, CanResetPassword, Presentable, Authorizable;
31
32
    protected $guarded = ['id'];
33
    protected $hidden = ['password', 'remember_token'];
34
    protected $dates = ['last_activity'];
35
36
    abstract public function guardDriver(): string;
37
    abstract public function getHomeUrl(): string;
38
    abstract public function getProfileUrl(): string;
39
40
    public function setPasswordAttribute(string $value)
41
    {
42
        if (is_null($value)) {
43
            $this->attributes['password'] = null;
44
            return;
45
        }
46
47
        $this->attributes['password'] = bcrypt($value);
48
    }
49
50
    public function getNameAttribute(): string
51
    {
52
        return $this->first_name . ' ' . $this->last_name;
53
    }
54
55
    public function hasNeverLoggedIn(): bool
56
    {
57
        return empty($this->password);
58
    }
59
60
    public function registerLastActivity(): User
61
    {
62
        $this->last_activity = Carbon::now();
63
64
        return $this;
65
    }
66
67
    public function isCurrentUser(): bool
68
    {
69
        if (!$this->id) {
70
            return false;
71
        }
72
73
        if ($this->guardDriver() !== config('auth.defaults.guard')) {
74
            return false;
75
        }
76
77
        return $this->id === auth()->id();
0 ignored issues
show
Bug introduced by
The method id() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
    }
79
80
    /**
81
     * @param string $token
82
     *
83
     * @return \App\Services\Auth\User|null
84
     */
85
    public static function findByToken(string $token)
86
    {
87
        $resetRecord = app('db')->table('password_resets')->where('token', $token)->first();
88
89
        if (empty($resetRecord)) {
90
            return;
91
        }
92
93
        return static::where('email', $resetRecord->email)->first();
94
    }
95
96
    /**
97
     * @param string $email
98
     *
99
     * @return \App\Services\Auth\User|null
100
     */
101
    public static function findByEmail(string $email)
102
    {
103
        return static::where('email', $email)->first();
104
    }
105
}
106