UpdateForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 70.73%

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 103
ccs 29
cts 41
cp 0.7073
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A callbackSubmit() 0 13 1
A getProfileDetails() 0 6 1
A __construct() 0 56 1
1
<?php
2
3
namespace Seb\Profile\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Seb\Profile\Profile;
8
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Seb\Profile\HTMLForm\Psr...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     * @param integer             $id to update
19
     */
20 2
    public function __construct(ContainerInterface $di, $id)
21
    {
22 2
        parent::__construct($di);
23 2
        $profile = $this->getProfileDetails($id);
24 2
        $this->form->create(
25
            [
26 2
                "id" => __CLASS__,
27 2
                "legend" => "Update {$profile->acronym}",
28
            ],
29
            [
30
                "id" => [
31 2
                    "type" => "hidden",
32
                    "validation" => ["not_empty"],
33
                    "readonly" => true,
34 2
                    "value" => $profile->id,
35
                ],
36
37
                "acronym" => [
38 2
                    "type" => "hidden",
39
                    "validation" => ["not_empty"],
40
                    "readonly" => true,
41 2
                    "value" => $profile->acronym,
42
                ],
43
44
                "password" => [
45 2
                    "type" => "hidden",
46
                    "validation" => ["not_empty"],
47
                    "readonly" => true,
48 2
                    "value" => $profile->password,
49
                ],
50
51
                "country" => [
52 2
                    "type" => "text",
53 2
                    "value" => $profile->country,
54
                ],
55
56
                "city" => [
57 2
                    "type" => "text",
58 2
                    "value" => $profile->city,
59
                ],
60
61
                "email" => [
62 2
                    "type" => "text",
63 2
                    "value" => $profile->email,
64
                ],
65
66
                "score" => [
67 2
                    "type" => "hidden",
68
                    "readonly" => true,
69 2
                    "value" => $profile->score,
70
                ],
71
72
                "submit" => [
73 2
                    "type" => "submit",
74 2
                    "value" => "Save",
75 2
                    "callback" => [$this, "callbackSubmit"]
76
                ],
77
            ]
78
        );
79 2
    }
80
81
    /**
82
     * Get details on item to load form with.
83
     *
84
     * @param integer $id get details on item with id.
85
     *
86
     * @return Book
0 ignored issues
show
Bug introduced by
The type Seb\Profile\HTMLForm\Book was not found. Did you mean Book? If so, make sure to prefix the type with \.
Loading history...
87
     */
88 2
    public function getProfileDetails($id) : object
89
    {
90 2
        $profile = new Profile();
91 2
        $profile->setDb($this->di->get("dbqb"));
92 2
        $profile->find("id", $id);
93 2
        return $profile;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $profile returns the type Seb\Profile\Profile which is incompatible with the documented return type Seb\Profile\HTMLForm\Book.
Loading history...
94
    }
95
96
    /**
97
     * Callback for submit-button which should return true if it could
98
     * carry out its work and false if something failed.
99
     *
100
     * @return bool true if okey, false if something went wrong.
101
     */
102
    public function callbackSubmit() : bool
103
    {
104
        $profile = new Profile();
105
        $profile->setDb($this->di->get("dbqb"));
106
        $profile->find("id", $this->form->value("id"));
107
        $profile->acronym = $this->form->value("acronym");
108
        $profile->password = $this->form->value("password");
109
        $profile->country = $this->form->value("country");
110
        $profile->city = $this->form->value("city");
111
        $profile->email = $this->form->value("email");
112
        $profile->score = $this->form->value("score");
113
        $profile->save();
114
        return true;
115
    }
116
}
117