UserLoginForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
c 0
b 0
f 0
rs 9.344
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Forum\User\HTMLForm;
4
5
use Forum\User\User;
6
use Anax\HTMLForm\FormModel;
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class UserLoginForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
18
     */
19 View Code Duplication
    public function __construct(ContainerInterface $di)
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...
20
    {
21
        parent::__construct($di);
22
23
        $this->form->create(
24
            [
25
                "id" => __CLASS__,
26
                "legend" => "Logga in"
27
            ],
28
            [
29
                "user" => [
30
                    "type"        => "text",
31
                    //"description" => "Here you can place a description.",
32
                    //"placeholder" => "Here is a placeholder",
33
                ],
34
35
                "password" => [
36
                    "type"        => "password",
37
                    //"description" => "Here you can place a description.",
38
                    //"placeholder" => "Here is a placeholder",
39
                ],
40
41
                "submit" => [
42
                    "type" => "submit",
43
                    "value" => "Login",
44
                    "callback" => [$this, "callbackSubmit"]
45
                ],
46
47
                "submit2" => [
48
                    "type" => "submit",
49
                    "value" => "Logout",
50
                    "callback" => [$this, "logOut"]
51
                ],
52
            ]
53
        );
54
    }
55
56
57
    public function logOut() {
58
        session_destroy();
59
    }
60
61
    /**
62
     * Callback for submit-button which should return true if it could
63
     * carry out its work and false if something failed.
64
     *
65
     * @return boolean true if okey, false if something went wrong.
66
     */
67
        public function callbackSubmit()
68
    {
69
        // Get values from the submitted form
70
        $acronym       = $this->form->value("user");
71
        $password      = $this->form->value("password");
72
        $email         = $this->form->value("email");
0 ignored issues
show
Unused Code introduced by
$email is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73
74
        // Try to login
75
        // $db = $this->di->get("dbqb");
76
        // $db->connect();
77
        // $user = $db->select("password")
78
        //            ->from("User")
79
        //            ->where("acronym = ?")
80
        //            ->execute([$acronym])
81
        //            ->fetch();
82
        //
83
        // // $user is false if user is not found
84
        // if (!$user || !password_verify($password, $user->password)) {
85
        //    $this->form->rememberValues();
86
        //    $this->form->addOutput("User or password did not match.");
87
        //    return false;
88
        // }
89
90
        $user = new User();
91
        $user->setDb($this->di->get("dbqb"));
92
        $res = $user->verifyPassword($acronym, $password);
93
94
        if (!$res) {
95
            $this->form->rememberValues();
96
            $this->form->addOutput("User or password did not match.");
97
            return false;
98
        }
99
        $this->di->get("session")->set("user_id", $user->id);
100
        $this->form->addOutput("User " . $user->acronym . " logged in");
101
        return true;
102
    }
103
104
    /**
105
     * Callback what to do if the form was successfully submitted, this
106
     * happen when the submit callback method returns true. This method
107
     * can/should be implemented by the subclass for a different behaviour.
108
     */
109
    public function callbackSuccess()
110
    {
111
        $this->di->get("response")->redirect("forum")->send();
112
    }
113
}
114