CreateUserForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
nc 2
nop 0
dl 0
loc 24
c 1
b 0
f 0
cc 2
rs 9.7998
1
<?php
2
3
namespace Pan\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pan\User\User;
8
9
/**
10
 * Form to create an item.
11
 */
12
class CreateUserForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Pan\User\HTMLForm\Psr\Container\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct(ContainerInterface $di)
20
    {
21
        parent::__construct($di);
22
        $this->form->create(
23
            [
24
                "id" => __CLASS__,
25
                "legend" => "Register",
26
            ],
27
            [
28
                "username" => [
29
                    "type"        => "text",
30
                ],
31
32
                "password" => [
33
                    "type"        => "password",
34
                ],
35
36
                "password-again" => [
37
                    "type"        => "password",
38
                    "validation" => [
39
                        "match" => "password"
40
                    ],
41
                ],
42
43
                "email" => [
44
                    "type" =>"email",
45
                    "label" => "Email",
46
                    "validation" => ["email"],
47
                ],
48
49
                "submit" => [
50
                    "type" => "submit",
51
                    "value" => "Register",
52
                    "callback" => [$this, "callbackSubmit"]
53
                ],
54
            ]
55
        );
56
    }
57
58
59
60
    /**
61
     * Callback for submit-button which should return true if it could
62
     * carry out its work and false if something failed.
63
     *
64
     * @return bool true if okey, false if something went wrong.
65
     */
66
    public function callbackSubmit() : bool
67
    {
68
        // Get values from the submitted form
69
        $username       = $this->form->value("username");
70
        $password      = $this->form->value("password");
71
        $passwordAgain = $this->form->value("password-again");
72
        $email         = $this->form->value("email");
73
74
        // Check password matches
75
        if ($password !== $passwordAgain ) {
76
            $this->form->rememberValues();
77
            $this->form->addOutput("Password did not match.");
78
            return false;
79
        }
80
81
        // Save to database
82
        $db = $this->di->get("dbqb");
83
        $password = password_hash($password, PASSWORD_DEFAULT);
84
        $db->connect()
85
        ->insert("users", ["username", "password", "email"])
86
        ->execute([$username, $password, $email]);
87
88
        // $this->form->addOutput("User was created.");
89
        return true;
90
    }
91
92
93
94
    /**
95
     * Callback what to do if the form was successfully submitted, this
96
     * happen when the submit callback method returns true. This method
97
     * can/should be implemented by the subclass for a different behaviour.
98
     */
99
    public function callbackSuccess()
100
    {
101
        $this->di->get("response")->redirect("user")->send();
102
    }
103
    //
104
    // public function callbackFail()
105
    // {
106
    //     $this->di->get("response")->redirectSelf()->send();
107
    // }
108
}
109