RegistersUsers::confirmed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\EmailConfirmation\Traits;
4
5
use BeyondCode\EmailConfirmation\Events\Confirmed;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
use Illuminate\Auth\Events\Registered;
9
10
trait RegistersUsers
11
{
12
    use \Illuminate\Foundation\Auth\RegistersUsers {
13
        register as baseRegister;
14
    }
15
16
    /**
17
     * Get redirect path after a successful confirmation.
18
     *
19
     * @return string
20
     */
21 View Code Duplication
    public function redirectAfterConfirmationPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        if (method_exists($this, 'redirectConfirmationTo')) {
24
            return $this->redirectConfirmationTo();
0 ignored issues
show
Bug introduced by
The method redirectConfirmationTo() does not exist on BeyondCode\EmailConfirmation\Traits\RegistersUsers. Did you maybe mean confirm()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
25
        }
26
27
        return property_exists($this, 'redirectConfirmationTo') ? $this->redirectConfirmationTo : route('login');
0 ignored issues
show
Bug introduced by
The property redirectConfirmationTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
    }
29
30
    /**
31
     * Get redirect path after a registration that still needs to be confirmed.
32
     *
33
     * @return string
34
     */
35 View Code Duplication
    public function redirectAfterRegistrationPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        if (method_exists($this, 'redirectAfterRegistrationTo')) {
38
            return $this->redirectAfterRegistrationTo();
0 ignored issues
show
Bug introduced by
It seems like redirectAfterRegistrationTo() 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...
39
        }
40
41
        return property_exists($this, 'redirectAfterRegistrationTo') ? $this->redirectAfterRegistrationTo : route('login');
0 ignored issues
show
Bug introduced by
The property redirectAfterRegistrationTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
    }
43
44
    /**
45
     * Get redirect path after the confirmation was sent.
46
     *
47
     * @return string
48
     */
49 View Code Duplication
    public function redirectAfterResendConfirmationPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        if (method_exists($this, 'redirectAfterResendConfirmationTo')) {
52
            return $this->redirectAfterResendConfirmationTo();
0 ignored issues
show
Bug introduced by
The method redirectAfterResendConfirmationTo() does not exist on BeyondCode\EmailConfirmation\Traits\RegistersUsers. Did you maybe mean confirm()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
53
        }
54
55
        return property_exists($this, 'redirectAfterResendConfirmationTo') ? $this->redirectAfterResendConfirmationTo : route('login');
0 ignored issues
show
Bug introduced by
The property redirectAfterResendConfirmationTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
    }
57
58
    /**
59
     * Confirm a user with a given confirmation code.
60
     *
61
     * @param $confirmation_code
62
     * @return \Illuminate\Http\RedirectResponse
63
     */
64
    public function confirm($confirmation_code)
65
    {
66
        $model = $this->guard()->getProvider()->createModel();
67
68
        $user = $model->where('confirmation_code', $confirmation_code)->firstOrFail();
69
70
        $user->confirmation_code = null;
71
        $user->confirmed_at = now();
72
        $user->save();
73
74
        event(new Confirmed($user));
75
76
        return $this->confirmed($user)
77
            ?: redirect($this->redirectAfterConfirmationPath())->with('confirmation', __('confirmation::confirmation.confirmation_successful'));
78
    }
79
80
    /**
81
     * Resend a confirmation code to a user.
82
     *
83
     * @param  \Illuminate\Http\Request  $request
84
     * @return \Illuminate\Http\RedirectResponse
85
     */
86
    public function resendConfirmation(Request $request)
87
    {
88
        $model = $this->guard()->getProvider()->createModel();
89
90
        $user = $model->findOrFail($request->session()->pull('confirmation_user_id'));
0 ignored issues
show
Bug introduced by
The method pull cannot be called on $request->session() (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
91
        $this->sendConfirmationToUser($user);
92
93
        return redirect($this->redirectAfterResendConfirmationPath())->with('confirmation', __('confirmation::confirmation.confirmation_resent'));
94
    }
95
96
    /**
97
     * Handle a registration request for the application.
98
     *
99
     * @param  \Illuminate\Http\Request  $request
100
     * @return \Illuminate\Http\Response
101
     */
102
    public function register(Request $request)
103
    {
104
        $this->validator($request->all())->validate();
0 ignored issues
show
Bug introduced by
It seems like validator() 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...
105
106
        event(new Registered($user = $this->create($request->all())));
0 ignored issues
show
Bug introduced by
It seems like create() 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...
107
108
        $this->sendConfirmationToUser($user);
109
110
        return $this->registered($request, $user)
111
            ?: redirect($this->redirectAfterRegistrationPath())->with('confirmation', __('confirmation::confirmation.confirmation_info'));
112
    }
113
114
    /**
115
     * Send the confirmation code to a user.
116
     *
117
     * @param $user
118
     */
119
    protected function sendConfirmationToUser($user)
120
    {
121
        // Create the confirmation code
122
        $user->confirmation_code = Str::random(25);
123
        $user->save();
124
125
        // Notify the user
126
        $notification = app(config('confirmation.notification'));
127
        $user->notify($notification);
128
    }
129
130
131
    /**
132
     * The users email address has been confirmed.
133
     *
134
     * @param  mixed  $user
135
     * @return mixed
136
     */
137
    protected function confirmed($user) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
        //
139
    }
140
}
141