CreateUserForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 55.26%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 21
cts 38
cp 0.5526
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 38 1
B callbackSubmit() 0 31 2
1
<?php
2
3
namespace Anax\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Anax\User\User;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class CreateUserForm 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 1
        $this->form->create(
23
            [
24 1
                "id" => __CLASS__,
25 1
                "legend" => "Create user",
26 1
            ],
27
            [
28
                "acronym" => [
29 1
                    "type"        => "text",
30 1
                ],
31
32
                "email" => [
33 1
                    "type"        => "text",
34
                    //"description" => "Here you can place a description.",
35
                    //"placeholder" => "Here is a placeholder",
36 1
                ],
37
38
                "password" => [
39 1
                    "type"        => "password",
40 1
                ],
41
42
                "password-again" => [
43 1
                    "type"        => "password",
44
                    "validation" => [
45
                        "match" => "password"
46 1
                    ],
47 1
                ],
48
49
                "submit" => [
50 1
                    "type" => "submit",
51 1
                    "value" => "Create user",
52 1
                    "callback" => [$this, "callbackSubmit"]
53 1
                ],
54
            ]
55 1
        );
56 1
    }
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 boolean true if okey, false if something went wrong.
65
     */
66
    public function callbackSubmit()
67
    {
68
        // Get values from the submitted form
69
        $acronym       = $this->form->value("acronym");
70
        $email       = $this->form->value("email");
71
        $password      = $this->form->value("password");
72
        $passwordAgain = $this->form->value("password-again");
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("db");
83
        // $password = password_hash($password, PASSWORD_DEFAULT);
84
        // $db->connect()
85
        //    ->insert("User", ["acronym", "password"])
86
        //    ->execute([$acronym, $password]);
87
        $user = new User();
88
        $user->setDb($this->di->get("db"));
89
        $user->acronym = $acronym;
90
        $user->email = $email;
91
        $user->setPassword($password);
92
        $user->save();
93
94
        $this->di->get("response")->redirect("user/login");
95
        return true;
96
    }
97
}
98