UserLoginForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 62
ccs 0
cts 22
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 1
A callbackSubmit() 0 20 2
1
<?php
2
3
namespace Aiur18\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Aiur18\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 Aiur18\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
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.",
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
        );
48
    }
49
50
51
52
    /** Method
53
     */
54
    public function callbackSubmit()
55
    {
56
        // Get values from the submitted form
57
        $acronym       = $this->form->value("user");
58
        $password      = $this->form->value("password");
59
60
        $user = new User();
61
        $user->setDb($this->di->get("dbqb"));
62
        $res = $user->verifyPassword($acronym, $password);
63
64
        if (!$res) {
65
            $this->form->rememberValues();
66
            $this->form->addOutput("User or password did not match.");
67
            return false;
68
        }
69
70
        $user->setSession($user->id);
71
72
        $this->form->addOutput("User " . $user->id . " logged in.");
73
        return true;
74
    }
75
}
76