Completed
Push — master ( 02f92d...9c9507 )
by Sebastian
15:38
created

User   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 6
Metric Value
wmc 9
lcom 4
cbo 6
dl 0
loc 68
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
guardDriver() 0 1 ?
getHomeUrl() 0 1 ?
getProfileUrl() 0 1 ?
A setPasswordAttribute() 0 4 1
A hasNeverLoggedIn() 0 4 1
A registerLastActivity() 0 6 1
A isCurrentUser() 0 12 3
A findByToken() 0 10 2
A findByEmail() 0 4 1
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 int $first_name
20
 * @property int $last_name
21
 * @property string $remember_token
22
 * @property string $locale
23
 * @property \Carbon\Carbon $last_activity
24
 * @property \Carbon\Carbon $created_at
25
 * @property \Carbon\Carbon $updated_at
26
 */
27
abstract class User extends Model implements AuthenticatableContract, CanResetPasswordContract, AuthorizableContract
28
{
29
    use Authenticatable, CanResetPassword, Presentable, Authorizable;
30
31
    protected $guarded = ['id'];
32
    protected $hidden = ['password', 'remember_token'];
33
    protected $dates = ['last_activity'];
34
35
    abstract public function guardDriver() : string;
36
    abstract public function getHomeUrl() : string;
37
    abstract public function getProfileUrl() : string;
38
39
    public function setPasswordAttribute(string $value)
40
    {
41
        $this->attributes['password'] = bcrypt($value);
42
    }
43
44
    public function hasNeverLoggedIn() : bool
45
    {
46
        return empty($this->password);
47
    }
48
49
    public function registerLastActivity() : User
50
    {
51
        $this->last_activity = Carbon::now();
52
53
        return $this;
54
    }
55
56
    public function isCurrentUser() : bool
57
    {
58
        if (! $this->id) {
59
            return false;
60
        }
61
62
        if ($this->guardDriver() !== config('auth.defaults.guard')) {
63
            return false;
64
        }
65
66
        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...
67
    }
68
69
    /**
70
     * @param string $token
71
     *
72
     * @return \App\Services\Auth\User|null
73
     */
74
    public static function findByToken(string $token)
75
    {
76
        $resetRecord = app('db')->table('password_resets')->where('token', $token)->first();
77
78
        if (empty($resetRecord)) {
79
            return null;
80
        }
81
82
        return static::where('email', $resetRecord->email)->first();
83
    }
84
85
    /**
86
     * @param string $email
87
     *
88
     * @return \App\Services\Auth\User|null
89
     */
90
    public static function findByEmail(string $email)
91
    {
92
        return static::where('email', $email)->first();
93
    }
94
}
95