UpdateQuestionForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 108
c 0
b 0
f 0
ccs 37
cts 37
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A callbackSubmit() 0 9 1
A getItemDetails() 0 6 1
A __construct() 0 49 1
A callbackSuccess() 0 3 1
1
<?php
2
3
namespace Pamo\Question\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pamo\Question\Question;
8
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateQuestionForm 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 Pamo\Question\HTMLForm\P...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
        $this->questionid = $id;
0 ignored issues
show
Bug Best Practice introduced by
The property questionid does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24 2
        $question = $this->getItemDetails($id);
25
26 2
        $this->form->create(
27
            [
28 2
                "id" => __CLASS__,
29
                "legend" => "Update details of the item",
30
            ],
31
            [
32 2
                "id" => [
33 2
                    "type" => "hidden",
34
                    "validation" => ["not_empty"],
35
                    "readonly" => true,
36 2
                    "value" => $question->id,
37
                ],
38
39
                "user" => [
40 2
                    "type" => "hidden",
41
                    "validation" => ["not_empty"],
42 2
                    "value" => $question->user,
43
                ],
44
45
                "title" => [
46 2
                    "type" => "text",
47
                    "label" => false,
48
                    "validation" => ["not_empty"],
49 2
                    "value" => $question->title,
50
                ],
51
52
                "text" => [
53 2
                    "type" => "textarea",
54
                    "validation" => ["not_empty"],
55
                    "label" => false,
56 2
                    "value" => $question->text,
57
                ],
58
59
                "submit" => [
60 2
                    "type" => "submit",
61 2
                    "value" => "Save",
62 2
                    "callback" => [$this, "callbackSubmit"],
63
                ],
64
65
                "cancel" => [
66 2
                    "type" => "button",
67 2
                    "value" => "Cancel",
68 2
                    "onclick" => "location.href='$this->questionid';"
69
                ],
70
            ]
71
        );
72 2
    }
73
74
75
76
    /**
77
     * Get details on item to load form with.
78
     *
79
     * @param integer $id get details on item with id.
80
     *
81
     * @return Question
82
     */
83 2
    public function getItemDetails($id) : object
84
    {
85 2
        $question = new Question();
86 2
        $question->setDb($this->di->get("dbqb"));
87 2
        $question->find("id", $id);
88 2
        return $question;
89
    }
90
91
92
93
    /**
94
     * Callback for submit-button which should return true if it could
95
     * carry out its work and false if something failed.
96
     *
97
     * @return bool true if okey, false if something went wrong.
98
     */
99 1
    public function callbackSubmit() : bool
100
    {
101 1
        $question = new Question();
102 1
        $question->setDb($this->di->get("dbqb"));
103 1
        $question->find("id", $this->form->value("id"));
104 1
        $question->title = $this->form->value("title");
105 1
        $question->text = $this->form->value("text");
106 1
        $question->save();
107 1
        return true;
108
    }
109
110
111
112
    /**
113
     * Callback what to do if the form was successfully submitted, this
114
     * happen when the submit callback method returns true. This method
115
     * can/should be implemented by the subclass for a different behaviour.
116
     */
117 1
    public function callbackSuccess()
118
    {
119 1
        $this->di->get("response")->redirect("game/question/$this->questionid")->send();
120 1
    }
121
122
123
124
    // /**
125
    //  * Callback what to do if the form was unsuccessfully submitted, this
126
    //  * happen when the submit callback method returns false or if validation
127
    //  * fails. This method can/should be implemented by the subclass for a
128
    //  * different behaviour.
129
    //  */
130
    // public function callbackFail()
131
    // {
132
    //     $this->di->get("response")->redirectSelf()->send();
133
    // }
134
}
135