CreateUserForm::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 3.0341

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 27
cts 32
cp 0.8438
rs 9.3333
c 0
b 0
f 0
cc 3
eloc 28
nc 3
nop 1
crap 3.0341
1
<?php
2
3
namespace Anax\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Anax\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 1
    public function __construct(DIInterface $di)
20
    {
21 1
        parent::__construct($di);
22
23 1
        $editable = "hidden";
24 1
        $acronym = $this->di->session->get("user");
25
26 1
        if (isset($acronym)) {
27
            $role = $this->di->get("commentController")->getRole($acronym);
28
            if ($role == 10) {
29
                $editable = "text";
30
            }
31
        }
32
33 1
        $this->form->create(
34
            [
35 1
                "id" => __CLASS__,
36 1
                "legend" => "Create user",
37 1
            ],
38
            [
39
                "acronym" => [
40 1
                    "type"        => "text",
41 1
                ],
42
43
                "password" => [
44 1
                    "type"        => "password",
45 1
                ],
46
47
                "password-again" => [
48 1
                    "type"        => "password",
49
                    "validation" => [
50
                        "match" => "password"
51 1
                    ],
52 1
                ],
53
                "email" => [
54 1
                    "type"        => "text",
55 1
                ],
56
                "role" => [
57 1
                    "type"        => $editable,
58 1
                    "value"       => 1,
59 1
                ],
60
61
                "submit" => [
62 1
                    "type" => "submit",
63 1
                    "value" => "Create user",
64 1
                    "callback" => [$this, "callbackSubmit"]
65 1
                ],
66
            ]
67 1
        );
68 1
    }
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 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...
78
    {
79
    // Get values from the submitted form
80
81
        $acronym       = $this->form->value("acronym");
82
        $password      = $this->form->value("password");
83
        $passwordAgain = $this->form->value("password-again");
84
        $email         = $this->form->value("email");
85
        $role          = $this->form->value("role");
86
87
        // Check password matches
88
        if ($password !== $passwordAgain) {
89
            $this->form->rememberValues();
90
            $this->form->addOutput("Password did not match.");
91
            return false;
92
        }
93
94
        // Save to database
95
        // $db = $this->di->get("db");
96
        // $password = password_hash($password, PASSWORD_DEFAULT);
97
        // $db->connect()
98
        //    ->insert("User", ["acronym", "password"])
99
        //    ->execute([$acronym, $password]);
100
        $user = new User();
101
        $user->setDb($this->di->get("db"));
102
        $user->acronym = $acronym;
103
        $user->email = $email;
104
        $user->role = $role;
0 ignored issues
show
Bug introduced by
The property role does not seem to exist in Anax\User\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
        // create gravatar from email
106
        $gravatar = $user->gravatar($email);
107
        $user->gravatar = $gravatar;
108
109
        $user->setPassword($password);
110
        $user->save();
111
112
        $this->form->addOutput("User was created.");
113
        return true;
114
    }
115
}
116