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 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 28
ccs 0
cts 8
cp 0
crap 2
rs 9.7333
1
<?php
2
3
namespace Aiur18\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Aiur18\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 Aiur18\User\HTMLForm\Psr...iner\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" => "Create user",
26
            ],
27
            [
28
                "acronym" => [
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
                "submit" => [
44
                    "type" => "submit",
45
                    "value" => "Create user",
46
                    "callback" => [$this, "callbackSubmit"]
47
                ],
48
            ]
49
        );
50
    }
51
52
53
    /** Method 
54
     */
55
    public function callbackSubmit()
56
    {
57
        // Get values from the submitted form
58
        $acronym       = $this->form->value("acronym");
59
        $password      = $this->form->value("password");
60
        $passwordAgain = $this->form->value("password-again");
61
62
        // Check password matches
63
        if ($password !== $passwordAgain) {
64
            $this->form->rememberValues();
65
            $this->form->addOutput("Password did not match.");
66
            return false;
67
        }
68
69
        $user = new User();
70
        $user->setDb($this->di->get("dbqb"));
71
        $user->acronym = $acronym;
72
        $user->setPassword($password);
73
        $user->save();
74
75
        $this->form->addOutput("User was created.");
76
        return true;
77
    }
78
}
79