CreateUserForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 32
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Anax\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
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 Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Anax\User\HTMLForm\Psr\C...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
                "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
                ],
46
47
                "submit" => [
48
                    "type" => "submit",
49
                    "value" => "Create user",
50
                    "callback" => [$this, "callbackSubmit"]
51
                ],
52
            ]
53
        );
54
    }
55
56
57
58
    /**
59
     * Callback for submit-button which should return true if it could
60
     * carry out its work and false if something failed.
61
     *
62
     * @return boolean true if okey, false if something went wrong.
63
     */
64
    public function callbackSubmit()
65
    {
66
        // Get values from the submitted form
67
        $username      = $this->form->value("username");
68
        $password      = $this->form->value("password");
69
        $passwordAgain = $this->form->value("password-again");
70
        $email = $this->form->value("email");
71
72
        // Check password matches
73
        if ($password !== $passwordAgain) {
74
            $this->form->rememberValues();
75
            $this->form->addOutput("Password did not match.");
76
            return false;
77
        }
78
        $user = new User();
79
        $user->setDb($this->di->get("dbqb"));
80
        $user->username = $username;
81
        $user->email = $email;
0 ignored issues
show
Bug introduced by
The property email does not seem to exist on Anax\User\User.
Loading history...
82
        $user->setPassword($password);
83
        $user->save();
84
85
        $this->form->addOutput("User was created.");
86
        return true;
87
    }
88
}
89