CreateUserForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 84
ccs 0
cts 30
cp 0
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 34 1
A callbackSubmit() 0 22 2
A callbackLogin() 0 3 1
1
<?php
2
3
namespace Lyco\User\HTMLForm;
4
5
use Lyco\User\User;
6
use Anax\HTMLForm\FormModel;
7
use Psr\Container\ContainerInterface;
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 Lyco\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
                "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
                "login" => [
50
                    "type" => "submit",
51
                    "value" => "Back to Login",
52
                    "callback" => [$this, "callbackLogin"]
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 boolean true if okey, false if something went wrong.
65
     */
66
    public function callbackSubmit()
67
    {
68
69
        // Get values from the submitted form
70
        $acronym       = $this->form->value("acronym");
71
        $password      = $this->form->value("password");
72
        $passwordAgain = $this->form->value("password-again");
73
        // Check password matches
74
        if ($password !== $passwordAgain ) {
75
            $this->form->rememberValues();
76
            $this->form->addOutput("User or password did not match.");
77
            return false;
78
        }
79
        // Save to database
80
        $user = New User();
81
        $user->setDb($this->di->get("dbqb"));
82
        $user->acronym = $acronym;
83
        $user->setPassword($password);
84
        $user->save();
85
        $this->id = $user->id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
        $this->form->addOutput("User created");
87
        return true;
88
    }
89
90
    /**
91
     * Redirect to login
92
     */
93
    public function callbackLogin()
94
    {
95
        $this->di->get("response")->redirect("user/login");
96
    }
97
}
98