Passed
Push — master ( ea5d4f...e4e351 )
by Magnus
02:01
created

CreateCommentForm::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 15

Duplication

Lines 28
Ratio 100 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 28
loc 28
ccs 16
cts 16
cp 1
rs 8.8571
cc 1
eloc 15
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Radchasay\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\Comment\Comment;
8
use \Radchasay\User\User;
9
10
/**
11
 * Example of FormModel implementation.
12
 */
13
class CreateCommentForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container.
17
     *
18
     * @param Anax\DI\DIInterface $di a service container
19
     */
20 2 View Code Duplication
    public function __construct(DIInterface $di, $id)
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...
21
    {
22 2
        parent::__construct($di);
23
24 2
        $this->form->create(
25
            [
26 2
                "id"     => __CLASS__,
27 2
                "legend" => "Create new comment",
28
                "class" => "createNewCommentForm"
29 2
            ],
30
            [
31
                    "text"    => [
32
                        "type"     => "textarea"
33 2
                    ],
34
35
                    "hidden" => [
36 2
                        "type"       => "hidden",
37 2
                        "value"      => $id,
38 2
                    ],
39
40
                    "submit" => [
41 2
                        "type"     => "submit",
42 2
                        "value"    => "Create comment",
43 2
                        "callback" => [$this, "callbackSubmit"],
44 2
                    ],
45
            ]
46 2
        );
47 2
    }
48
49
    /**
50
     * Callback for submit-button which should return true if it could
51
     * carry out its work and false if something failed.
52
     *
53
     * @return boolean true if okey, false if something went wrong.
54
     */
55
    public function callbackSubmit()
56
    {
57
        // Get values from the submitted form
58
59
        $comment = new Comment();
60
        $comment->setDB($this->di->get("db"));
61
        $data = $this->form->value("text");
62
        $text = $this->di->get("textfilter")->doFilter($data, ["shortcode", "markdown", "clickable", "bbcode"]);
63
        $comment->commenttext = $text;
64
        $comment->idpost = $this->form->value("hidden");
65
        $comment->postuser = $this->di->get("session")->get("email");
66
67
        $user = new User();
68
        $user->setDb($this->di->get("db"));
69
        $user->getInformation($comment->postuser);
70
        $user->points += 1;
0 ignored issues
show
Bug introduced by
The property points does not seem to exist in Radchasay\User\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
71
72
73
        $comment->save();
74
        $user->save();
75
76
        $url = $this->di->get("url")->create("comment/retrieve/$comment->idpost");
77
        $this->di->get("response")->redirect($url);
78
        return true;
79
    }
80
}
81