CreateUserForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 84
c 0
b 0
f 0
ccs 29
cts 29
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 29 1
A callbackSuccess() 0 3 1
A callbackSubmit() 0 25 2
1
<?php
2
3
namespace Pamo\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pamo\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 Pamo\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 1
    public function __construct(ContainerInterface $di)
20
    {
21 1
        parent::__construct($di);
22 1
        $this->form->create(
23
            [
24 1
                "id" => __CLASS__,
25
                "legend" => "Create user",
26
            ],
27
            [
28 1
                "username" => [
29
                    "type"        => "text",
30
                ],
31
32
                "email" => [
33
                    "type"        => "email",
34
                ],
35
36
                "password" => [
37
                    "type"        => "password",
38
                ],
39
40
                "repeat-password" => [
41
                    "type"        => "password",
42
                ],
43
44
                "submit" => [
45 1
                    "type" => "submit",
46 1
                    "value" => "Create user",
47 1
                    "callback" => [$this, "callbackSubmit"]
48
                ],
49
            ]
50
        );
51 1
    }
52
53
54
55
    /**
56
     * Callback for submit-button which should return true if it could
57
     * carry out its work and false if something failed.
58
     *
59
     * @return boolean true if okey, false if something went wrong.
60
     */
61 1
    public function callbackSubmit()
62
    {
63
        // Get values from the submitted form
64 1
        $username      = $this->form->value("username");
65 1
        $email         = $this->form->value("email");
66 1
        $password      = $this->form->value("password");
67 1
        $passwordAgain = $this->form->value("repeat-password");
68
69
        // Check password matches
70 1
        if ($password !== $passwordAgain) {
71 1
            $this->form->rememberValues();
72 1
            $this->form->addOutput("Password did not match.");
73 1
            return false;
74
        }
75
76 1
        $user = new User();
77 1
        $user->setDb($this->di->get("dbqb"));
78 1
        $user->username = $username;
79 1
        $user->email = $email;
80 1
        $user->setPassword($password);
81 1
        $user->rank = 0;
82 1
        $user->voted = 0;
83 1
        $user->save();
84
85 1
        return true;
86
    }
87
88
    /**
89
     * Callback what to do if the form was successfully submitted, this
90
     * happen when the submit callback method returns true. This method
91
     * can/should be implemented by the subclass for a different behaviour.
92
     */
93 1
    public function callbackSuccess()
94
    {
95 1
        $this->di->get("response")->redirect("user/login")->send();
96 1
    }
97
}
98