LinkBroker::sendMagicLink()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Soved\Laravel\Magic\Auth\Links;
4
5
use Closure;
6
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...
7
use UnexpectedValueException;
8
use Illuminate\Support\Facades\URL;
9
use Illuminate\Contracts\Auth\UserProvider;
10
use Soved\Laravel\Magic\Auth\Contracts\LinkBroker as LinkBrokerContract;
11
use Soved\Laravel\Magic\Auth\Contracts\CanMagicallyLogin as CanMagicallyLoginContract;
12
13
class LinkBroker implements LinkBrokerContract
14
{
15
    /**
16
     * The user provider implementation.
17
     *
18
     * @var \Illuminate\Contracts\Auth\UserProvider
19
     */
20
    protected $users;
21
22
    /**
23
     * Create a new magic link broker instance.
24
     *
25
     * @param  \Illuminate\Contracts\Auth\UserProvider  $users
26
     * @return void
27
     */
28
    public function __construct(UserProvider $users)
29
    {
30
        $this->users = $users;
31
    }
32
33
    /**
34
     * Send a magic link to a user.
35
     *
36
     * @param  array  $credentials
37
     * @return string
38
     */
39
    public function sendMagicLink(array $credentials)
40
    {
41
        $user = $this->getUser($credentials);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as $this->getUser($credentials) targeting Soved\Laravel\Magic\Auth...s\LinkBroker::getUser() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
42
43
        if (is_null($user)) {
0 ignored issues
show
introduced by
The condition is_null($user) is always true.
Loading history...
44
            return static::INVALID_USER;
45
        }
46
47
        $user->sendMagicLinkNotification(
48
            $this->createMagicLink($user)
49
        );
50
51
        return static::MAGIC_LINK_SENT;
52
    }
53
54
    /**
55
     * Log the user into the application.
56
     *
57
     * @param  \Illuminate\Http\Request  $request
58
     * @param  \Closure  $callback
59
     * @return string
60
     */
61
    public function login(
62
        Request $request,
63
        Closure $callback
64
    ) {
65
        $user = $this->validateLogin($request);
66
67
        if (!$user instanceof CanMagicallyLoginContract) {
0 ignored issues
show
introduced by
$user is never a sub-type of Soved\Laravel\Magic\Auth...racts\CanMagicallyLogin.
Loading history...
68
            return $user;
69
        }
70
71
        $callback($user);
72
73
        return static::USER_AUTHENTICATED;
74
    }
75
76
    /**
77
     * Validate a magic authentication request for the given user.
78
     *
79
     * @param  \Illuminate\Http\Request  $request
80
     * @return \Soved\Laravel\Magic\Auth\Traits\CanMagicallyLogin|string
81
     */
82
    protected function validateLogin(Request $request)
83
    {
84
        $credentials = $request->only([
85
            'id',
86
            'email',
87
            'updated_at',
88
        ]);
89
90
        if (is_null($user = $this->getUser($credentials))) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as $this->getUser($credentials) targeting Soved\Laravel\Magic\Auth...s\LinkBroker::getUser() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
introduced by
The condition is_null($user = $this->getUser($credentials)) is always true.
Loading history...
91
            return static::INVALID_USER;
92
        }
93
94
        if (!$request->hasValidSignature()) {
95
            return static::INVALID_SIGNATURE;
96
        }
97
98
        return $user;
99
    }
100
101
    /**
102
     * Get the user for the given credentials.
103
     *
104
     * @param  array  $credentials
105
     * @return \Soved\Laravel\Magic\Auth\Traits\CanMagicallyLogin|null
106
     *
107
     * @throws \UnexpectedValueException
108
     */
109
    public function getUser(array $credentials)
110
    {
111
        $user = $this->users->retrieveByCredentials($credentials);
112
113
        if ($user && !$user instanceof CanMagicallyLoginContract) {
114
            throw new UnexpectedValueException('User must implement CanMagicallyLogin interface.');
115
        }
116
117
        return $user;
118
    }
119
120
    /**
121
     * Create a new magic link.
122
     *
123
     * @param  \Soved\Laravel\Magic\Auth\Traits\CanMagicallyLogin  $user
124
     * @return string
125
     */
126
    public function createMagicLink(CanMagicallyLoginContract $user)
127
    {
128
        $email = $user->getEmailForMagicLink();
129
130
        return URL::temporarySignedRoute(
131
            'magic.login',
132
            now()->addMinutes(config('magic-auth.expiration')),
0 ignored issues
show
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

132
            now()->addMinutes(/** @scrutinizer ignore-call */ config('magic-auth.expiration')),
Loading history...
Bug introduced by
The function now 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

132
            /** @scrutinizer ignore-call */ 
133
            now()->addMinutes(config('magic-auth.expiration')),
Loading history...
133
            [
134
                'id'         => $user->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Soved\Laravel\Magic\Auth...racts\CanMagicallyLogin suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
135
                'email'      => $email,
136
                'updated_at' => $user->updated_at,
0 ignored issues
show
Bug introduced by
Accessing updated_at on the interface Soved\Laravel\Magic\Auth...racts\CanMagicallyLogin suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
137
            ]
138
        );
139
    }
140
}
141