UserLoginForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
nc 1
nop 1
dl 0
loc 26
c 1
b 0
f 0
cc 1
rs 9.8666
1
<?php
2
3
namespace Pan\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pan\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 Pan\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
    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
                "username" => [
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
    /**
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
        $username       = $this->form->value("username");
62
        $password      = $this->form->value("password");
63
64
        // Try to login
65
        $user = new User();
66
        $user->setDb($this->di->get("dbqb"));
67
        $res = $user->verifyPassword($username, $password);
68
69
        if (!$res) {
70
            $this->form->rememberValues();
71
            $this->form->addOutput("User or password did not match.");
72
            return false;
73
        }
74
        $session = $this->di->get("session");
75
        $session->set("username", $username);
76
        
77
        return true;
78
    }
79
80
    /**
81
     * Callback what to do if the form was successfully submitted, this
82
     * happen when the submit callback method returns true. This method
83
     * can/should be implemented by the subclass for a different behaviour.
84
     */
85
    public function callbackSuccess()
86
    {
87
        $this->di->get("response")->redirect("user")->send();
88
    }
89
}
90