CreateAnswer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 21
ccs 0
cts 9
cp 0
crap 2
rs 9.8666
1
<?php
2
3
namespace Aiur18\Question\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Aiur18\Question\Answer;
8
use Aiur18\getset\getset;
9
10
/**
11
 * Form to create an item.
12
 */
13
class CreateAnswer extends FormModel
14
{
15
    public $idForQuestion;
16
17
    /**
18
     * Constructor injects with DI container.
19
     *
20
     * @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...
21
     */
22
    public function __construct(ContainerInterface $di, $id)
23
    {
24
        parent::__construct($di);
25
        $this->idForQuestion = $id;
26
        $this->form->create(
27
            [
28
                "id" => __CLASS__,
29
                "legend" => "Details of the item",
30
            ],
31
            [
32
33
                "answer" => [
34
                    "type" => "textarea",
35
                    "validation" => ["not_empty"],
36
                ],
37
38
39
                "submit" => [
40
                    "type" => "submit",
41
                    "value" => "Create item",
42
                    "callback" => [$this, "callbackSubmit"]
43
                ],
44
            ]
45
        );
46
    }
47
48
49
50
    /** Method
51
     */
52
    public function callbackSubmit() : bool
53
    {
54
55
56
        $answer = new Answer();
57
        $answer->setDb($this->di->get("dbqb"));
58
        //Markdown filter applied
59
        $filterMarkdown = new \Aiur18\Filter\MyTextFilter();
60
        $answerRes = $filterMarkdown->markdown($this->form->value("answer"));
61
62
        $getServer = new getSet();
63
        $answer->user_id  = $getServer->getServer('user_id');
64
        $answer->answer = $answerRes;
65
        $answer->id_question = $this->idForQuestion;
0 ignored issues
show
Bug introduced by
The property id_question does not seem to exist on Aiur18\Question\Answer.
Loading history...
66
        $answer->save();
67
        return true;
68
    }
69
70
    /** Method
71
     */
72
    public function getItemDetails($id) : object
73
    {
74
        $answer = new Answer();
75
        $answer->setDb($this->di->get("dbqb"));
76
        $answer->find("id", $id);
77
        return $answer;
78
    }
79
80
81
    /**
82
     * Method
83
     */
84
    public function setId($id) : bool
85
    {
86
        $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...
87
        return true;
88
    }
89
90
91
    /**
92
     * Method
93
     */
94
    public function getId() : int
95
    {
96
        return $this->id;
97
    }
98
99
100
101
    /** Method
102
     */
103
    public function callbackSuccess()
104
    {
105
        $this->di->get("response")->redirect("question")->send();
106
    }
107
}
108