CreateForm   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 55
c 0
b 0
f 0
ccs 0
cts 17
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 29 1
A callbackSubmit() 0 9 1
1
<?php
2
3
namespace Vihd14\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Vihd14\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
    public function __construct(DIInterface $di)
20
    {
21
        parent::__construct($di);
22
        $this->form->create(
23
            [
24
                "id" => __CLASS__,
25
                // "legend" => "Details of the item",
26
            ],
27
            [
28
                "email" => [
29
                    "type" => "text",
30
                    "label" => "E-mail:",
31
                    "validation" => ["not_empty"],
32
                ],
33
                "text" => [
34
                    "type" => "textarea",
35
                    "label" => "Comment:",
36
                    "class" => "comment-section",
37
                    "validation" => ["not_empty"],
38
                ],
39
40
                "submit" => [
41
                    "type" => "submit",
42
                    "value" => "Publish",
43
                    "callback" => [$this, "callbackSubmit"]
44
                ],
45
            ]
46
        );
47
    }
48
49
50
51
    /**
52
     * Callback for submit-button which should return true if it could
53
     * carry out its work and false if something failed.
54
     *
55
     * @return boolean true if okey, false if something went wrong.
56
     */
57
    public function callbackSubmit()
58
    {
59
        $comment = new Comment();
60
        $comment->setDb($this->di->get("db"));
61
        $comment->email  = $this->form->value("email");
62
        $comment->text = $this->form->value("text");
63
        $comment->save();
64
        $this->di->get("response")->redirect("comments");
65
    }
66
}
67