Test Failed
Push — master ( 48da91...4d2acf )
by Nicklas
01:59
created

UserResetForm::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 1
1
<?php
2
namespace Nicklas\Comment\HTMLForm\User;
3
use \Anax\HTMLForm\FormModel;
4
use \Anax\DI\DIInterface;
5
use \Nicklas\Comment\Modules\User;
6
/**
7
 * Example of FormModel implementation.
8
 */
9
class UserResetForm extends FormModel
10
{
11
    /**
12
     * Constructor injects with DI container.
13
     *
14
     * @param Anax\DI\DIInterface $di a service container
15
     */
16
    public function __construct(DIInterface $di)
17
    {
18
        parent::__construct($di);
19
        $this->form->create(
20
            [
21
                "id" => __CLASS__,
22
                "br-after-label" => false,
23
                "use_fieldset" => false,
24
                "wrapper-element" => "div",
25
            ],
26
            [
27
                "user" => [
28
                    "type"        => "text",
29
                    //"description" => "Here you can place a description.",
30
                    "placeholder" => "Användarnamn",
31
                    "validation" => ["not_empty"],
32
                    "label" => false,
33
                ],
34
                "answer" => [
35
                    "type"        => "text",
36
                    "description" => "Vad är din favorträtt?",
37
                    "placeholder" => "Kontrollfråga",
38
                    "validation" => ["not_empty"],
39
                    "label" => false,
40
                ],
41
                "submit" => [
42
                    "type" => "submit",
43
                    "value" => "Svara",
44
                    "callback" => [$this, "callbackSubmit"]
45
                ],
46
            ]
47
        );
48
    }
49
    /**
50
     * Callback for submit-button which should return true if it could
51
     * carry out its work and false if something failed.
52
     *
53
     * @return boolean true if okey, false if something went wrong.
54
     */
55
    public function callbackSubmit()
56
    {
57
        // Get values from the submitted form
58
        $name       = $this->form->value("user");
59
        $answer      = $this->form->value("answer");
60
        $user = new User();
61
        $user->setDb($this->di->get("db"));
62
        $res = $user->verifyQuestion($name, $answer);
63
        if ($res) {
64
            $this->form->rememberValues();
65
            $random = substr(md5(mt_rand()), 0, 7);
66
            $this->form->addOutput("Rätt! Ditt lösenord har ändrats till <b>'{$random}'</b>. Ange det när du loggar in och byt sedan till valfritt i din profilsida.");
67
            $user->setPassword($random);
68
            $user->save();
69
            return true;
70
        }
71
        $this->form->rememberValues();
72
        $this->form->addOutput("Du hade fel, kontrollera så du inte skriver med stora bokstäver");
73
        return false;
74
    }
75
}
76