UpdateAnswerForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 26
nc 1
nop 2
dl 0
loc 40
c 0
b 0
f 0
cc 1
ccs 19
cts 19
cp 1
crap 1
rs 9.504
1
<?php
2
3
namespace Pamo\Answer\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pamo\Answer\Answer;
8
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateAnswerForm 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\Answer\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 1
    public function __construct(ContainerInterface $di, $id)
21
    {
22 1
        parent::__construct($di);
23 1
        $answer = $this->getItemDetails($id);
24 1
        $this->questionid = $answer->questionid;
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...
25
26 1
        $this->form->create(
27
            [
28 1
                "id" => __CLASS__,
29
                "legend" => "Update details of the item",
30
            ],
31
            [
32 1
                "id" => [
33 1
                    "type" => "hidden",
34
                    "readonly" => true,
35 1
                    "value" => $answer->id,
36
                ],
37
38
                "questionid" => [
39 1
                    "type" => "hidden",
40
                    "validation" => ["not_empty"],
41 1
                    "value" => $answer->questionid,
42
                ],
43
44
                "text" => [
45 1
                    "type" => "textarea",
46
                    "label" => false,
47 1
                    "value" => $answer->text,
48
                ],
49
50
                "submit" => [
51 1
                    "type" => "submit",
52 1
                    "value" => "Save",
53 1
                    "callback" => [$this, "callbackSubmit"]
54
                ],
55
56
                "cancel" => [
57 1
                    "type" => "button",
58 1
                    "value" => "Cancel",
59 1
                    "onclick" => "location.href='$this->questionid';"
60
                ],
61
            ]
62
        );
63 1
    }
64
65
66
67
    /**
68
     * Get details on item to load form with.
69
     *
70
     * @param integer $id get details on item with id.
71
     *
72
     * @return Answer
73
     */
74 1
    public function getItemDetails($id) : object
75
    {
76 1
        $answer = new Answer();
77 1
        $answer->setDb($this->di->get("dbqb"));
78 1
        $answer->find("id", $id);
79 1
        return $answer;
80
    }
81
82
83
84
    /**
85
     * Callback for submit-button which should return true if it could
86
     * carry out its work and false if something failed.
87
     *
88
     * @return bool true if okey, false if something went wrong.
89
     */
90 1
    public function callbackSubmit() : bool
91
    {
92 1
        $answer = new Answer();
93 1
        $answer->setDb($this->di->get("dbqb"));
94 1
        $answer->find("id", $this->form->value("id"));
95 1
        $answer->text = $this->form->value("text");
96 1
        $answer->save();
97 1
        return true;
98
    }
99
100
101
102
    /**
103
     * Callback what to do if the form was successfully submitted, this
104
     * happen when the submit callback method returns true. This method
105
     * can/should be implemented by the subclass for a different behaviour.
106
     */
107 1
    public function callbackSuccess()
108
    {
109 1
        $this->di->get("response")->redirect("game/question/$this->questionid")->send();
110 1
    }
111
112
113
114
    // /**
115
    //  * Callback what to do if the form was unsuccessfully submitted, this
116
    //  * happen when the submit callback method returns false or if validation
117
    //  * fails. This method can/should be implemented by the subclass for a
118
    //  * different behaviour.
119
    //  */
120
    // public function callbackFail()
121
    // {
122
    //     $this->di->get("response")->redirectSelf()->send();
123
    // }
124
}
125