CreateAnswerCommentsForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 69
ccs 12
cts 24
cp 0.5
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

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