UpdateCommentForm::callbackSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Pamo\Comment\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pamo\Comment\CommentQ;
8
use Pamo\Comment\CommentA;
9
10
/**
11
 * Form to update an item.
12
 */
13
class UpdateCommentForm 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 Pamo\Comment\HTMLForm\Ps...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 2
    public function __construct(ContainerInterface $di, $type, $commentId)
22
    {
23 2
        parent::__construct($di);
24 2
        $comment = $this->getItemDetails($type, $commentId);
25
26 2
        $this->questionid = $comment->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...
27
28 2
        $this->form->create(
29
            [
30 2
                "id" => __CLASS__,
31
                "legend" => "Update a comment",
32
            ],
33
            [
34 2
                "id" => [
35 2
                    "type" => "hidden",
36 2
                    "value" => $comment->id,
37
                ],
38
39
                "type" => [
40 2
                    "type" => "hidden",
41 2
                    "value" => $type,
42
                ],
43
44
                "text" => [
45 2
                    "type" => "text",
46
                    "label" => false,
47 2
                    "value" => $comment->text
48
                ],
49
50
                "submit" => [
51 2
                    "type" => "submit",
52 2
                    "value" => "Save",
53 2
                    "callback" => [$this, "callbackSubmit"]
54
                ],
55
56
                "cancel" => [
57 2
                    "type" => "button",
58 2
                    "value" => "Cancel",
59 2
                    "onclick" => "location.href='$this->questionid';"
60
                ],
61
            ]
62
        );
63 2
    }
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 $comment
0 ignored issues
show
Documentation Bug introduced by
The doc comment $comment at position 0 could not be parsed: Unknown type name '$comment' at position 0 in $comment.
Loading history...
73
     */
74 2
    public function getItemDetails($type, $commentId) : object
75
    {
76 2
        $comment = $type === "question" ? new CommentQ() : new CommentA();
77 2
        $comment->setDb($this->di->get("dbqb"));
78 2
        $comment->find("id", $commentId);
79 2
        return $comment;
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 2
    public function callbackSubmit() : bool
91
    {
92 2
        $type = $this->form->value("type");
93 2
        $comment = $type === "question" ? new CommentQ() : new CommentA();
94 2
        $comment->setDb($this->di->get("dbqb"));
95 2
        $comment->find("id", $this->form->value("id"));
96 2
        $comment->text = $this->form->value("text");
97 2
        $comment->save();
98 2
        return true;
99
    }
100
101
102
103
    /**
104
     * Callback what to do if the form was successfully submitted, this
105
     * happen when the submit callback method returns true. This method
106
     * can/should be implemented by the subclass for a different behaviour.
107
     */
108 2
    public function callbackSuccess()
109
    {
110 2
        $this->di->get("response")->redirect("game/question/$this->questionid")->send();
111 2
    }
112
113
114
115
    // /**
116
    //  * Callback what to do if the form was unsuccessfully submitted, this
117
    //  * happen when the submit callback method returns false or if validation
118
    //  * fails. This method can/should be implemented by the subclass for a
119
    //  * different behaviour.
120
    //  */
121
    // public function callbackFail()
122
    // {
123
    //     $this->di->get("response")->redirectSelf()->send();
124
    // }
125
}
126