CreateUserForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 1
dl 0
loc 28
ccs 8
cts 8
cp 1
crap 1
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Seb\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Seb\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 Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Seb\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 2
    public function __construct(ContainerInterface $di)
20
    {
21 2
        parent::__construct($di);
22 2
        $this->form->create(
23
            [
24 2
                "id" => __CLASS__,
25
                "legend" => "Create user",
26
            ],
27
            [
28
                "acronym" => [
29 2
                    "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
                "submit" => [
44 2
                    "type" => "submit",
45 2
                    "value" => "Create user",
46 2
                    "callback" => [$this, "callbackSubmit"]
47
                ],
48
            ]
49
        );
50 2
    }
51
52
53
54
    /**
55
     * Callback for submit-button which should return true if it could
56
     * carry out its work and false if something failed.
57
     *
58
     * @return boolean true if okey, false if something went wrong.
59
     */
60
    public function callbackSubmit()
61
    {
62
        // Get values from the submitted form
63
        $acronym       = $this->form->value("acronym");
64
        $password      = $this->form->value("password");
65
        $passwordAgain = $this->form->value("password-again");
66
67
        // Check password matches
68
        if ($password !== $passwordAgain) {
69
            $this->form->rememberValues();
70
            $this->form->addOutput("Password did not match.");
71
            return false;
72
        }
73
74
        // Save to database
75
        // $db = $this->di->get("dbqb");
76
        // $password = password_hash($password, PASSWORD_DEFAULT);
77
        // $db->connect()
78
        //    ->insert("User", ["acronym", "password"])
79
        //    ->execute([$acronym])
80
        //    ->fetch();
81
        $user = new User();
82
        $user->setDb($this->di->get("dbqb"));
83
        $user->acronym = $acronym;
84
        $user->setPassword($password);
85
        $user->score = 0;
86
        $user->save();
87
88
        $this->form->addOutput("User was created.");
89
        return true;
90
    }
91
}
92