Passed
Push — master ( 627324...37a157 )
by Magnus
02:13
created

UpdateCommentCommentForm::callbackSubmit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
crap 2
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\Comment\CommentComments;
9
10
/**
11
 * Form to update an item.
12
 */
13
class UpdateCommentCommentForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container and the id to update.
17
     *
18
     * @param Anax\DI\DIInterface $di a service container
19
     * @param integer             $id to update
20
     */
21
    public function __construct(DIInterface $di, $id)
22
    {
23
        parent::__construct($di);
24
        $commentInfo = $this->getItemDetails($id);
25
        $this->form->create(
26
            [
27
                "id"     => __CLASS__,
28
                "legend" => "Update details of the item",
29
            ],
30
            [
31
                "id"    => [
32
                    "type"     => "hidden",
33
                    "value"    => $commentInfo->idcommentc,
34
                ],
35
                "text" => [
36
                    "type"       => "text",
37
                    "value"      => $commentInfo->textcomment,
38
                ],
39
40
                "submit" => [
41
                    "type"     => "submit",
42
                    "value"    => "Edit comment",
43
                    "callback" => [$this, "callbackSubmit"],
44
                ],
45
            ]
46
        );
47
    }
48
49
50
    /**
51
     * Get details on item to load form with.
52
     *
53
     * @param integer $id get details on item with id.
54
     *
55
     * @return $comment
0 ignored issues
show
Documentation introduced by
The doc-type $comment could not be parsed: Unknown type name "$comment" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
56
     */
57
    public function getItemDetails($id)
58
    {
59
        $comment = new CommentComments();
60
        $comment->setDb($this->di->get("db"));
61
        $comment->find("idcommentc", $id);
62
        return $comment;
63
    }
64
65
66
67
    /**
68
     * Callback for submit-button which should return true if it could
69
     * carry out its work and false if something failed.
70
     *
71
     * @return boolean true if okey, false if something went wrong.
72
     */
73
    public function callbackSubmit()
74
    {
75
        $comment = new CommentComments();
76
        $comment->setDb($this->di->get("db"));
77
        $comment->find("idcommentc", $this->form->value("id"));
78
        $data = htmlentities($this->form->value("text"));
79
        $text = htmlentities($this->di->get("textfilter")->doFilter($data, ["bbcode",
80
        "clickable", "shortcode", "markdown", "purify"]));
81
        $comment->textcomment = $text;
82
        $id = $this->form->value("id");
83
        $comment->save("idcommentc", $id);
84
        $id = $this->form->value("id");
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
    //    $postid = $comment->idpost;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
        $url = $this->di->get("url")->create("comment/viewAllPosts");
87
        $this->di->get("response")->redirect($url);
88
    }
89
}
90