CreateUserForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 6.19 %

Test Coverage

Coverage 59.46%

Importance

Changes 0
Metric Value
dl 6
loc 97
ccs 22
cts 37
cp 0.5946
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B callbackSubmit() 0 28 5
A __construct() 6 49 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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