UpdateUserForm::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 29
cts 29
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 27
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Almrooth\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Almrooth\Comment\User;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class UpdateUserForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 1
    public function __construct(DIInterface $di, $id)
20
    {
21 1
        parent::__construct($di);
22 1
        $user = $this->getItemDetails($id);
23 1
        $this->form->create(
24
            [
25 1
                "id" => __CLASS__,
26 1
                "use_fieldset" => false,
27 1
            ],
28
            [
29
                "id" => [
30 1
                    "type" => "hidden",
31 1
                    "validation" => ["not_empty"],
32 1
                    "readonly" => true,
33 1
                    "value" => $user->id,
34 1
                ],
35
                "username" => [
36 1
                    "type" => "text",
37 1
                    "validation" => ["not_empty"],
38 1
                    "label" => "Användarnamn",
39 1
                    "readonly" => true,
40 1
                    "value" => $user->username,
41 1
                ],
42
                "email" => [
43 1
                    "type"          => "text",
44 1
                    "label"         => "Epost",
45 1
                    "validation"    => ["not_empty"],
46 1
                    "value"         => $user->email,
47 1
                ],
48
49
                "submit" => [
50 1
                    "type" => "submit",
51 1
                    "value" => "Uppdatera profil",
52 1
                    "callback" => [$this, "callbackSubmit"],
53
                    "class" => "btn"
54 1
                ],
55
            ]
56 1
        );
57 1
    }
58
59
60
    /**
61
     * Get details on item to load form with.
62
     *
63
     * @param integer $id get details on item with id.
64
     *
65
     * @return User true if okey, false if something went wrong.
66
     */
67 1
    public function getItemDetails($id)
68
    {
69 1
        $user = new User();
70 1
        $user->setDb($this->di->get("db"));
71 1
        $user->find("id", $id);
72 1
        return $user;
73
    }
74
75
76
    /**
77
     * Callback for submit-button which should return true if it could
78
     * carry out its work and false if something failed.
79
     *
80
     * @return boolean true if okey, false if something went wrong.
81
     */
82 View Code Duplication
    public function callbackSubmit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $user = new User();
85
        $user->setDb($this->di->get("db"));
86
        $user->find("id", $this->form->value("id"));
87
        $user->email   = $this->form->value("email");
88
        $user->save();
89
90
        // Redirect to profile page
91
        $this->di->get("response")->redirect("user/profile/" . $user->id);
92
    }
93
}
94