RegisterController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 64
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B filters() 0 26 1
A actionIndex() 0 7 1
A actionSuccess() 0 4 1
A actionError() 0 4 1
A actionPost() 0 16 4
1
<?php
2
3
namespace App\Controllers;
4
5
use App\Components\Controller;
6
use App\Components\View;
7
use App\Models\User;
8
use Micro\Web\RequestInjector;
9
10
/**
11
 * Class RegisterController
12
 * @package App\Controllers
13
 */
14
class RegisterController extends Controller
15
{
16
    public function filters()
17
    {
18
        return [
19
            [
20
                'class' => '\Micro\Filter\AccessFilter',
21
                'actions' => ['success', 'error', 'index', 'post'],
22
                'rules' => [
23
                    [
24
                        'allow' => false,
25
                        'actions' => ['index', 'success', 'error', 'post'],
26
                        'users' => ['@'],
27
                        'message' => 'Only for not authorized!'
28
                    ]
29
                ]
30
            ],
31
            [
32
                'class' => '\Micro\Filter\CsrfFilter',
33
                'actions' => ['index']
34
            ],
35
            [
36
                'class' => '\Micro\Filter\XssFilter',
37
                'actions' => ['post'],
38
                'clean' => '*'
39
            ]
40
        ];
41
    }
42
43
    public function actionIndex()
44
    {
45
        $v = new View;
46
        $v->addParameter('model', new User);
47
48
        return $v;
49
    }
50
51
    public function actionSuccess()
52
    {
53
        return new View;
54
    }
55
56
    public function actionError()
57
    {
58
        return new View;
59
    }
60
61
    public function actionPost()
62
    {
63
        $body = (new RequestInjector)->build()->getParsedBody();
64
65
        if (!empty($body['User'])) {
66
            $user = new User();
67
            $user->setModelData($body['User']);
68
            $user->pass = md5($user->pass);
69
70
            if ($user->validate() && $user->save()) {
71
                return $this->redirect('/register/success');
72
            }
73
            return $this->redirect('/register/error');
74
        }
75
        return $this->redirect('/register');
76
    }
77
}
78