Passed
Branch master (3ea22d)
by Fariz
03:08
created

Auth::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * StupidlySimple - A PHP Framework For Lazy Developers.
4
 *
5
 * @author		Fariz Luqman <[email protected]>
6
 * @copyright	2017 Fariz Luqman
7
 * @license		MIT
8
 *
9
 * @link		https://stupidlysimple.github.io/
10
 */
11
12
namespace Controller;
13
14
use Request;
15
use Response;
16
use Sentry;
0 ignored issues
show
Bug introduced by
The type Sentry 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...
17
use Viewer;
0 ignored issues
show
Bug introduced by
The type Viewer 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...
18
19
class Auth
20
{
21
    public function __construct()
22
    {
23
    }
24
25
    public function displayRegisterPage()
26
    {
27
        Viewer::file('resources/views/auth/register');
28
    }
29
30
    public function displayLoginPage()
31
    {
32
        Viewer::file('resources/views/auth/login');
33
    }
34
35
    public function doAuthenticate()
36
    {
37
        try {
38
            // Login credentials
39
            $credentials = [
40
                'email'    => Request::get('email'),
41
                'password' => Request::get('password'),
42
            ];
43
            // Authenticate the user
44
            $user = Sentry::authenticate($credentials, false);
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
45
        } catch (\Cartalyst\Sentry\Users\LoginRequiredException $e) {
46
            Response::redirect('login')->with([
47
                'login_message'=> 'Login credentials not supplied',
48
                'type'         => 'alert-danger',
49
            ]);
50
        } catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e) {
51
            Response::redirect('login')->with([
52
                'login_message'=> 'Password field is required',
53
                'type'         => 'alert-danger',
54
            ]);
55
        } catch (\Cartalyst\Sentry\Users\WrongPasswordException $e) {
56
            Response::redirect('login')->with([
57
                'login_message'=> 'Wrong password, try again.',
58
                'type'         => 'alert-danger',
59
            ]);
60
        } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
61
            Response::redirect('login')->with([
62
                'login_message'=> 'User not found.',
63
                'type'         => 'alert-danger',
64
            ]);
65
        } catch (\Cartalyst\Sentry\Users\UserNotActivatedException $e) {
66
            Response::redirect('login')->with([
67
                'login_message'=> 'User is not activated.',
68
                'type'         => 'alert-danger',
69
            ]);
70
        } finally {
71
            if (Sentry::check() === true) {
72
                Admin::redirectToAdminHome();
73
            } else {
74
                Response::redirect('login')->with([
75
                    'login_message'=> 'Unable to login',
76
                    'type'         => 'alert-danger',
77
                ]);
78
            }
79
        }
80
    }
81
82
    public function doRegister()
83
    {
84
        try {
85
            $user = Sentry::register([
86
                'email'      => Request::get('email'),
87
                'password'   => Request::get('password'),
88
                'first_name' => Request::get('first_name'),
89
                'last_name'  => Request::get('last_name'),
90
            ], $activate = true);
91
        } catch (\Cartalyst\Sentry\Users\LoginRequiredException $e) {
92
            Response::redirect('register')->with([
93
                'login_message'=> 'Login credentials not supplied',
94
                'type'         => 'alert-danger',
95
            ]);
96
        } catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e) {
97
            Response::redirect('register')->with([
98
                'login_message'=> 'Password field is required',
99
                'type'         => 'alert-danger',
100
            ]);
101
        } catch (\Cartalyst\Sentry\Users\UserExistsException $e) {
102
            Response::redirect('register')->with([
103
                'login_message'=> 'User with that login already exist.',
104
                'type'         => 'alert-danger',
105
            ]);
106
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
107
        } finally {
108
            if ($user) {
109
                Response::redirect('login')->with([
110
                    'login_message'=> 'Registration successful. You can now login.',
111
                    'type'         => 'alert-success',
112
                ]);
113
            }
114
        }
115
    }
116
117
    public function doLogout()
118
    {
119
        Sentry::logout();
120
        Response::redirect('login');
121
    }
122
}
123