UserLoginForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 32
ccs 0
cts 11
cp 0
crap 2
rs 9.7333
1
<?php
2
3
namespace Lyco\User\HTMLForm;
4
5
use Lyco\User\User;
6
use Anax\HTMLForm\FormModel;
7
use Psr\Container\ContainerInterface;
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 Lyco\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
    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
                "acronym" => [
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
                "create" => [
48
                    "type" => "submit",
49
                    "value" => "Create User",
50
                    "callback" => [$this, "callbackRegister"]
51
                ],
52
            ]
53
        );
54
    }
55
56
57
58
    /**
59
     * Callback for submit-button which should return true if it could
60
     * carry out its work and false if something failed.
61
     *
62
     * @return boolean true if okey, false if something went wrong.
63
     */
64
     public function callbackSubmit()
65
     {
66
         // Get values from the submitted form
67
         $acronym       = $this->form->value("acronym");
68
         $password      = $this->form->value("password");
69
70
         // Try to login
71
         $user = new User();
72
         $user->setDb($this->di->get("dbqb"));
73
         $res = $user->verifyPassword($acronym, $password);
74
75
         if (!$res) {
76
            $this->form->rememberValues();
77
            $this->form->addOutput("User or password did not match.");
78
            $this->di->get("session")->delete("acronym");
79
            return false;
80
         }
81
82
         $this->di->get("session")->set("userId", $user->acronym);
83
         $this->di->get("session")->set("userName", $acronym);
84
         return true;
85
     }
86
87
    /**
88
     * Redirect for create button
89
     */
90
    public function callbackRegister()
91
    {
92
        $this->di->get("response")->redirect("user/create");
93
    }
94
95
96
    public function callbackSuccess()
97
    {
98
        $this->di->get("response")->redirect("post")->send();
99
    }
100
}
101