CreateForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Vibe\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Vibe\Comment\Comment;
8
9
/**
10
 * Form to create an item.
11
 */
12
class CreateForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 View Code Duplication
    public function __construct(DIInterface $di)
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...
20
    {
21
        parent::__construct($di);
22
        $this->form->create(
23
            [
24
                "id" => __CLASS__,
25
                "legend" => "Details of the item",
26
            ],
27
            [
28
                "text" => [
29
                    "type" => "textarea",
30
                    "validation" => ["not_empty"],
31
                ],
32
33
                "submit" => [
34
                    "type" => "submit",
35
                    "value" => "Add comment",
36
                    "callback" => [$this, "callbackSubmit"]
37
                ],
38
            ]
39
        );
40
    }
41
42
43
44
    /**
45
     * Callback for submit-button which should return true if it could
46
     * carry out its work and false if something failed.
47
     *
48
     * @return boolean true if okey, false if something went wrong.
49
     */
50
    public function callbackSubmit()
51
    {
52
        $comment = new Comment();
53
        $comment->setDb($this->di->get("database"));
54
        $comment->userId = $this->di->get("session")->get("userId");
55
        $comment->email = $this->di->get("session")->get("userEmail");
56
        $comment->postedBy = $this->di->get("session")->get("username");
57
        $comment->text = $this->form->value("text");
58
        $comment->save();
59
        $this->di->get("response")->redirect("comment");
60
    }
61
}
62