CreateAnswerForm::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\Answer\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pamo\Answer\Answer;
8
9
/**
10
 * Form to create an item.
11
 */
12
class CreateAnswerForm 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 7
    public function __construct(ContainerInterface $di, int $questionId, string $user)
20
    {
21 7
        parent::__construct($di);
22 7
        $this->questionId = $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...
23 7
        $this->form->create(
24
            [
25 7
                "id" => __CLASS__,
26
                "legend" => "Details of the item",
27
            ],
28
            [
29 7
                "questionid" => [
30 7
                    "type" => "hidden",
31 7
                    "value" => $questionId,
32
                ],
33
34
                "user" => [
35 7
                    "type" => "hidden",
36 7
                    "value" => $user,
37
                ],
38
39
                "answer" => [
40
                    "type" => "textarea",
41
                    "validation" => ["not_empty"],
42
                ],
43
44
                "submit" => [
45 7
                    "type" => "submit",
46 7
                    "value" => "Post Your Answer",
47 7
                    "callback" => [$this, "callbackSubmit"]
48
                ],
49
            ]
50
        );
51 7
    }
52
53
54
55
    /**
56
     * Callback for submit-button which should return true if it could
57
     * carry out its work and false if something failed.
58
     *
59
     * @return bool true if okey, false if something went wrong.
60
     */
61 1
    public function callbackSubmit() : bool
62
    {
63 1
        $answer = new Answer();
64 1
        $answer->setDb($this->di->get("dbqb"));
65 1
        $answer->questionid  = $this->form->value("questionid");
66 1
        $answer->user = $this->form->value("user");
67 1
        $answer->text = $this->form->value("answer");
68 1
        $answer->vote = 0;
69 1
        $answer->save();
70
71 1
        $game = $this->di->get("game");
72 1
        $game->updateRank($answer->user, 5);
73
74 1
        return true;
75
    }
76
77
78
79
    /**
80
     * Callback what to do if the form was successfully submitted, this
81
     * happen when the submit callback method returns true. This method
82
     * can/should be implemented by the subclass for a different behaviour.
83
     */
84 1
    public function callbackSuccess()
85
    {
86 1
        $this->di->get("response")->redirect("game/question/$this->questionId")->send();
87 1
    }
88
89
90
91
    // /**
92
    //  * Callback what to do if the form was unsuccessfully submitted, this
93
    //  * happen when the submit callback method returns false or if validation
94
    //  * fails. This method can/should be implemented by the subclass for a
95
    //  * different behaviour.
96
    //  */
97
    // public function callbackFail()
98
    // {
99
    //     $this->di->get("response")->redirectSelf()->send();
100
    // }
101
}
102