UpdateForm::getItemDetails()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Aiur18\Question\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Aiur18\Question\Question;
8
use Aiur18\getset\getset;
9
10
/**
11
 * Form to update an item.
12
 */
13
class UpdateForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container and the id to update.
17
     *
18
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Aiur18\Question\HTMLForm...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
19
     * @param integer             $id to update
20
     */
21
    public function __construct(ContainerInterface $di, $id)
22
    {
23
        parent::__construct($di);
24
        $question = $this->getItemDetails($id);
25
        $this->form->create(
26
            [
27
                "id" => __CLASS__,
28
                "legend" => "Update details of the item",
29
            ],
30
            [
31
                "id" => [
32
                    "type" => "text",
33
                    "validation" => ["not_empty"],
34
                    "readonly" => true,
35
                    "value" => $question->id,
36
                ],
37
38
                "column1" => [
39
                    "type" => "text",
40
                    "validation" => ["not_empty"],
41
                    "value" => $question->column1,
42
                ],
43
44
                "column2" => [
45
                    "type" => "text",
46
                    "validation" => ["not_empty"],
47
                    "value" => $question->column2,
48
                ],
49
50
                "submit" => [
51
                    "type" => "submit",
52
                    "value" => "Save",
53
                    "callback" => [$this, "callbackSubmit"]
54
                ],
55
56
                "reset" => [
57
                    "type"      => "reset",
58
                ],
59
            ]
60
        );
61
    }
62
63
64
65
    /** Method
66
     */
67
    public function getItemDetails($id) : object
68
    {
69
        $question = new Question();
70
        $question->setDb($this->di->get("dbqb"));
71
        $question->find("id", $id);
72
        return $question;
73
    }
74
75
76
77
    /** Method
78
     */
79
    public function callbackSubmit() : bool
80
    {
81
        $question = new Question();
82
        $question->setDb($this->di->get("dbqb"));
83
        $question->find("id", $this->form->value("id"));
84
        $question->column1 = $this->form->value("column1");
0 ignored issues
show
Bug introduced by
The property column1 does not seem to exist on Aiur18\Question\Question.
Loading history...
85
        $question->column2 = $this->form->value("column2");
0 ignored issues
show
Bug introduced by
The property column2 does not seem to exist on Aiur18\Question\Question.
Loading history...
86
        $question->save();
87
        return true;
88
    }
89
}
90