CreateForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Lyco\Post\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Lyco\Post\Post;
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 Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Lyco\Post\HTMLForm\Psr\C...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct(ContainerInterface $di)
20
    {
21
        parent::__construct($di);
22
        $userName = $this->di->get("session")->get("userName");
23
        $this->form->create(
24
            [
25
                "postId" => __CLASS__
26
            ],
27
            [
28
                "acronym" => [
29
                    "type" => "text",
30
                    "readonly" => true,
31
                    "validation" => ["not_empty"],
32
                    "value" => $userName
33
                ],
34
35
                "title" => [
36
                    "type" => "text",
37
                    "validation" => ["not_empty"],
38
                ],
39
40
                "text" => [
41
                    "type" => "textarea",
42
                    "validation" => ["not_empty"],
43
                ],
44
45
                "submit" => [
46
                    "type" => "submit",
47
                    "value" => "Create Post",
48
                    "callback" => [$this, "callbackSubmit"]
49
                ],
50
            ]
51
        );
52
    }
53
54
55
56
    /**
57
     * Callback for submit-button which should return true if it could
58
     * carry out its work and false if something failed.
59
     *
60
     * @return bool true if okey, false if something went wrong.
61
     */
62
    public function callbackSubmit() : bool
63
    {
64
        $post = new Post();
65
        $post->setDb($this->di->get("dbqb"));
66
        $post->acronym  = $this->form->value("acronym");
67
        $post->title = $this->form->value("title");
68
        $post->text = $this->form->value("text");
69
        $post->save();
70
        return true;
71
    }
72
73
74
75
    /**
76
     * Callback what to do if the form was successfully submitted, this
77
     * happen when the submit callback method returns true. This method
78
     * can/should be implemented by the subclass for a different behaviour.
79
     */
80
    public function callbackSuccess()
81
    {
82
        $this->di->get("response")->redirect("post")->send();
83
    }
84
85
86
87
    // /**
88
    //  * Callback what to do if the form was unsuccessfully submitted, this
89
    //  * happen when the submit callback method returns false or if validation
90
    //  * fails. This method can/should be implemented by the subclass for a
91
    //  * different behaviour.
92
    //  */
93
    // public function callbackFail()
94
    // {
95
    //     $this->di->get("response")->redirectSelf()->send();
96
    // }
97
}
98