GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b8adf1...8f3529 )
by Pascal
01:09
created

MustVerifyNewEmail   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 101
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A newEmail() 0 10 3
A getEmailVerificationModel() 0 10 2
A createPendingUserEmailModel() 0 11 1
A getPendingEmail() 0 4 1
A clearPendingEmail() 0 4 1
A sendPendingEmailVerificationMail() 0 12 2
A resendPendingEmailVerificationMail() 0 6 1
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 getEmailForVerification() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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);
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
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
            'email'     => $email,
55
            'token'     => Password::broker()->getRepository()->createNewToken(),
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()) {
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);
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