Test Failed
Push — master ( 10865e...d038e5 )
by Simon
10:06
created

CreateUserForm::callbackSubmit()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 27
loc 27
rs 8.8571
cc 2
eloc 19
nc 2
nop 0
1
<?php
2
3
namespace Schanihbg\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Schanihbg\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
    public function __construct(DIInterface $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
                "email" => [
33
                    "type" => "email",
34
                ],
35
36
                "password" => [
37
                    "type"        => "password",
38
                ],
39
40
                "password-again" => [
41
                    "type"        => "password",
42
                    "validation" => [
43
                        "match" => "password"
44
                    ],
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
     * Adminflag is always 0 when creating a user. Admin has to change it later.
63
     *
64
     * @return boolean true if okey, false if something went wrong.
65
     */
66 View Code Duplication
    public function callbackSubmit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        // Get values from the submitted form
69
        $acronym       = $this->form->value("acronym");
70
        $email         = $this->form->value("email");
71
        $password      = $this->form->value("password");
72
        $passwordAgain = $this->form->value("password-again");
73
        $adminflag     = 0;
74
75
        // Check password matches
76
        if ($password !== $passwordAgain) {
77
            $this->form->rememberValues();
78
            $this->form->addOutput("Password did not match.");
79
            return false;
80
        }
81
82
        $user = new User();
83
        $user->setDb($this->di->get("database"));
84
        $user->acronym = $acronym;
85
        $user->email = $email;
86
        $user->setPassword($password);
87
        $user->adminflag = $adminflag;
88
        $user->save();
89
90
        $this->form->addOutput("User was created.");
91
        return true;
92
    }
93
}
94