Completed
Push — master ( a54060...9ea8d0 )
by Magnus
02:10
created

CreatePostForm   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 61.53%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 59
ccs 16
cts 26
cp 0.6153
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 26 1
A callbackSubmit() 0 17 1
1
<?php
2
3
namespace Radchasay\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\Comment\Post;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class CreatePostForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 2
    public function __construct(DIInterface $di)
20
    {
21 2
        parent::__construct($di);
22
23 2
        $this->form->create(
24
            [
25 2
                "id"     => __CLASS__,
26 2
                "legend" => "Create Post",
27 2
            ],
28
            [
29
                "title" => [
30 2
                    "type" => "text",
31 2
                ],
32
33
                "text" => [
34 2
                    "type" => "text",
35 2
                ],
36
37
                "submit" => [
38 2
                    "type"     => "submit",
39 2
                    "value"    => "Create Post",
40 2
                    "callback" => [$this, "callbackSubmit"],
41 2
                ],
42
            ]
43 2
        );
44 2
    }
45
46
47
    /**
48
     * Callback for submit-button which should return true if it could
49
     * carry out its work and false if something failed.
50
     *
51
     * @return boolean true if okey, false if something went wrong.
52
     */
53
    public function callbackSubmit()
54
    {
55
        // Get values from the submitted form
56
        //
57
        $post = new Post();
58
        $post->setDB($this->di->get("db"));
59
        $post->posttitle = htmlentities($this->form->value("title"));
60
        $post->posttext = htmlentities($this->form->value("text"));
61
        $post->postname = htmlentities($this->di->get("session")->get("email"));
62
63
64
        $post->save();
65
66
        $url = $this->di->get("url")->create("comment/viewAllPosts");
67
        $this->di->get("response")->redirect($url);
68
        return true;
69
    }
70
}
71