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

UpdateProfileSecurityForm::callbackSubmit()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 0
1
<?php
2
namespace Nicklas\Comment\HTMLForm\Profile;
3
4
use \Anax\HTMLForm\FormModel;
5
use \Anax\DI\DIInterface;
6
use \Nicklas\Comment\Modules\User;
7
/**
8
 * Example of FormModel implementation.
9
 */
10
class UpdateProfileSecurityForm extends FormModel
11
{
12
    /**
13
     * Constructor injects with DI container.
14
     *
15
     * @param Anax\DI\DIInterface $di a service container
16
     */
17
    public function __construct(DIInterface $di)
18
    {
19
        parent::__construct($di);
20
        $this->user = new User();
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
        $this->user->setDb($this->di->get("db"));
22
        $this->user = $this->user->find("name", $this->di->get("session")->get("user"));
23
        $this->form->create(
24
            [
25
                "id" => __CLASS__,
26
                "br-after-label" => false,
27
                "wrapper-element" => "div",
28
                "legend" => "Ändra ditt lösenord"
29
            ],
30
            [
31
                "answer" => [
32
                    "type"        => "text",
33
                    "value" => "{$this->user->question}",
34
                    "label" => "Kontrollfråga",
35
                    "validation" => ["not_empty"],
36
                ],
37
                "password" => [
38
                    "type"        => "password",
39
                    "label" => "Lösenord",
40
                ],
41
                "password-again" => [
42
                    "type"        => "password",
43
                    "label" => "Lösenord igen"
44
                ],
45
                "submit" => [
46
                    "type" => "submit",
47
                    "value" => "Ändra lösenord",
48
                    "callback" => [$this, "callbackSubmit"],
49
                ],
50
                "submit2" => [
51
                    "type" => "submit",
52
                    "value" => "Ändra kontrollfråga",
53
                    "callback" => [$this, "callbackQuestion"],
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
        $password      = $this->form->value("password");
68
        $passwordAgain = $this->form->value("password-again");
69
70
        // Check password matches
71
        if ($password == null) {
72
            $this->form->rememberValues();
73
            $this->form->addOutput("Du angav aldrig ett lösenord");
74
            return false;
75
        }
76
77
        // Check password matches
78
        if ($password !== $passwordAgain) {
79
            $this->form->rememberValues();
80
            $this->form->addOutput("Lösenordet matchade ej");
81
            return false;
82
        }
83
84
        $this->user->setPassword($password);
85
        $this->user->save();
86
        $this->form->addOutput("<p>Du ändrade ditt lösenord!</p> <p>TIPS:Innan du loggar ut, kontrollera så din kontrollfråga stämmer ifall du glömmer bort</p>");
87
        return true;
88
    }
89
90
    /**
91
     * Callback for submit-button which should return true if it could
92
     * carry out its work and false if something failed.
93
     *
94
     * @return boolean true if okey, false if something went wrong.
95
     */
96
    public function callbackQuestion()
97
    {
98
        // Get values from the submitted form
99
        $this->user->question = $this->form->value("answer");
100
        $this->form->addOutput("Du har ändrat svaret på din kontrollfråga till `{$this->user->question}`");
101
        $this->user->save();
102
        return true;
103
    }
104
}
105