1 | <?php |
||||
2 | |||||
3 | namespace ProtoneMedia\LaravelVerifyNewEmail; |
||||
4 | |||||
5 | use Illuminate\Database\Eloquent\Model; |
||||
6 | use Illuminate\Support\Facades\Mail; |
||||
7 | use Illuminate\Support\Facades\Password; |
||||
8 | |||||
9 | trait MustVerifyNewEmail |
||||
10 | { |
||||
11 | /** |
||||
12 | * Deletes all previous attempts for this user, creates a new model/token |
||||
13 | * to verify the given email address and send the verification URL |
||||
14 | * to the new email address. |
||||
15 | * |
||||
16 | * @param string $email |
||||
17 | * @return \Illuminate\Database\Eloquent\Model|null |
||||
18 | */ |
||||
19 | public function newEmail(string $email): ?Model |
||||
20 | { |
||||
21 | if ($this->getEmailForVerification() === $email && $this->hasVerifiedEmail()) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() It seems like
hasVerifiedEmail() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
22 | return null; |
||||
23 | } |
||||
24 | |||||
25 | return $this->createPendingUserEmailModel($email)->tap(function ($model) { |
||||
26 | $this->sendPendingEmailVerificationMail($model); |
||||
27 | }); |
||||
28 | } |
||||
29 | |||||
30 | public function getEmailVerificationModel(): Model |
||||
31 | { |
||||
32 | $modelClass = config('verify-new-email.model'); |
||||
33 | |||||
34 | if (!$modelClass) { |
||||
35 | throw new InvalidEmailVerificationModelException; |
||||
36 | } |
||||
37 | |||||
38 | return app($modelClass); |
||||
0 ignored issues
–
show
|
|||||
39 | } |
||||
40 | |||||
41 | /** |
||||
42 | * Creates new PendingUserModel model for the given email. |
||||
43 | * |
||||
44 | * @param string $email |
||||
45 | * @return \Illuminate\Database\Eloquent\Model |
||||
46 | */ |
||||
47 | public function createPendingUserEmailModel(string $email): Model |
||||
48 | { |
||||
49 | $this->clearPendingEmail(); |
||||
50 | |||||
51 | return $this->getEmailVerificationModel()->create([ |
||||
52 | 'user_type' => get_class($this), |
||||
53 | 'user_id' => $this->getKey(), |
||||
0 ignored issues
–
show
It seems like
getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
54 | 'email' => $email, |
||||
55 | 'token' => Password::broker()->getRepository()->createNewToken(), |
||||
0 ignored issues
–
show
The method
createNewToken() does not exist on Illuminate\Auth\Passwords\TokenRepositoryInterface . Did you maybe mean create() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
56 | ]); |
||||
57 | } |
||||
58 | |||||
59 | /** |
||||
60 | * Returns the pending email address. |
||||
61 | * |
||||
62 | * @return string|null |
||||
63 | */ |
||||
64 | public function getPendingEmail(): ?string |
||||
65 | { |
||||
66 | return $this->getEmailVerificationModel()->forUser($this)->value('email'); |
||||
67 | } |
||||
68 | |||||
69 | /** |
||||
70 | * Deletes the pending email address models for this user. |
||||
71 | * |
||||
72 | * @return void |
||||
73 | */ |
||||
74 | public function clearPendingEmail() |
||||
75 | { |
||||
76 | $this->getEmailVerificationModel()->forUser($this)->get()->each->delete(); |
||||
77 | } |
||||
78 | |||||
79 | /** |
||||
80 | * Sends the VerifyNewEmail Mailable to the new email address. |
||||
81 | * |
||||
82 | * @param \Illuminate\Database\Eloquent\Model $pendingUserEmail |
||||
83 | * @return mixed |
||||
84 | */ |
||||
85 | public function sendPendingEmailVerificationMail(Model $pendingUserEmail) |
||||
86 | { |
||||
87 | $mailableClass = config('verify-new-email.mailable_for_first_verification'); |
||||
88 | |||||
89 | if ($pendingUserEmail->User->hasVerifiedEmail()) { |
||||
0 ignored issues
–
show
The method
hasVerifiedEmail() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
90 | $mailableClass = config('verify-new-email.mailable_for_new_email'); |
||||
91 | } |
||||
92 | |||||
93 | $mailable = new $mailableClass($pendingUserEmail); |
||||
94 | |||||
95 | return Mail::to($pendingUserEmail->email)->send($mailable); |
||||
0 ignored issues
–
show
Are you sure the usage of
Illuminate\Support\Facad...email)->send($mailable) targeting Illuminate\Mail\PendingMail::send() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
96 | } |
||||
97 | |||||
98 | /** |
||||
99 | * Grabs the pending user email address, generates a new token and sends the Mailable. |
||||
100 | * |
||||
101 | * @return \Illuminate\Database\Eloquent\Model|null |
||||
102 | */ |
||||
103 | public function resendPendingEmailVerificationMail(): ?Model |
||||
104 | { |
||||
105 | $pendingUserEmail = $this->getEmailVerificationModel()->forUser($this)->firstOrFail(); |
||||
106 | |||||
107 | return $this->newEmail($pendingUserEmail->email); |
||||
108 | } |
||||
109 | } |
||||
110 |