Completed
Push — develop ( 9664f4...8505c4 )
by Abdelrahman
06:05
created

GenericHandler::emailVerificationSuccess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Fort\Handlers;
6
7
use Illuminate\Http\Request;
8
use Illuminate\Auth\Events\Login;
9
use Illuminate\Auth\Events\Lockout;
10
use Illuminate\Contracts\Events\Dispatcher;
11
use Illuminate\Contracts\Container\Container;
12
use Illuminate\Contracts\Auth\Authenticatable;
13
use Rinvex\Fort\Notifications\RegistrationSuccessNotification;
14
use Rinvex\Fort\Notifications\AuthenticationLockoutNotification;
15
16
class GenericHandler
17
{
18
    /**
19
     * The container instance.
20
     *
21
     * @var \Illuminate\Container\Container
22
     */
23
    protected $app;
24
25
    /**
26
     * Create a new fort event listener instance.
27
     *
28
     * @param \Illuminate\Contracts\Container\Container $app
29
     */
30
    public function __construct(Container $app)
31
    {
32
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
$app is of type object<Illuminate\Contracts\Container\Container>, but the property $app was declared to be of type object<Illuminate\Container\Container>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
33
    }
34
35
    /**
36
     * Register the listeners for the subscriber.
37
     *
38
     * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
39
     */
40
    public function subscribe(Dispatcher $dispatcher)
41
    {
42
        $dispatcher->listen(Login::class, __CLASS__.'@authLogin');
43
        $dispatcher->listen(Lockout::class, __CLASS__.'@authLockout');
44
        $dispatcher->listen('rinvex.fort.register.success', __CLASS__.'@registerSuccess');
45
        $dispatcher->listen('rinvex.fort.register.social.success', __CLASS__.'@registerSocialSuccess');
46
    }
47
48
    /**
49
     * Listen to the authentication lockout event.
50
     *
51
     * @param \Illuminate\Http\Request $request
52
     *
53
     * @return void
54
     */
55
    public function authLockout(Request $request): void
56
    {
57
        if (config('rinvex.fort.throttle.lockout_email')) {
58
            $user = get_login_field($loginfield = $request->get('loginfield')) === 'email' ? app('rinvex.fort.user')->where('email', $loginfield)->first() : app('rinvex.fort.user')->where('username', $loginfield)->first();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 222 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
59
60
            $user->notify(new AuthenticationLockoutNotification($request));
61
        }
62
    }
63
64
    /**
65
     * Listen to the authentication login event.
66
     *
67
     * @param \Illuminate\Auth\Events\Login $event
68
     *
69
     * @return void
70
     */
71
    public function authLogin(Login $event): void
72
    {
73
        ! config('rinvex.fort.persistence') === 'single' || $event->user->sessions()->delete();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method sessions() does only exist in the following implementations of said interface: Rinvex\Fort\Models\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
74
    }
75
76
    /**
77
     * Listen to the register success event.
78
     *
79
     * @param \Illuminate\Contracts\Auth\Authenticatable $user
80
     *
81
     * @return void
82
     */
83
    public function registerSuccess(Authenticatable $user): void
84
    {
85
        // Send welcome email
86
        if (config('rinvex.fort.registration.welcome_email')) {
87
            $user->notify(new RegistrationSuccessNotification());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method notify() does only exist in the following implementations of said interface: Rinvex\Fort\Models\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
88
        }
89
    }
90
91
    /**
92
     * Listen to the register social success event.
93
     *
94
     * @param \Illuminate\Contracts\Auth\Authenticatable $user
95
     *
96
     * @return void
97
     */
98
    public function registerSocialSuccess(Authenticatable $user): void
99
    {
100
        // Send welcome email
101
        if (config('rinvex.fort.registration.welcome_email')) {
102
            $user->notify(new RegistrationSuccessNotification(true));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method notify() does only exist in the following implementations of said interface: Rinvex\Fort\Models\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
103
        }
104
    }
105
}
106