UserAuthenticator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 140
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 3
dl 7
loc 140
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A attempt() 0 13 3
A authenticate() 0 20 4
A login() 0 6 1
A logout() 0 14 3
A kickOut() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Anomaly\UsersModule\User;
2
3
use Anomaly\Streams\Platform\Addon\Extension\ExtensionCollection;
4
use Anomaly\UsersModule\User\Authenticator\Contract\AuthenticatorExtensionInterface;
5
use Anomaly\UsersModule\User\Contract\UserInterface;
6
use Anomaly\UsersModule\User\Event\UserWasKickedOut;
7
use Anomaly\UsersModule\User\Event\UserWasLoggedIn;
8
use Anomaly\UsersModule\User\Event\UserWasLoggedOut;
9
use Illuminate\Contracts\Auth\Guard;
10
use Illuminate\Contracts\Container\Container;
11
use Illuminate\Contracts\Events\Dispatcher;
12
use Illuminate\Http\RedirectResponse;
13
14
/**
15
 * Class UserAuthenticator
16
 *
17
 * @link   http://pyrocms.com/
18
 * @author PyroCMS, Inc. <[email protected]>
19
 * @author Ryan Thompson <[email protected]>
20
 */
21
class UserAuthenticator
22
{
23
24
    /**
25
     * Laravel's authentication.
26
     *
27
     * @var Guard
28
     */
29
    protected $guard;
30
31
    /**
32
     * The event dispatcher.
33
     *
34
     * @var Dispatcher
35
     */
36
    protected $events;
37
38
    /**
39
     * The service container.
40
     *
41
     * @var Container
42
     */
43
    protected $container;
44
45
    /**
46
     * The extension collection.
47
     *
48
     * @var ExtensionCollection
49
     */
50
    protected $extensions;
51
52
    /**
53
     * Create a new Authenticator instance.
54
     *
55
     * @param Guard               $guard
56
     * @param Dispatcher          $events
57
     * @param Container           $container
58
     * @param ExtensionCollection $extensions
59
     */
60 View Code Duplication
    public function __construct(Guard $guard, Dispatcher $events, Container $container, ExtensionCollection $extensions)
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...
61
    {
62
        $this->guard      = $guard;
63
        $this->events     = $events;
64
        $this->container  = $container;
65
        $this->extensions = $extensions;
66
    }
67
68
    /**
69
     * Attempt to login a user.
70
     *
71
     * @param  array $credentials
72
     * @param  bool  $remember
73
     * @return bool|UserInterface
74
     */
75
    public function attempt(array $credentials, $remember = false)
76
    {
77
        if ($response = $this->authenticate($credentials)) {
78
79
            if ($response instanceof UserInterface) {
80
                $this->login($response, $remember);
81
            }
82
83
            return $response;
84
        }
85
86
        return false;
87
    }
88
89
    /**
90
     * Attempt to authenticate the credentials.
91
     *
92
     * @param  array $credentials
93
     * @return bool|UserInterface
94
     */
95
    public function authenticate(array $credentials)
96
    {
97
        $authenticators = $this->extensions->search('anomaly.module.users::authenticator.*');
98
99
        /* @var AuthenticatorExtensionInterface $authenticator */
100
        foreach ($authenticators as $authenticator) {
101
102
            $response = $authenticator->authenticate($credentials);
103
104
            if ($response instanceof UserInterface) {
105
                return $response;
106
            }
107
108
            if ($response instanceof RedirectResponse) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Http\RedirectResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
109
                return $response;
110
            }
111
        }
112
113
        return false;
114
    }
115
116
    /**
117
     * Force login a user.
118
     *
119
     * @param UserInterface $user
120
     * @param bool          $remember
121
     */
122
    public function login(UserInterface $user, $remember = false)
123
    {
124
        $this->guard->login($user, $remember);
125
126
        $this->events->fire(new UserWasLoggedIn($user));
127
    }
128
129
    /**
130
     * Logout a user.
131
     *
132
     * @param UserInterface $user
133
     */
134
    public function logout(UserInterface $user = null)
135
    {
136
        if (!$user) {
137
            $user = $this->guard->user();
138
        }
139
140
        if (!$user) {
141
            return;
142
        }
143
144
        $this->guard->logout($user);
145
146
        $this->events->fire(new UserWasLoggedOut($user));
147
    }
148
149
    /**
150
     * Kick out a user. They've been bad.
151
     *
152
     * @param UserInterface $user
153
     */
154
    public function kickOut(UserInterface $user, $reason)
155
    {
156
        $this->guard->logout($user);
157
158
        $this->events->fire(new UserWasKickedOut($user, $reason));
159
    }
160
}
161