UpdateProfileSecurityForm::callbackSubmit()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 16
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 0
crap 12
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
/**
9
 * Example of FormModel implementation.
10
 */
11
class UpdateProfileSecurityForm extends FormModel
12
{
13
    /**
14
     * Constructor injects with DI container.
15
     *
16
     * @param Anax\DI\DIInterface $di a service container
17
     */
18
    public function __construct(DIInterface $di)
19
    {
20
        parent::__construct($di);
21
        $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...
22
        $this->user->setDb($this->di->get("db"));
23
        $this->user = $this->user->find("name", $this->di->get("session")->get("user"));
24
        $this->form->create(
25
            [
26
                "id" => __CLASS__,
27
                "br-after-label" => false,
28
                "wrapper-element" => "div",
29
                "legend" => "Ändra ditt lösenord"
30
            ],
31
            [
32
                "answer" => [
33
                    "type"        => "text",
34
                    "value" => "{$this->user->question}",
35
                    "label" => "Kontrollfråga",
36
                    "validation" => ["not_empty"],
37
                ],
38
                "password" => [
39
                    "type"        => "password",
40
                    "label" => "Lösenord",
41
                ],
42
                "password-again" => [
43
                    "type"        => "password",
44
                    "label" => "Lösenord igen"
45
                ],
46
                "submit" => [
47
                    "type" => "submit",
48
                    "value" => "Ändra lösenord",
49
                    "callback" => [$this, "callbackSubmit"],
50
                ],
51
                "submit2" => [
52
                    "type" => "submit",
53
                    "value" => "Ändra kontrollfråga",
54
                    "callback" => [$this, "callbackQuestion"],
55
                ],
56
            ]
57
        );
58
    }
59
    /**
60
     * Callback for submit-button which should return true if it could
61
     * carry out its work and false if something failed.
62
     *
63
     * @return boolean true if okey, false if something went wrong.
64
     */
65
    public function callbackSubmit()
66
    {
67
        // Get values from the submitted form
68
        $password      = $this->form->value("password");
69
        $passwordAgain = $this->form->value("password-again");
70
71
        // Check password matches
72
        if ($password == null) {
73
            $this->form->rememberValues();
74
            $this->form->addOutput("Du angav aldrig ett lösenord");
75
            return false;
76
        }
77
78
        // Check password matches
79
        if ($password !== $passwordAgain) {
80
            $this->form->rememberValues();
81
            $this->form->addOutput("Lösenordet matchade ej");
82
            return false;
83
        }
84
85
        $this->user->setPassword($password);
86
        $this->user->save();
87
        $this->form->addOutput("<p>Du ändrade ditt lösenord!</p>
88
        <p>TIPS:Innan du loggar ut, kontrollera så din kontrollfråga stämmer ifall du glömmer bort</p>");
89
        return true;
90
    }
91
92
    /**
93
     * Callback for submit-button which should return true if it could
94
     * carry out its work and false if something failed.
95
     *
96
     * @return boolean true if okey, false if something went wrong.
97
     */
98
    public function callbackQuestion()
99
    {
100
        // Get values from the submitted form
101
        $this->user->question = $this->form->value("answer");
102
        $this->form->addOutput("Du har ändrat svaret på din kontrollfråga till `{$this->user->question}`");
103
        $this->user->save();
104
        return true;
105
    }
106
}
107