UserLoginForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.0491

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 13
cp 0.7692
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 0
crap 2.0491
1
<?php
2
3
namespace Nicklas\Comment\HTMLForm\User;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Nicklas\Comment\Modules\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 Anax\DI\DIInterface $di a service container
18
     */
19 2
    public function __construct(DIInterface $di)
20
    {
21 2
        parent::__construct($di);
22
23 2
        $this->form->create(
24
            [
25 2
                "id" => __CLASS__,
26 2
                "br-after-label" => false,
27 2
                "use_fieldset" => false,
28 2
            ],
29
            [
30
                "user" => [
31 2
                    "type"        => "text",
32
                    //"description" => "Here you can place a description.",
33 2
                    "placeholder" => "Användarnamn",
34 2
                    "validation" => ["not_empty"],
35
                    "label" => false
36 2
                ],
37
38
                "password" => [
39 2
                    "type"        => "password",
40
                    //"description" => "Here you can place a description.",
41 2
                    "placeholder" => "Lösenord",
42 2
                    "validation" => ["not_empty"],
43
                    "label" => false
44 2
                ],
45
46
                "submit" => [
47 2
                    "type" => "submit",
48 2
                    "value" => "Login",
49 2
                    "callback" => [$this, "callbackSubmit"]
50 2
                ],
51
            ]
52 2
        );
53 2
    }
54
55
56
57
    /**
58
     * Callback for submit-button which should return true if it could
59
     * carry out its work and false if something failed.
60
     *
61
     * @return boolean true if okey, false if something went wrong.
62
     */
63 1
    public function callbackSubmit()
64
    {
65
        // Get values from the submitted form
66 1
        $name       = $this->form->value("user");
67 1
        $password      = $this->form->value("password");
68
69 1
        $user = new User();
70 1
        $user->setDb($this->di->get("db"));
71 1
        $res = $user->verifyPassword($name, $password);
72
73 1
        if (!$res) {
74 1
            $this->form->rememberValues();
75 1
            $this->form->addOutput("User or password did not match.");
76 1
            return false;
77
        }
78
79
        $this->di->get('session')->set("user", $name); # set user in session
80
        $this->di->get("response")->redirect("user/profile");
81
    }
82
}
83