DeleteUserForm::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
ccs 27
cts 27
cp 1
rs 8.8571
cc 1
eloc 24
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Peto16\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
8
/**
9
 * Form to update an item.
10
 */
11
class DeleteUserForm extends FormModel
12
{
13
    /**
14
     * Constructor injects with DI container and the id to update.
15
     *
16
     * @param Anax\DI\DIInterface $di a service container
17
     * @param integer             $id to update
18
     */
19 2
    public function __construct(DIInterface $di, $id)
20
    {
21 2
        parent::__construct($di);
22 2
        $user = $di->get("userService")->getUserByField("id", $id);
23 2
        $this->form->create(
24
            [
25 2
                "id" => __CLASS__,
26 2
                "legend" => "Radera användare.",
27 2
            ],
28
            [
29
                "id" => [
30 2
                    "type" => "text",
31 2
                    "validation" => ["not_empty"],
32 2
                    "readonly" => true,
33 2
                    "value" => $user->id,
34 2
                ],
35
36
                "username" => [
37 2
                    "label" => "Användarnamn",
38 2
                    "readonly" => true,
39 2
                    "type" => "text",
40 2
                    "value" => htmlentities($user->username),
41 2
                ],
42
                "confirm" => [
43 2
                    "required"    => true,
44 2
                    "label"       => "Bekräfta",
45 2
                    "type"        => "checkbox",
46 2
                ],
47
48
                "submit" => [
49 2
                    "type" => "submit",
50 2
                    "value" => "Radera",
51 2
                    "callback" => [$this, "callbackSubmit"]
52 2
                ],
53
            ]
54 2
        );
55 2
    }
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 1
    public function callbackSubmit()
66
    {
67 1
        $id = $this->form->value("id");
68 1
        $this->di->get("userService")->deleteUser($id);
69 1
        $this->di->get("utils")->redirect("admin");
70 1
    }
71
}
72