Completed
Push — master ( 2c69ad...5eb3ba )
by Alexis
01:46
created

AuthController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B login() 0 23 4
B register() 0 54 5
A logout() 0 6 1
1
<?php
2
3
namespace Security\Controller;
4
5
use App\Controller\Controller;
6
use Cartalyst\Sentinel\Checkpoints\ThrottlingException;
7
use Respect\Validation\Validator as V;
8
use Slim\Http\Request;
9
use Slim\Http\Response;
10
11
class AuthController extends Controller
12
{
13
    public function login(Request $request, Response $response)
14
    {
15
        if ($request->isPost()) {
16
            $credentials = [
17
                'username' => $request->getParam('username'),
18
                'password' => $request->getParam('password')
19
            ];
20
            $remember = $request->getParam('remember', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
21
22
            try {
23
                if ($this->auth->authenticate($credentials, $remember)) {
24
                    $this->flash('success', 'You are now logged in.');
25
                    return $this->redirect($response, 'home');
26
                } else {
27
                    $this->validator->addError('auth', 'Bad username or password');
28
                }
29
            } catch (ThrottlingException $e) {
30
                $this->validator->addError('auth', 'Too many attempts!');
31
            }
32
        }
33
34
        return $this->view->render($response, 'Auth/login.twig');
35
    }
36
37
    public function register(Request $request, Response $response)
38
    {
39
        if ($request->isPost()) {
40
            $username = $request->getParam('username');
41
            $email = $request->getParam('email');
42
            $password = $request->getParam('password');
43
44
            $this->validator->request($request, [
45
                'username' => V::length(3, 25)->alnum('_')->noWhitespace(),
46
                'email' => V::noWhitespace()->email(),
47
                'password' => [
48
                    'rules' => V::noWhitespace()->length(6, 25),
49
                    'messages' => [
50
                        'length' => 'The password length must be between {{minValue}} and {{maxValue}} characters'
51
                    ]
52
                ],
53
                'password_confirm' => [
54
                    'rules' => V::equals($password),
55
                    'messages' => [
56
                        'equals' => 'Passwords don\'t match'
57
                    ]
58
                ]
59
            ]);
60
61
            if ($this->auth->findByCredentials(['login' => $username])) {
62
                $this->validator->addError('username', 'This username is already used.');
63
            }
64
65
            if ($this->auth->findByCredentials(['login' => $email])) {
66
                $this->validator->addError('email', 'This email is already used.');
67
            }
68
69
            if ($this->validator->isValid()) {
70
                $role = $this->auth->findRoleByName('User');
71
72
                $user = $this->auth->registerAndActivate([
73
                    'username' => $username,
74
                    'email' => $email,
75
                    'password' => $password,
76
                    'permissions' => [
77
                        'user.delete' => 0
78
                    ]
79
                ]);
80
81
                $role->users()->attach($user);
82
83
                $this->flash('success', 'Your account has been created.');
84
85
                return $this->redirect($response, 'login');
86
            }
87
        }
88
89
        return $this->view->render($response, 'Auth/register.twig');
90
    }
91
92
    public function logout(Request $request, Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        $this->auth->logout();
95
96
        return $this->redirect($response, 'home');
97
    }
98
}
99