RegisterForm::callbackSubmit()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 24
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Almrooth\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Almrooth\Comment\User;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class RegisterForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 1
    public function __construct(DIInterface $di)
20
    {
21 1
        parent::__construct($di);
22
23 1
        $this->form->create(
24
            [
25 1
                "id" => __CLASS__,
26 1
                "use_fieldset" => false,
27 1
            ],
28
            [
29
                "username" => [
30 1
                    "type"          => "text",
31 1
                    "label"         => "Användarnamn",
32 1
                    "validation"    => ["not_empty"],
33 1
                ],
34
                        
35
                "password" => [
36 1
                    "type"          => "password",
37 1
                    "label"         => "Lösenord",
38 1
                    "validation"    => ["not_empty"],
39 1
                ],
40
41
                "password_confirmation" => [
42 1
                    "type"          => "password",
43 1
                    "label"         => "Bekräfta lösenord",
44 1
                    "validation"    => ["not_empty"],
45 1
                ],
46
47
                "email" => [
48 1
                    "type"          => "email",
49 1
                    "label"         => "E-post",
50 1
                    "validation"    => ["not_empty"],
51 1
                ],
52
53
                "submit" => [
54 1
                    "type" => "submit",
55 1
                    "value" => "Registrera",
56 1
                    "callback" => [$this, "callbackSubmit"],
57
                    "class" => "btn"
58 1
                ],
59
            ]
60 1
        );
61 1
    }
62
63
64
    /**
65
     * Callback for submit-button which should return true if it could
66
     * carry out its work and false if something failed.
67
     *
68
     * @return boolean true if okey, false if something went wrong.
69
     */
70
    public function callbackSubmit()
71
    {
72
        // Get values from the submitted form
73
        $username       = $this->form->value("username");
74
        $password       = $this->form->value("password");
75
        $password2      = $this->form->value("password_confirmation");
76
        $email          = $this->form->value("email");
77
78
        // Check password matches
79
        if ($password !== $password2) {
80
            $this->form->rememberValues();
81
            $this->form->addOutput("Password did not match.");
82
            return false;
83
        }
84
85
        // Check if username is taken
86
        $user = new User();
87
        $user->setDb($this->di->get("db"));
88
        $user->find("username", $username);
89
        
90
        if (isset($user->id)) {
91
            $this->form->addOutput("Username already taken.");
92
            return false;
93
        }
94
95
        // Save user to database
96
        $user = new User();
97
        $user->setDb($this->di->get("db"));
98
        $user->role         = "user";
99
        $user->username     = $username;
100
        $user->email        = $email;
101
        $user->setPassword($password);
102
        $user->save();
103
104
        // Redirect to login page
105
        $this->di->get("response")->redirect($this->di->get("url")->create("user/login"));
106
    }
107
}
108