UserLoginForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 80
c 0
b 0
f 0
ccs 26
cts 26
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A callbackSubmit() 0 22 2
A callbackSuccess() 0 3 1
A __construct() 0 26 1
1
<?php
2
3
namespace Pamo\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pamo\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 Pamo\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 1
    public function __construct(ContainerInterface $di)
20
    {
21 1
        parent::__construct($di);
22
23 1
        $this->form->create(
24
            [
25 1
                "id" => __CLASS__,
26
                "legend" => "User Login"
27
            ],
28
            [
29 1
                "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 1
                    "type" => "submit",
43 1
                    "value" => "Login",
44 1
                    "callback" => [$this, "callbackSubmit"]
45
                ],
46
            ]
47
        );
48 1
    }
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 1
    public function callbackSubmit()
59
    {
60
        // Get values from the submitted form
61 1
        $username       = $this->form->value("username");
62 1
        $password      = $this->form->value("password");
63
64 1
        $user = new User();
65 1
        $user->setDb($this->di->get("dbqb"));
66 1
        $res = $user->verifyPassword($username, $password);
67
68 1
        if (!$res) {
69 1
            $this->form->rememberValues();
70 1
            $this->form->addOutput("User or password did not match.");
71 1
            return false;
72
        }
73
74 1
        $this->di->session->set("user", [
75 1
            "id" => $user->id,
76 1
            "username" => $user->username,
77 1
            "email" => $user->email
78
        ]);
79 1
        return true;
80
    }
81
82
83
84
    /**
85
     * Callback what to do if the form was successfully submitted, this
86
     * happen when the submit callback method returns true. This method
87
     * can/should be implemented by the subclass for a different behaviour.
88
     */
89 1
    public function callbackSuccess()
90
    {
91 1
        $this->di->get("response")->redirect("")->send();
92 1
    }
93
}
94