CreateForm::callbackSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Lyco\Comment\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Lyco\Post\Post;
8
use Lyco\User\User;
9
use Lyco\Comment\Comment;
10
11
/**
12
 * Form to create an item.
13
 */
14
class CreateForm extends FormModel
15
{
16
    public $postId;
17
    public $id;
18
    /**
19
     * Constructor injects with DI container.
20
     *
21
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Lyco\Comment\HTMLForm\Ps...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
22
     */
23
    public function __construct(ContainerInterface $di, $postId)
24
    {
25
        parent::__construct($di);
26
        $this->postId = $postId;
27
        $this->replyId = $this->di->get("request")->getGet("replyId");
0 ignored issues
show
Bug Best Practice introduced by
The property replyId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
29
        $this->form->create(
30
            [
31
                "commentId" => __CLASS__
32
            ],
33
            [
34
                "comment" => [
35
                    "type" => "textarea",
36
                    "validation" => ["not_empty"],
37
                ],
38
39
                "submit" => [
40
                    "type" => "submit",
41
                    "value" => "Send",
42
                    "callback" => [$this, "callbackSubmit"]
43
                ],
44
            ]
45
        );
46
    }
47
48
    /**
49
     * Callback for submit-button which should return true if it could
50
     * carry out its work and false if something failed.
51
     *
52
     * @return bool true if okey, false if something went wrong.
53
     */
54
    public function callbackSubmit() : bool
55
    {
56
        $comment = new Comment();
57
        $comment->setDb($this->di->get("dbqb"));
58
        $comment->postId = $this->postId;
59
        $comment->replyId = $this->replyId;
60
        $comment->id = $this->di->get("session")->get("userId");
61
        $comment->text = $this->form->value("comment");
62
        $comment->save();
63
        return true;
64
    }
65
66
    /**
67
     * Callback what to do if the form was successfully submitted, this
68
     * happen when the submit callback method returns true. This method
69
     * can/should be implemented by the subclass for a different behaviour.
70
     */
71
    public function callbackSuccess()
72
    {
73
        $this->di->get("response")->redirect("post/view/" . $this->postId)->send();
74
    }
75
}
76