DeleteAnswerForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 22
nc 1
nop 2
dl 0
loc 34
c 0
b 0
f 0
cc 1
ccs 17
cts 17
cp 1
crap 1
rs 9.568
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 delete an item.
11
 */
12
class DeleteAnswerForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
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
     */
19 1
    public function __construct(ContainerInterface $di, $id)
20
    {
21 1
        parent::__construct($di);
22 1
        $answer = $this->getItemDetails($id);
23 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...
24
25 1
        $this->form->create(
26
            [
27 1
                "id" => __CLASS__,
28
                "legend" => "Delete an item",
29
            ],
30
            [
31 1
                "id" => [
32 1
                    "type" => "hidden",
33 1
                    "value" => $answer->id,
34
                ],
35
36
                "text" => [
37 1
                    "type" => "textarea",
38
                    "readonly" => true,
39
                    "label" => false,
40 1
                    "value" => $answer->text
41
                ],
42
43
                "submit" => [
44 1
                    "type" => "submit",
45 1
                    "value" => "Delete answer",
46 1
                    "callback" => [$this, "callbackSubmit"]
47
                ],
48
49
                "cancel" => [
50 1
                    "type" => "button",
51 1
                    "value" => "Cancel",
52 1
                    "onclick" => "location.href='$this->questionid';"
53
                ],
54
            ]
55
        );
56 1
    }
57
58
59
60
    /**
61
     * Get all items.
62
     *
63
     * @return object with key value of all items.
64
     */
65 1
    protected function getItemDetails($id) : object
66
    {
67 1
        $answer = new Answer();
68 1
        $answer->setDb($this->di->get("dbqb"));
69 1
        $answer->find("id", $id);
70 1
        return $answer;
71
    }
72
73
74
75
    /**
76
     *
77
     * @return null
78
     */
79 1
    public function deleteComments($id)
80
    {
81 1
        $game = $this->di->get("game");
82
83 1
        $commentAnswer = $game->commentAnswer;
84 1
        $commentsToDelete = $commentAnswer->findAllWhere("answerid = ?", $id);
85
86 1
        foreach ($commentsToDelete as $comment) {
87 1
            $commentAnswer->find("id", $comment->id);
88 1
            $commentAnswer->delete();
89
        }
90 1
    }
91
92
93
94
    /**
95
     * Callback for submit-button which should return true if it could
96
     * carry out its work and false if something failed.
97
     *
98
     * @return bool true if okey, false if something went wrong.
99
     */
100 1
    public function callbackSubmit() : bool
101
    {
102 1
        $id = $this->form->value("id");
103
        // First delete all comments connected to answer
104 1
        $this->deleteComments($id);
105
106
        // Now delete the actual answer
107 1
        $answer = new Answer();
108 1
        $answer->setDb($this->di->get("dbqb"));
109 1
        $answer->find("id", $id);
110 1
        $answer->delete();
111 1
        return true;
112
    }
113
114
115
116
    /**
117
     * Callback what to do if the form was successfully submitted, this
118
     * happen when the submit callback method returns true. This method
119
     * can/should be implemented by the subclass for a different behaviour.
120
     */
121 1
    public function callbackSuccess()
122
    {
123 1
        $this->di->get("response")->redirect("game/question/$this->questionid")->send();
124 1
    }
125
126
127
128
    // /**
129
    //  * Callback what to do if the form was unsuccessfully submitted, this
130
    //  * happen when the submit callback method returns false or if validation
131
    //  * fails. This method can/should be implemented by the subclass for a
132
    //  * different behaviour.
133
    //  */
134
    // public function callbackFail()
135
    // {
136
    //     $this->di->get("response")->redirectSelf()->send();
137
    // }
138
}
139