CreateUserForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 93
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 38 1
A callbackSubmit() 0 35 2
1
<?php
2
3
namespace Alfs18\User\HTMLForm;
4
5
use Alfs18\User\User;
6
use Anax\HTMLForm\FormModel;
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class CreateUserForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Alfs18\User\HTMLForm\Psr...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct(ContainerInterface $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
                "password" => [
33
                    "type"  => "password",
34
                ],
35
36
                "password-again" => [
37
                    "type"  => "password",
38
                    "validation" => [
39
                        "match" => "password"
40
                    ]
41
                ],
42
43
                "points" => [
44
                    "type"  => "hidden",
45
                    "value" => 0,
46
                ],
47
48
                "created" => [
49
                    "type"  => "hidden",
50
                    "value" => date("d M Y, H:i"),
51
                ],
52
53
                "submit" => [
54
                    "type" => "submit",
55
                    "value" => "Create user",
56
                    "callback" => [$this, "callbackSubmit"]
57
                ],
58
            ]
59
        );
60
    }
61
62
63
64
    /**
65
     * Callback for submit-button which should return true if it could
66
     * carry out its work and false if something failed.
67
     *
68
     * @return boolean true if okey, false if something went wrong.
69
     */
70
    public function callbackSubmit()
71
    {
72
        $user = new User();
73
        // Get values from the submitted form
74
        $acronym = $this->form->value("acronym");
75
        $password = $user->changeCharacter($this->form->value("password"));
76
        $passwordAgain = $user->changeCharacter($this->form->value("password-again"));
77
        $points = $this->form->value("points");
78
        $created = $this->form->value("created");
79
80
        // Check password matches
81
        if ($password !== $passwordAgain) {
82
            $this->form->rememberValues();
83
            $this->form->addOutput("Password dig not match.");
84
            return false;
85
        }
86
87
        // Save to database
88
        // $db = $this->di->get("dbqb");
89
        // $password = password_hash($password, PASSWORD_DEFAULT);
90
        // $db->connect()
91
        //     ->insert("User", ["acronym", "password"])
92
        //     ->execute([$acronym, $password]);
93
        $user = new User();
94
        $user->setDb($this->di->get("dbqb"));
95
        $user->acronym = $user->changeCharacter($acronym);
96
        $password = $user->changeCharacter($password);
0 ignored issues
show
Bug introduced by
$password of type array is incompatible with the type string expected by parameter $text of Alfs18\User\User::changeCharacter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $password = $user->changeCharacter(/** @scrutinizer ignore-type */ $password);
Loading history...
97
        $user->setPassword($password);
0 ignored issues
show
Bug introduced by
$password of type array|string[] is incompatible with the type string expected by parameter $password of Alfs18\User\User::setPassword(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $user->setPassword(/** @scrutinizer ignore-type */ $password);
Loading history...
98
        $user->setPicture($this->di);
99
        $user->points = intval($points);
100
        $user->created = $created;
101
        $user->saveUser($this->di);
102
103
        $this->form->addOutput("User was created.");
104
        return true;
105
    }
106
}
107