CreateQuestionCommentsForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 47.83%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 67
ccs 11
cts 23
cp 0.4783
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A callbackSubmit() 0 11 1
A __construct() 0 27 1
A callbackSuccess() 0 3 1
1
<?php
2
3
namespace Seb\QuestionComments\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Seb\QuestionComments\QuestionComments;
8
9
/**
10
 * Form to create an item.
11
 */
12
class CreateQuestionCommentsForm extends FormModel
13
{
14
    public $qid;
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 Seb\QuestionComments\HTM...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, int $id)
21
    {
22 2
        $this->qid = $id;
23 2
        parent::__construct($di);
24 2
        $this->form->create(
25
            [
26 2
                "id" => __CLASS__,
27
                "legend" => "Create Comment",
28
            ],
29
            [
30
                
31
                "questionid" => [
32 2
                    "type" => "hidden",
33
                    "validation" => ["not_empty"],
34
                    "readonly" => true,
35 2
                    "value" => $id,
36
                ],
37
38
                "comment" => [
39
                    "type" => "textarea",
40
                    "validation" => ["not_empty"],
41
                ],
42
43
                "submit" => [
44 2
                    "type" => "submit",
45 2
                    "value" => "Create item",
46 2
                    "callback" => [$this, "callbackSubmit"]
47
                ],
48
            ]
49
        );
50 2
    }
51
52
    /**
53
     * Callback for submit-button which should return true if it could
54
     * carry out its work and false if something failed.
55
     *
56
     * @return bool true if okey, false if something went wrong.
57
     */
58
    public function callbackSubmit() : bool
59
    {
60
        $session = $this->di->get("session");
61
        $comments = new QuestionComments();
62
        $comments->setDb($this->di->get("dbqb"));
63
        $comments->acronym = $session->get("acronym");
64
        $comments->userid = $session->get("userid");
65
        $comments->comment = $this->form->value("comment");
66
        $comments->questionid = $this->form->value("questionid");
67
        $comments->save();
68
        return true;
69
    }
70
71
    /**
72
     * Callback what to do if the form was successfully submitted, this
73
     * happen when the submit callback method returns true. This method
74
     * can/should be implemented by the subclass for a different behaviour.
75
     */
76
    public function callbackSuccess()
77
    {
78
        $this->di->get("response")->redirect("forum/topic/{$this->qid}")->send();
79
    }
80
}
81