UserLoginForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 35
ccs 0
cts 14
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Anax\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Anax\User\User;
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
0 ignored issues
show
Bug introduced by
The type Anax\User\HTMLForm\Psr\C...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" => "Login"
26
                ],
27
                [
28
                "username" => [
29
                    "type"        => "text",
30
                    //"description" => "Here you can place a description.",
31
                    "placeholder" => "Username",
32
                ],
33
34
                "password" => [
35
                    "type"        => "password",
36
                    //"description" => "Here you can place a description.",
37
                    "placeholder" => "Password",
38
                ],
39
                "submit" => [
40
                    "type" => "submit",
41
                    "value" => "Login",
42
                    "callback" => [$this, "callbackSubmit"]
43
                ],
44
                ]
45
            );
46
    }
47
48
49
50
    /**
51
     * Callback for submit-button which should return true if it could
52
     * carry out its work and false if something failed.
53
     *
54
     * @return boolean true if okey, false if something went wrong.
55
     */
56
    public function callbackSubmit()
57
    {
58
        // Get values from the submitted form
59
        $username       = $this->form->value("username");
60
        $password      = $this->form->value("password");
61
62
        // Try to login
63
        // $db = $this->di->get("dbqb");
64
        // $db->connect();
65
        // $user = $db->select("password")
66
        //            ->from("User")
67
        //            ->where("acronym = ?")
68
        //            ->execute([$acronym])
69
        //            ->fetch();
70
        //
71
        // // $user is false if user is not found
72
        // if (!$user || !password_verify($password, $user->password)) {
73
        //    $this->form->rememberValues();
74
        //    $this->form->addOutput("User or password did not match.");
75
        //    return false;
76
        // }
77
78
        $user = new User();
79
        $user->setDb($this->di->get("dbqb"));
80
        $res = $user->verifyPassword($username, $password);
81
        $session = $this->di->get("session");
82
        $session->set("login", $user->id);
83
84
        if (!$res) {
85
            $this->form->rememberValues();
86
            $this->form->addOutput("User or password did not match.");
87
            return false;
88
        }
89
        $this->form->addOutput("User " . $user->username . " logged in.");
90
        return true;
91
    }
92
93
    // public function callbackLogout()
94
    // {
95
    //     $session = $this->di->get("session");
96
    //     $session->destroy();
97
    //     $this->di->get("response")->redirect("login");
98
    // }
99
}
100