UpdateCommentForm::callbackSubmit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
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
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateCommentForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     * @param integer             $id to update
19
     */
20
    public function __construct(DIInterface $di, $id)
21
    {
22
        parent::__construct($di);
23
        $commentInfo = $this->getItemDetails($id);
24
        $this->form->create(
25
            [
26
                "id"     => __CLASS__,
27
                "legend" => "Update details of the item",
28
            ],
29
            [
30
                "id"    => [
31
                    "type"     => "hidden",
32
                    "value"    => $commentInfo->idcomment,
33
                ],
34
                "text" => [
35
                    "type"       => "text",
36
                    "value"      => $commentInfo->commenttext,
37
                ],
38
39
                "submit" => [
40
                    "type"     => "submit",
41
                    "value"    => "Edit comment",
42
                    "callback" => [$this, "callbackSubmit"],
43
                ],
44
            ]
45
        );
46
    }
47
48
49
    /**
50
     * Get details on item to load form with.
51
     *
52
     * @param integer $id get details on item with id.
53
     *
54
     * @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...
55
     */
56
    public function getItemDetails($id)
57
    {
58
        $comment = new Comment();
59
        $comment->setDb($this->di->get("db"));
60
        $comment->find("idcomment", $id);
61
        return $comment;
62
    }
63
64
65
66
    /**
67
     * Callback for submit-button which should return true if it could
68
     * carry out its work and false if something failed.
69
     *
70
     * @return boolean true if okey, false if something went wrong.
71
     */
72
    public function callbackSubmit()
73
    {
74
        $comment = new Comment();
75
        $comment->setDb($this->di->get("db"));
76
        $comment->find("idcomment", $this->form->value("id"));
77
        $data = htmlentities($this->form->value("text"));
78
        $text = htmlentities($this->di->get("textfilter")->doFilter($data, ["bbcode",
79
        "clickable", "shortcode", "markdown", "purify"]));
80
        $comment->commenttext = $text;
81
        $id = $this->form->value("id");
82
        $comment->save("idcomment", $id);
83
        $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...
84
        $postid = $comment->idpost;
85
        $url = $this->di->get("url")->create("comment/retrieve/{$postid}");
86
        $this->di->get("response")->redirect($url);
87
    }
88
}
89