CreateComment   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 95
ccs 0
cts 35
cp 0
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A callbackSubmit() 0 16 1
A getId() 0 3 1
A callbackSuccess() 0 3 1
A setId() 0 4 1
A getItemDetails() 0 6 1
1
<?php
2
3
namespace Aiur18\Question\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Aiur18\Question\Comment;
8
use Aiur18\Filter\MyTextFilter;
9
use Aiur18\getset\getset;
10
11
/**
12
 * Form to create an item.
13
 */
14
class CreateComment extends FormModel
15
{
16
    public $idForQuestion;
17
    public $idForAnswer;
18
19
    /**
20
     * Constructor injects with DI container.
21
     *
22
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Aiur18\Question\HTMLForm...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
23
     */
24
    public function __construct(ContainerInterface $di, $idAnswer, $idQuestion)
25
    {
26
        parent::__construct($di);
27
        $this->idForAnswer = $idAnswer;
28
        $this->idForQuestion = $idQuestion;
29
        $this->form->create(
30
            [
31
                "id" => __CLASS__,
32
                "legend" => "Details of the item",
33
            ],
34
            [
35
36
                "comment" => [
37
                    "type" => "textarea",
38
                    "validation" => ["not_empty"],
39
                ],
40
41
42
                "submit" => [
43
                    "type" => "submit",
44
                    "value" => "Create item",
45
                    "callback" => [$this, "callbackSubmit"]
46
                ],
47
            ]
48
        );
49
    }
50
51
52
53
    /** Method
54
     */
55
    public function callbackSubmit() : bool
56
    {
57
        $answer = new Comment();
58
        $answer->setDb($this->di->get("dbqb"));
59
60
        //Markdown filter applied
61
        $filterMarkdown = new \Aiur18\Filter\MyTextFilter();
62
        $commentRes = $filterMarkdown->markdown($this->form->value("comment"));
63
64
        $getServer = new getSet();
65
        $answer->user_id  = $getServer->getServer('user_id');
66
        $answer->comment = $commentRes;
67
        $answer->id_answer = $this->idForAnswer;
0 ignored issues
show
Bug introduced by
The property id_answer does not seem to exist on Aiur18\Question\Comment.
Loading history...
68
        $answer->id_question = $this->idForQuestion;
0 ignored issues
show
Bug introduced by
The property id_question does not seem to exist on Aiur18\Question\Comment.
Loading history...
69
        $answer->save();
70
        return true;
71
    }
72
73
    /** Method
74
     */
75
    public function getItemDetails($id) : object
76
    {
77
        $answer = new Comment();
78
        $answer->setDb($this->di->get("dbqb"));
79
        $answer->find("id", $id);
80
        return $answer;
81
    }
82
83
84
    /**
85
     * Method
86
     */
87
    public function setId($id) : bool
88
    {
89
        $this->id = $id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
90
        return true;
91
    }
92
93
94
    /**
95
     * Method
96
     */
97
    public function getId() : int
98
    {
99
        return $this->id;
100
    }
101
102
103
104
    /** Method
105
     */
106
    public function callbackSuccess()
107
    {
108
        $this->di->get("response")->redirect("question")->send();
109
    }
110
}
111