CreateUserForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 59.61%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 101
c 0
b 0
f 0
ccs 31
cts 52
cp 0.5961
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 49 1
B callbackSubmit() 0 35 3
1
<?php
2
3
namespace Peto16\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Peto16\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 Anax\DI\DIInterface $di a service container
18
     */
19 2
    public function __construct(DIInterface $di)
20
    {
21 2
        parent::__construct($di);
22 2
        $this->form->create(
23
            [
24 2
                "id" => __CLASS__,
25 2
                "legend" => "Skapa användare",
26 2
            ],
27
            [
28
                "username" => [
29 2
                    "label"       => "Användarnamn",
30 2
                    "type"        => "text",
31 2
                ],
32
33
                "firstname" => [
34 2
                    "label"       => "Förnamn",
35 2
                    "type"        => "text",
36 2
                ],
37
38
                "lastname" => [
39 2
                    "label"       => "Efternamn",
40 2
                    "type"        => "text",
41 2
                ],
42
43
                "email" => [
44 2
                    "label"       => "Epost",
45 2
                    "type"        => "email",
46 2
                ],
47
48
                "password" => [
49 2
                    "label"       => "Lösenord",
50 2
                    "type"        => "password",
51 2
                ],
52
53
                "password-again" => [
54 2
                    "label"       => "Bekräfta lösenord",
55 2
                    "type"        => "password",
56
                    "validation" => [
57
                        "match" => "password"
58 2
                    ],
59 2
                ],
60
                "submit" => [
61 2
                    "type" => "submit",
62 2
                    "value" => "Skapa",
63 2
                    "callback" => [$this, "callbackSubmit"]
64 2
                ],
65
            ]
66 2
        );
67 2
    }
68
69
70
71
    /**
72
     * Callback for submit-button which should return true if it could
73
     * carry out its work and false if something failed.
74
     *
75
     * @return boolean true if okey, false if something went wrong.
76
     */
77
    public function callbackSubmit()
78
    {
79
        // Get values from the submitted form
80
        $password      = $this->form->value("password");
81
        $passwordAgain = $this->form->value("password-again");
82
83
84
        // Check password matches
85
        if ($password !== $passwordAgain) {
86
            $this->form->rememberValues();
87
            $this->form->addOutput("Lösenorden är ej identiska.");
88
            return false;
89
        }
90
91
        // Create a new user.
92
        $user = new User();
93
        $user->username = $this->form->value("username");
94
        $user->firstname = $this->form->value("firstname");
95
        $user->lastname = $this->form->value("lastname");
96
        $user->email = $this->form->value("email");
97
        $user->password = $user->hashPassword($password);
98
        $user->enabled = 1;
99
        $user->administrator = 0;
100
101
        try {
102
            // Save user
103
            $this->di->get("userService")->createUser($user);
104
        } catch (\Peto16\User\Exception $e) {
105
            $this->form->addOutput($e->getMessage());
106
            return false;
107
        }
108
109
        $this->form->addOutput("Användare skapad.");
110
        return true;
111
    }
112
}
113