UserLoginForm::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
ccs 0
cts 9
cp 0
rs 8.8571
cc 1
eloc 13
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Schanihbg\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Schanihbg\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 Anax\DI\DIInterface $di a service container
18
     */
19
    public function __construct(DIInterface $di)
20
    {
21
        parent::__construct($di);
22
23
        $this->form->create(
24
            [
25
                "id" => __CLASS__,
26
                "legend" => "User Login"
27
            ],
28
            [
29
                "user" => [
30
                    "type"        => "text",
31
                    //"description" => "Here you can place a description.",
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
32
                    //"placeholder" => "Here is a placeholder",
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
                ],
34
                        
35
                "password" => [
36
                    "type"        => "password",
37
                    //"description" => "Here you can place a description.",
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
                    //"placeholder" => "Here is a placeholder",
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
                ],
40
41
                "submit" => [
42
                    "type" => "submit",
43
                    "value" => "Login",
44
                    "callback" => [$this, "callbackSubmit"]
45
                ],
46
            ]
47
        );
48
    }
49
50
51
52
    /**
53
     * Callback for submit-button which should return true if it could
54
     * carry out its work and false if something failed.
55
     *
56
     * @return boolean true if okey, false if something went wrong.
57
     */
58
    public function callbackSubmit()
59
    {
60
        // Get values from the submitted form
61
        $acronym       = $this->form->value("user");
62
        $password      = $this->form->value("password");
63
64
        $user = new User();
65
        $user->setDb($this->di->get("database"));
66
        $res = $user->verifyPassword($acronym, $password);
67
68
        if (!$res) {
69
            $this->form->rememberValues();
70
            $this->form->addOutput("User or password did not match.");
71
            return false;
72
        }
73
74
        $this->di->get("userController")->loginUser($acronym);
75
        $this->di->get("response")->redirect("user");
76
        return true;
77
    }
78
}
79