UpdateProfileForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Radchasay\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\User\User;
8
9
/**
10
 * Form to update an item.
11
 */
12 View Code Duplication
class UpdateProfileForm extends FormModel
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     * @param integer             $id to update
19
     */
20
    public function __construct(DIInterface $di, $id)
21
    {
22
        parent::__construct($di);
23
        $profileInfo = $this->getItemDetails($id);
24
        $this->form->create(
25
            [
26
                "id"     => __CLASS__,
27
                "legend" => "Update details of the item",
28
            ],
29
            [
30
                "id"    => [
31
                    "type"     => "hidden",
32
                    "value"    => $profileInfo->id,
33
                ],
34
                "email" => [
35
                    "type"       => "text",
36
                    "validation" => ["not_empty"],
37
                    "value"      => $profileInfo->email,
38
                ],
39
40
                "name" => [
41
                    "type"       => "text",
42
                    "validation" => ["not_empty"],
43
                    "value"      => $profileInfo->name,
44
                ],
45
46
                "age" => [
47
                    "type"       => "number",
48
                    "validation" => ["not_empty"],
49
                    "value"      => $profileInfo->age,
50
                ],
51
52
                "submit" => [
53
                    "type"     => "submit",
54
                    "value"    => "Save",
55
                    "callback" => [$this, "callbackSubmit"],
56
                ],
57
58
                "reset" => [
59
                    "type" => "reset",
60
                ],
61
            ]
62
        );
63
    }
64
65
66
    /**
67
     * Get details on item to load form with.
68
     *
69
     * @param integer $id get details on item with id.
70
     *
71
     * @return $user
0 ignored issues
show
Documentation introduced by
The doc-type $user could not be parsed: Unknown type name "$user" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
72
     */
73
    public function getItemDetails($id)
74
    {
75
        $user = new User();
76
        $user->setDb($this->di->get("db"));
77
        $user->find("id", $id);
78
        return $user;
79
    }
80
81
82
    /**
83
     * Callback for submit-button which should return true if it could
84
     * carry out its work and false if something failed.
85
     *
86
     * @return boolean true if okey, false if something went wrong.
87
     */
88
    public function callbackSubmit()
89
    {
90
        $user = new User();
91
        $user->setDb($this->di->get("db"));
92
        $user->find("id", $this->form->value("id"));
93
        $user->email = htmlentities($this->form->value("email"));
94
        $user->name = htmlentities($this->form->value("name"));
95
        $user->age = htmlentities($this->form->value("age"));
96
        $user->save();
97
        $this->di->get("session")->set("email", $user->email);
98
        $url = $this->di->get("url")->create("user/login");
99
        $this->di->get("response")->redirect($url);
100
        //$this->di->get("response")->redirect($url . "/update/{$book->id}");
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
    }
102
}
103