User::sendPasswordResetNotification()   A
last analyzed

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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App\Services\Auth\Front;
4
5
use Mail;
6
use App\Services\Auth\User as BaseUser;
7
use App\Services\Auth\Front\Enums\UserRole;
8
use App\Services\Auth\Front\Enums\UserStatus;
9
use App\Services\Auth\Front\Mail\ResetPassword;
10
use App\Services\Auth\Front\Events\UserRegistered;
11
use App\Services\Auth\Front\Exceptions\UserIsAlreadyActivated;
12
13
/**
14
 * @property string $address
15
 * @property string $postal
16
 * @property string $city
17
 * @property string $country
18
 * @property string $telephone
19
 * @property \App\Services\Auth\Front\Enums\UserRole $role
20
 * @property \App\Services\Auth\Front\Enums\UserStatus $status
21
 */
22
class User extends BaseUser
23
{
24
    protected $table = 'users_front';
25
26
    public static function register(array $input): User
27
    {
28
        $defaults = [
29
            'role' => UserRole::MEMBER,
30
            'status' => UserStatus::ACTIVE,
31
        ];
32
33
        $user = static::create($defaults + array_only($input, [
34
            'first_name',
35
            'last_name',
36
            'address',
37
            'postal',
38
            'city',
39
            'country',
40
            'telephone',
41
            'email',
42
            'password',
43
        ]));
44
45
        event(new UserRegistered($user));
46
47
        return $user;
48
    }
49
50
    public function guardDriver(): string
51
    {
52
        return 'front';
53
    }
54
55
    public function getHomeUrl(): string
56
    {
57
        return url('/');
58
    }
59
60
    public function getProfileUrl(): string
61
    {
62
        return url('/');
63
    }
64
65
    public function getStatusAttribute(): string
66
    {
67
        return $this->attributes['status'];
68
    }
69
70
    public function setStatusAttribute(string $status)
71
    {
72
        $this->attributes['status'] = $status;
73
    }
74
75
    public function hasStatus(string $status): bool
76
    {
77
        return $this->status === $status;
78
    }
79
80
    public function isActive(): bool
81
    {
82
        return $this->hasStatus(UserStatus::ACTIVE);
83
    }
84
85
    public function activate(): User
86
    {
87
        if ($this->status !== UserStatus::WAITING_FOR_APPROVAL) {
88
            throw new UserIsAlreadyActivated();
89
        }
90
91
        $this->status = UserStatus::ACTIVE;
92
93
        return $this;
94
    }
95
96
    public function getRoleAttribute(): string
97
    {
98
        return $this->attributes['role'];
99
    }
100
101
    public function setRoleAttribute(string $role)
102
    {
103
        $this->attributes['role'] = $role;
104
    }
105
106
    public function hasRole($role): bool
107
    {
108
        return $this->role === $role;
109
    }
110
111
    /**
112
     * Send the password reset notification.
113
     *
114
     * @param string $token
115
     */
116
    public function sendPasswordResetNotification($token)
117
    {
118
        Mail::to($this->email)->send(new ResetPassword($this, $token));
119
    }
120
}
121