CreateForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 33
Ratio 100 %

Importance

Changes 0
Metric Value
dl 33
loc 33
c 0
b 0
f 0
rs 9.392
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Forum\Comment\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Forum\Answer\Answer;
7
use Forum\Comment\Comment;
8
use Forum\User\User;
9
use Psr\Container\ContainerInterface;
10
11
12
/**
13
 * Form to create an item.
14
 */
15
class CreateForm extends FormModel
16
{
17
    /**
18
     * Constructor injects with DI container.
19
     *
20
     * @param ContainerInterface $di a service container
21
     * @param string $questionId the id of the question
22
     */
23 View Code Duplication
    public function __construct(ContainerInterface $di, string $questionId, string $answerId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        error_log("constructor called");
26
        parent::__construct($di);
27
        $this->form->create(
28
            [
29
                "id" => __CLASS__,
30
                "legend" => "Skapa svar",
31
            ],
32
            [
33
                "kommentar" => [
34
                    "type" => "textarea",
35
                    "validation" => ["not_empty"],
36
                ],
37
38
                "answer_id" => [
39
                    "type"        => "hidden",
40
                    "value"       => $answerId,
41
                ],
42
43
                "question_id" => [
44
                    "type"        => "hidden",
45
                    "value"       => $questionId,
46
                ],
47
48
                "submit" => [
49
                    "type" => "submit",
50
                    "value" => "Skapa svar",
51
                    "callback" => [$this, "callbackSubmit"]
52
                ],
53
            ]
54
        );
55
    }
56
57
58
59
    /**
60
     * Callback for submit-button which should return true if it could
61
     * carry out its work and false if something failed.
62
     *
63
     * @return bool true if okey, false if something went wrong.
64
     */
65
    public function callbackSubmit() : bool
66
    {
67
        error_log("callbacksubmit");
68
        $comment = new Comment();
69
        $comment->setDb($this->di->get("dbqb"));
70
        $comment->comment = $this->form->value("kommentar");
71
        $answerId = $this->form->value("answer_id");
72
        if (!empty($answerId)) {
73
            $comment->answer_id = $answerId;
74
        }
75
        $questionId = $this->form->value("question_id");
76
        if (!empty($questionId)) {
77
            $comment->question_id = $questionId;
78
        }
79
        $comment->user_id = $this->di->get("session")->get("user_id");
80
        var_dump($comment->user_id);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($comment->user_id); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
81
        $comment->save();
82
        return true;
83
    }
84
85
86
87
    /**
88
     * Callback what to do if the form was successfully submitted, this
89
     * happen when the submit callback method returns true. This method
90
     * can/should be implemented by the subclass for a different behaviour.
91
     */
92
    public function callbackSuccess()
93
    {
94
        $questionId = $this->form->value("question_id");
95
        if (!empty($questionId)) {
96
            $this->di->get("response")->redirect("question/read/" . $questionId)->send();
97
        }
98
        $answerId = $this->form->value("answer_id");
99
        if (!empty($answerId)) {
100
            $answer = new Answer();
101
            $answer->setDb($this->di->get("dbqb"));
102
            $answer->findById($answerId);
103
            $this->di->get("response")->redirect("question/read/" . $answer->question_id)->send();
104
        }
105
    }
106
107
108
109
     /**
110
      * Callback what to do if the form was unsuccessfully submitted, this
111
      * happen when the submit callback method returns false or if validation
112
      * fails. This method can/should be implemented by the subclass for a
113
      * different behaviour.
114
      */
115
     public function callbackFail()
116
     {
117
         $this->di->get("response")->redirectSelf()->send();
118
     }
119
}
120