SentryAuthentication   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 23
lcom 0
cbo 1
dl 0
loc 157
rs 10
c 5
b 1
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
C login() 0 22 8
A register() 0 4 1
A activate() 0 15 3
A assignRole() 0 4 1
A logout() 0 4 1
A createActivation() 0 4 1
A createReminderCode() 0 4 1
A completeResetPassword() 0 4 1
A hasAccess() 0 10 2
A check() 0 8 2
A id() 0 8 2
1
<?php namespace Modules\User\Repositories\Sentry;
2
3
use Cartalyst\Sentry\Facades\Laravel\Sentry;
4
use Cartalyst\Sentry\Throttling\UserBannedException;
5
use Cartalyst\Sentry\Throttling\UserSuspendedException;
6
use Cartalyst\Sentry\Users\LoginRequiredException;
7
use Cartalyst\Sentry\Users\PasswordRequiredException;
8
use Cartalyst\Sentry\Users\UserNotActivatedException;
9
use Cartalyst\Sentry\Users\UserNotFoundException;
10
use Cartalyst\Sentry\Users\WrongPasswordException;
11
use Modules\Core\Contracts\Authentication;
12
use Modules\User\Events\UserHasActivatedAccount;
13
14
class SentryAuthentication implements Authentication
15
{
16
    /**
17
     * Authenticate a user
18
     * @param  array $credentials
19
     * @param  bool  $remember    Remember the user
20
     * @return mixed
21
     */
22
    public function login(array $credentials, $remember = false)
23
    {
24
        try {
25
            Sentry::authenticate($credentials, $remember);
26
27
            return false;
28
        } catch (LoginRequiredException $e) {
0 ignored issues
show
Bug introduced by
The class Cartalyst\Sentry\Users\LoginRequiredException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
29
            return 'Login field is required.';
30
        } catch (PasswordRequiredException $e) {
0 ignored issues
show
Bug introduced by
The class Cartalyst\Sentry\Users\PasswordRequiredException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
31
            return 'Password field is required.';
32
        } catch (WrongPasswordException $e) {
0 ignored issues
show
Bug introduced by
The class Cartalyst\Sentry\Users\WrongPasswordException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
33
            return 'Wrong password, try again.';
34
        } catch (UserNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class Cartalyst\Sentry\Users\UserNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
35
            return 'User was not found.';
36
        } catch (UserNotActivatedException $e) {
0 ignored issues
show
Bug introduced by
The class Cartalyst\Sentry\Users\UserNotActivatedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
37
            return 'User is not activated.';
38
        } catch (UserSuspendedException $e) {
0 ignored issues
show
Bug introduced by
The class Cartalyst\Sentry\Throttling\UserSuspendedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
39
            return 'User is suspended.';
40
        } catch (UserBannedException $e) {
0 ignored issues
show
Bug introduced by
The class Cartalyst\Sentry\Throttling\UserBannedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
41
            return 'User is banned.';
42
        }
43
    }
44
45
    /**
46
     * Register a new user.
47
     * @param  array $user
48
     * @return bool
49
     */
50
    public function register(array $user)
51
    {
52
        return Sentry::register($user);
53
    }
54
55
    /**
56
     * Activate the given used id
57
     * @param  int    $userId
58
     * @param  string $code
59
     * @return bool
60
     */
61
    public function activate($userId, $code)
62
    {
63
        $user = Sentry::findUserById($userId);
64
65
        try {
66
            $success = $user->attemptActivation($code);
67
            if ($success) {
68
                event(new UserHasActivatedAccount($user));
69
            }
70
71
            return $success;
72
        } catch (\Exception $e) {
73
            return false;
74
        }
75
    }
76
77
    /**
78
     * Assign a role to the given user.
79
     * @param  \Modules\User\Repositories\UserRepository $user
80
     * @param  \Modules\User\Repositories\RoleRepository $role
81
     * @return mixed
82
     */
83
    public function assignRole($user, $role)
84
    {
85
        return $role->users()->attach($user);
0 ignored issues
show
Bug introduced by
The method users() does not seem to exist on object<Modules\User\Repositories\RoleRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
    }
87
88
    /**
89
     * Log the user out of the application.
90
     * @return mixed
91
     */
92
    public function logout()
93
    {
94
        return Sentry::logout();
95
    }
96
97
    /**
98
     * Create an activation code for the given user
99
     * @param $user
100
     * @return mixed
101
     */
102
    public function createActivation($user)
103
    {
104
        return $user->getActivationCode();
105
    }
106
107
    /**
108
     * Create a reminders code for the given user
109
     * @param $user
110
     * @return mixed
111
     */
112
    public function createReminderCode($user)
113
    {
114
        return $user->getResetPasswordCode();
115
    }
116
117
    /**
118
     * Completes the reset password process
119
     * @param $user
120
     * @param  string $code
121
     * @param  string $password
122
     * @return bool
123
     */
124
    public function completeResetPassword($user, $code, $password)
125
    {
126
        return $user->attemptResetPassword($code, $password);
127
    }
128
129
    /**
130
     * Determines if the current user has access to given permission
131
     * @param $permission
132
     * @return bool
133
     */
134
    public function hasAccess($permission)
135
    {
136
        $user = Sentry::getUser();
137
138
        if (is_null($user)) {
139
            return false;
140
        }
141
142
        return $user->hasAccess($permission);
143
    }
144
145
    /**
146
     * Check if the user is logged in
147
     * @return mixed
148
     */
149
    public function check()
150
    {
151
        if (Sentry::check()) {
152
            return Sentry::getUser();
153
        }
154
155
        return false;
156
    }
157
158
    /**
159
     * Get the ID for the currently authenticated user
160
     * @return int
161
     */
162
    public function id()
163
    {
164
        if (! $user = $this->check()) {
165
            return;
166
        }
167
168
        return $user->id;
169
    }
170
}
171