EditUserForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 45
ccs 0
cts 16
cp 0
rs 9.456
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Anax\User\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Anax\User\User;
8
9
class EditUserForm extends FormModel
10
{
11
    /**
12
     * Constructor injects with DI container.
13
     *
14
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Anax\User\HTMLForm\Psr\C...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
15
     * @param integer             $id to update
16
     */
17
    public function __construct(ContainerInterface $di, $id)
18
    {
19
        parent::__construct($di);
20
        $user = $this->getItemDetails($id);
21
        $this->form->create(
22
            [
23
                "id" => __CLASS__,
24
                "legend" => "Edit exsiting user",
25
            ],
26
            [
27
                "id" => [
28
                    "type"        => "text",
29
                    "validation" => ["not_empty"],
30
                    "readonly" => "readonly",
31
                    "value" => $user->id,
32
                ],
33
34
                "username" => [
35
                    "type"        => "text",
36
                    "validation" => ["not_empty"],
37
                    "value" => $user->username,
38
                ],
39
40
                "password" => [
41
                    "type"        => "password",
42
                    "validation" => ["not_empty"],
43
                ],
44
45
                "password-again" => [
46
                    "type"        => "password",
47
                    "validation" => [
48
                        "match" => "password"
49
                    ],
50
                ],
51
52
                "email" => [
53
                    "type"        => "email",
54
                    "validation" => ["not_empty"],
55
                    "value" => $user->email,
56
                ],
57
58
                "submit" => [
59
                    "type" => "submit",
60
                    "value" => "Create user",
61
                    "callback" => [$this, "callbackSubmit"]
62
                ],
63
            ]
64
        );
65
    }
66
67
    /**
68
     * Get details on item to load form with.
69
     *
70
     * @param integer $id get details on item with id.
71
     *
72
     * @return Book
0 ignored issues
show
Bug introduced by
The type Anax\User\HTMLForm\Book was not found. Did you mean Book? If so, make sure to prefix the type with \.
Loading history...
73
     */
74
    public function getItemDetails($id) : object
75
    {
76
        $user = new User();
77
        $user->setDb($this->di->get("dbqb"));
78
        $user->find("id", $id);
79
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type Anax\User\User which is incompatible with the documented return type Anax\User\HTMLForm\Book.
Loading history...
80
    }
81
82
83
    /**
84
     * Callback for submit-button which should return true if it could
85
     * carry out its work and false if something failed.
86
     *
87
     * @return boolean true if okey, false if something went wrong.
88
     */
89
    public function callbackSubmit()
90
    {
91
        // Get values from the submitted form
92
        $user = new User();
93
        $user->setDb($this->di->get("dbqb"));
94
        $username      = $this->form->value("username");
95
        $password      = $this->form->value("password");
96
        $passwordAgain = $this->form->value("password-again");
97
        $email = $this->form->value("email");
98
99
        // Check password matches
100
        if ($password !== $passwordAgain) {
101
            $this->form->rememberValues();
102
            $this->form->addOutput("Password did not match.");
103
            return false;
104
        }
105
        $user->find("id", $this->form->value("id"));
106
        $user->username  = $username;
107
        $user->password = $this->form->value("password");
108
        $user->email = $email;
0 ignored issues
show
Bug introduced by
The property email does not seem to exist on Anax\User\User.
Loading history...
109
        $user->save();
110
        $this->form->addOutput("User was updated.");
111
        return true;
112
    }
113
}
114