-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
181 lines (160 loc) · 5.13 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace App\Models;
use App\Enums\PaymentMethodStatus;
use App\Enums\SubscriptionStatus;
use App\Enums\UserStatus;
use App\Notifications\UserSignedUp;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Sanctum\HasApiTokens;
/**
* User represents system customer.
*
* @property int $id
* @property string $name
* @property string $email
* @property string $password
* @property int $status
* @property string|null $remember_token
* @property \Illuminate\Support\Carbon|null $email_verified_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*
* @property \Illuminate\Database\Eloquent\Collection|\Laravel\Sanctum\PersonalAccessToken[] $tokens
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\PaymentMethod[] $paymentMethods
* @property \App\Models\PaymentMethod|null $activePaymentMethod
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\Subscription[] $subscriptions
* @property \App\Models\Subscription|null $activeSubscription
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\Favorite[] $favorites
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\Rent[] $rents
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\Rent[] $currentRents
*
* @method static \Illuminate\Database\Eloquent\Builder|static query()
*/
class User extends Authenticatable
{
use HasApiTokens;
use Notifiable;
use SoftDeletes;
use MustVerifyEmail;
/**
* {@inheritdoc}
*/
protected $fillable = [
'name',
'email',
'password',
'status',
];
/**
* {@inheritdoc}
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* {@inheritdoc}
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\App\Models\Favorite
*/
public function favorites()
{
return $this->hasMany(Favorite::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\App\Models\PaymentMethod
*/
public function paymentMethods(): HasMany
{
return $this->hasMany(PaymentMethod::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne|\App\Models\PaymentMethod
*/
public function activePaymentMethod(): HasOne
{
return $this->hasOne(PaymentMethod::class)->where('status', PaymentMethodStatus::ACTIVE);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\App\Models\Subscription
*/
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne|\App\Models\Subscription
*/
public function activeSubscription(): HasOne
{
return $this->hasOne(Subscription::class)->where('status', SubscriptionStatus::ACTIVE);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\App\Models\Rent
*/
public function rents(): HasMany
{
return $this->hasMany(Rent::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\App\Models\Rent
*/
public function currentRents(): HasMany
{
return $this->rents()->current();
}
/**
* Registers new user account.
* New account remains 'pending' waiting for identity confirmation, keeping login forbidden.
* Sends email verification notification.
* @see confirmIdentity()
*
* @param array $attributes
* @return static self reference.
*/
public function signup(array $attributes): self
{
if ($this->exists) {
throw new \LogicException('Unable to signup already existing account.');
}
if (isset($attributes['password'])) {
$password = $attributes['password'];
} else {
$password = Str::random(8);
$attributes['password'] = $password;
}
$this->fill($attributes);
$this->password = bcrypt($password);
$this->status = UserStatus::PENDING;
$this->save();
$this->notify(new UserSignedUp($password));
return $this;
}
/**
* Marks merchant as the one with confirmed identity (e.g. being a live person), allowing log in the system.
* This method sends merchant's projects to moderation.
* @see signup()
*
* @return static self reference.
*/
public function confirmIdentity(): self
{
if ($this->status !== UserStatus::PENDING) {
throw new \LogicException('Identity confirmation can be performed only for pending accounts.');
}
$this->status = UserStatus::ACTIVE;
$this->save();
return $this;
}
}