UserLoginForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 66
ccs 9
cts 24
cp 0.375
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 1
A callbackSubmit() 0 22 2
1
<?php
2
3
namespace Seb\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Seb\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 Seb\User\HTMLForm\Psr\Container\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19 2
    public function __construct(ContainerInterface $di)
20
    {
21 2
        parent::__construct($di);
22
23 2
        $this->form->create(
24
            [
25 2
                "id" => __CLASS__,
26
                "legend" => "User Login"
27
            ],
28
            [
29
                "user" => [
30 2
                    "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 2
                    "type" => "submit",
43 2
                    "value" => "Login",
44 2
                    "callback" => [$this, "callbackSubmit"]
45
                ],
46
            ]
47
        );
48 2
    }
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
        $acronym       = $this->form->value("user");
60
        $password      = $this->form->value("password");
61
62
        $user = new User();
63
        $user->setDb($this->di->get("dbqb"));
64
        $res = $user->verifyPassword($acronym, $password);
65
66
        $session = $this->di->get("session");
67
        $session->set("acronym", $user->acronym);
68
        $session->set("userid", $user->id);
69
70
        if (!$res) {
71
            $this->form->rememberValues();
72
            $this->form->addOutput("User or password did not match.");
73
            return false;
74
        }
75
76
        $this->form->addOutput("User " . $user->acronym . " logged in.");
77
        return true;
78
    }
79
}
80