DeleteCommentForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 25
nc 1
nop 3
dl 0
loc 40
c 0
b 0
f 0
cc 1
ccs 19
cts 19
cp 1
crap 1
rs 9.52
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 delete an item.
12
 */
13
class DeleteCommentForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container.
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
     */
20 2
    public function __construct(ContainerInterface $di, $type, $commentId)
21
    {
22 2
        parent::__construct($di);
23 2
        $comment = $this->getItemDetails($type, $commentId);
24
25 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...
26
27 2
        $this->form->create(
28
            [
29 2
                "id" => __CLASS__,
30
                "legend" => "Delete a comment",
31
            ],
32
            [
33 2
                "id" => [
34 2
                    "type" => "hidden",
35 2
                    "value" => $comment->id,
36
                ],
37
38
                "type" => [
39 2
                    "type" => "hidden",
40 2
                    "value" => $type,
41
                ],
42
43
                "text" => [
44 2
                    "type" => "text",
45
                    "readonly" => true,
46
                    "label" => false,
47 2
                    "value" => $comment->text
48
                ],
49
50
                "submit" => [
51 2
                    "type" => "submit",
52 2
                    "value" => "Delete comment",
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->delete();
97 2
        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 2
    public function callbackSuccess()
108
    {
109 2
        $this->di->get("response")->redirect("game/question/$this->questionid")->send();
110 2
    }
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