Completed
Push — develop ( 10b573...34b970 )
by Abdelrahman
09:38
created

GenericHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Handlers;
6
7
use Illuminate\Http\Request;
8
use Illuminate\Auth\Events\Login;
9
use Illuminate\Auth\Events\Lockout;
10
use Illuminate\Auth\Events\Registered;
11
use Illuminate\Contracts\Events\Dispatcher;
12
use Illuminate\Contracts\Container\Container;
13
use Illuminate\Contracts\Auth\Authenticatable;
14
use Rinvex\Fort\Notifications\RegistrationSuccessNotification;
15
use Rinvex\Fort\Notifications\AuthenticationLockoutNotification;
16
17
class GenericHandler
18
{
19
    /**
20
     * The container instance.
21
     *
22
     * @var \Illuminate\Container\Container
23
     */
24
    protected $app;
25
26
    /**
27
     * Create a new fort event listener instance.
28
     *
29
     * @param \Illuminate\Contracts\Container\Container $app
30
     */
31
    public function __construct(Container $app)
32
    {
33
        $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...
34
    }
35
36
    /**
37
     * Register the listeners for the subscriber.
38
     *
39
     * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
40
     */
41
    public function subscribe(Dispatcher $dispatcher)
42
    {
43
        $dispatcher->listen(Login::class, __CLASS__.'@login');
44
        $dispatcher->listen(Lockout::class, __CLASS__.'@lockout');
45
        $dispatcher->listen(Registered::class, __CLASS__.'@registered');
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 lockout(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 login(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: Cortex\Fort\Models\User, 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 registered(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: Cortex\Fort\Models\User, 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