Issues (16)

src/Controller/RegistrationController.php (2 issues)

1
<?php
2
3
namespace App\Controller;
4
5
use Awurth\Slim\Helper\Controller\RestController;
6
use Respect\Validation\Validator as V;
7
use Slim\Http\Request;
8
use Slim\Http\Response;
9
10
class RegistrationController extends RestController
11
{
12
    public function register(Request $request, Response $response)
13
    {
14
        $username = $request->getParam('username');
15
        $email = $request->getParam('email');
16
        $password = $request->getParam('password');
17
18
        $this->validator->request($request, [
0 ignored issues
show
Bug Best Practice introduced by
The property validator does not exist on App\Controller\RegistrationController. Since you implemented __get, consider adding a @property annotation.
Loading history...
19
            'username' => V::length(3, 25)->alnum('_')->noWhitespace(),
20
            'email' => V::noWhitespace()->email(),
21
            'password' => [
22
                'rules' => V::noWhitespace()->length(6, 25),
23
                'messages' => [
24
                    'length' => 'The password length must be between {{minValue}} and {{maxValue}} characters'
25
                ]
26
            ],
27
            'password_confirm' => [
28
                'rules' => V::equals($password),
29
                'messages' => [
30
                    'equals' => 'Passwords don\'t match'
31
                ]
32
            ]
33
        ]);
34
35
        if ($this->sentinel->findByCredentials(['login' => $username])) {
0 ignored issues
show
Bug Best Practice introduced by
The property sentinel does not exist on App\Controller\RegistrationController. Since you implemented __get, consider adding a @property annotation.
Loading history...
36
            $this->validator->addError('username', 'User already exists with this username.');
37
        }
38
39
        if ($this->sentinel->findByCredentials(['login' => $email])) {
40
            $this->validator->addError('email', 'User already exists with this email address.');
41
        }
42
43
        if ($this->validator->isValid()) {
44
            $role = $this->sentinel->findRoleByName('user');
45
46
            $user = $this->sentinel->registerAndActivate([
47
                'username' => $username,
48
                'email' => $email,
49
                'password' => $password
50
            ]);
51
52
            $role->users()->attach($user);
53
54
            return $this->created($response, 'login');
55
        }
56
57
        return $this->badRequest($response, $this->validator->getErrors());
58
    }
59
}
60