UserLoginForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
crap 2.0054
1
<?php
2
3
namespace Peto16\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
8
/**
9
 * Example of FormModel implementation.
10
 */
11
class UserLoginForm extends FormModel
12
{
13
    /**
14
     * Constructor injects with DI container.
15
     *
16
     * @param Anax\DI\DIInterface $di a service container
17
     */
18 3
    public function __construct(DIInterface $di)
19
    {
20 3
        parent::__construct($di);
21
22 3
        $this->form->create(
23
            [
24 3
                "id" => __CLASS__,
25
                "legend" => "Logga In"
26 3
            ],
27
            [
28
                "username" => [
29 3
                    "type"        => "text",
30
                    // "description" => "Here you can place a description.",
31
                    // "placeholder" => "Here is a placeholder",
32 3
                ],
33
34
                "password" => [
35 3
                    "type"        => "password",
36
                    //"description" => "Here you can place a description.",
37
                    //"placeholder" => "Here is a placeholder",
38 3
                ],
39
40
                "submit" => [
41 3
                    "type" => "submit",
42 3
                    "value" => "Login",
43 3
                    "callback" => [$this, "callbackSubmit"]
44 3
                ],
45
            ]
46 3
        );
47 3
    }
48
49
50
51
    /**
52
     * Callback for submit-button which should return true if it could
53
     * carry out its work and false if something failed.
54
     *
55
     * @return boolean true if okey, false if something went wrong.
56
     */
57 1
    public function callbackSubmit()
58
    {
59
        // Remember values during resubmit, useful when failing (retunr false)
60
        // and asking the user to resubmit the form.
61 1
        $this->form->rememberValues();
62 1
        $username = $this->form->value("username");
63 1
        $password = $this->form->value("password");
64
        try {
65 1
            $this->di->get("userService")->login($username, $password);
66 1
        } catch (\Peto16\User\Exception $e) {
67 1
            $this->form->addOutput($e->getMessage());
68 1
            return false;
69
        }
70
        return true;
71
    }
72
}
73