AuthenticatesUsers   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 110
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticated() 0 8 1
A validateLogin() 0 3 1
A broker() 0 5 1
A sendLoginResponse() 0 6 2
A guard() 0 3 1
A sendFailedLoginResponse() 0 4 1
A login() 0 13 2
A authenticateUser() 0 3 1
1
<?php
2
3
namespace Soved\Laravel\Magic\Auth;
4
5
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Foundation\Auth\RedirectsUsers;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Auth\RedirectsUsers was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Soved\Laravel\Magic\Auth\Links\LinkBroker;
9
10
trait AuthenticatesUsers
11
{
12
    use RedirectsUsers;
13
14
    /**
15
     * Handle a magic authentication request to the application.
16
     *
17
     * @param  \Illuminate\Http\Request  $request
18
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Illuminate\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     */
20
    public function login(Request $request)
21
    {
22
        $this->validateLogin($request);
23
24
        $response = $this->broker()->login(
25
            $request, function ($user) {
26
                $this->authenticateUser($user);
27
            }
28
        );
29
30
        return $response == LinkBroker::USER_AUTHENTICATED
31
            ? $this->sendLoginResponse($request)
32
            : $this->sendFailedLoginResponse($response);
33
    }
34
35
    /**
36
     * Validate the user login request.
37
     *
38
     * @param  \Illuminate\Http\Request  $request
39
     * @return void
40
     */
41
    protected function validateLogin(Request $request)
42
    {
43
        $this->validate($request, ['email' => 'required|email']);
44
    }
45
46
    /**
47
     * Authenticate the given user.
48
     *
49
     * @param  \Soved\Laravel\Magic\Auth\Traits\CanMagicallyLogin  $user
50
     * @return void
51
     */
52
    protected function authenticateUser($user)
53
    {
54
        $this->guard()->login($user, config('magic-auth.remember'));
0 ignored issues
show
Bug introduced by
$user of type Soved\Laravel\Magic\Auth\Traits\CanMagicallyLogin is incompatible with the type Illuminate\Contracts\Auth\Authenticatable expected by parameter $user of Illuminate\Contracts\Auth\StatefulGuard::login(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $this->guard()->login(/** @scrutinizer ignore-type */ $user, config('magic-auth.remember'));
Loading history...
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $this->guard()->login($user, /** @scrutinizer ignore-call */ config('magic-auth.remember'));
Loading history...
55
    }
56
57
    /**
58
     * Send the response after the user was authenticated.
59
     *
60
     * @param  \Illuminate\Http\Request  $request
61
     * @return \Illuminate\Http\Response
62
     */
63
    protected function sendLoginResponse(Request $request)
64
    {
65
        $request->session()->regenerate();
66
67
        return $this->authenticated($request, $this->guard()->user())
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->authenticated($re...$this->guard()->user()) targeting Soved\Laravel\Magic\Auth...sUsers::authenticated() 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 getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
            ?: redirect()->intended($this->redirectPath());
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
            ?: /** @scrutinizer ignore-call */ redirect()->intended($this->redirectPath());
Loading history...
69
    }
70
71
    /**
72
     * The user has been authenticated.
73
     *
74
     * @param  \Illuminate\Http\Request  $request
75
     * @param  mixed  $user
76
     * @return mixed
77
     */
78
    protected function authenticated(
79
        Request $request,
80
        $user
81
    ) {
82
        $request->session()->put('viaMagicLink', true);
83
84
        // Invalidating all unexpired magic links
85
        $user->touch();
86
    }
87
88
    /**
89
     * Get the failed login response instance.
90
     *
91
     * @param  string  $response
92
     * @return \Illuminate\Http\RedirectResponse
93
     */
94
    protected function sendFailedLoginResponse(string $response)
95
    {
96
        return redirect($this->redirectPath())
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        return /** @scrutinizer ignore-call */ redirect($this->redirectPath())
Loading history...
97
            ->withErrors(['email' => __($response)]);
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
            ->withErrors(['email' => /** @scrutinizer ignore-call */ __($response)]);
Loading history...
98
    }
99
100
    /**
101
     * Get the broker to be used during magic authentication.
102
     *
103
     * @return \Soved\Laravel\Magic\Auth\Links\LinkBroker
104
     */
105
    public function broker()
106
    {
107
        $userProvider = Auth::getProvider();
108
109
        return new LinkBroker($userProvider);
110
    }
111
112
    /**
113
     * Get the guard to be used during magic authentication.
114
     *
115
     * @return \Illuminate\Contracts\Auth\StatefulGuard
116
     */
117
    protected function guard()
118
    {
119
        return Auth::guard();
120
    }
121
}
122