CreateUserForm::callbackSubmit()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 10.7052

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 3
nop 0
dl 0
loc 28
ccs 7
cts 18
cp 0.3889
crap 10.7052
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Alvo\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Alvo\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
0 ignored issues
show
Bug introduced by
The type Alvo\User\HTMLForm\Anax\DI\DIInterface was not found. Did you mean Anax\DI\DIInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19 1
    public function __construct(DIInterface $di)
20
    {
21 1
        parent::__construct($di);
22
23 1
        $this->form->create(
24
            [
25 1
                "id" => __CLASS__,
26
                "legend" => "Create user",
27
                "class" => "center form",
28
                "wrapper-element" => "div",
29
                "use_fieldset" => false,
30
                // "br-after-label" => false,
31
            ],
32
            [
33 1
                "email" => [
34 1
                    "type"        => "email",
35
                    "validation" => [
36
                        "custom_test" => [
37 1
                            "message" => "User with this email is already registered",
38 1 View Code Duplication
                            "test" => function ($email) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
39
                                $user = new User();
40
                                $user->setDb($this->di->get("db"));
41
                                $check = $user->find('email', $email);
42
                                return !$check;
43 1
                            }
44
                        ],
45 1
                        "not_empty"
46
                    ]
47
                ],
48
49
                "password" => [
50
                    "type"        => "password",
51
                    "validation" => [
52
                        "not_empty"
53
                    ],
54
                ],
55
56
                "password-again" => [
57
                    "type"        => "password",
58
                    "validation" => [
59
                        "match" => "password"
60
                    ],
61
                ],
62
63
                "submit" => [
64 1
                    "type" => "submit",
65 1
                    "value" => "Create user",
66 1
                    "callback" => [$this, "callbackSubmit"],
67 1
                    "class" => "btn btn-success"
68
                ],
69
            ]
70
        );
71 1
    }
72
73
74
75
    /**
76
     * Callback for submit-button which should return true if it could
77
     * carry out its work and false if something failed.
78
     *
79
     * @return boolean true if okey, false if something went wrong.
80
     */
81 1
    public function callbackSubmit()
82
    {
83
        // Get values from the submitted form
84 1
        $email         = $this->form->value("email");
85 1
        $password      = $this->form->value("password");
86 1
        $passwordAgain = $this->form->value("password-again");
87
88
        // Check password matches
89 1
        if ($password !== $passwordAgain) {
90
            $this->form->rememberValues();
91
            $this->form->addOutput("Password did not match.");
92
            return false;
93
        }
94
95 1
        if (!$email || !$password || !$passwordAgain) {
96 1
            return false;
97
        }
98
99
        $user = new User();
100
        $user->setDb($this->di->get("db"));
101
        $user->email = $email;
102
        $user->setPassword($password);
103
        $user->created = date("Y-m-d H:i:s");
104
        $user->save();
105
106
        $this->form->addOutput("User was created.");
107
108
        return true;
109
    }
110
}
111