UpdateCommentForm::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 23
cts 23
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Almrooth\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Almrooth\Comment\Comment;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class UpdateCommentForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 1
    public function __construct(DIInterface $di, $id)
20
    {
21 1
        parent::__construct($di);
22 1
        $comment = $this->getItemDetails($id);
23 1
        $this->form->create(
24
            [
25 1
                "id" => __CLASS__,
26 1
                "use_fieldset" => false,
27 1
            ],
28
            [
29
                "id" => [
30 1
                    "type" => "hidden",
31 1
                    "validation" => ["not_empty"],
32 1
                    "readonly" => true,
33 1
                    "value" => $comment->id,
34 1
                ],
35
                "content" => [
36 1
                    "type"          => "textarea",
37 1
                    "label"         => "Kommentar",
38 1
                    "validation"    => ["not_empty"],
39 1
                    "value"         => $comment->content,
40 1
                ],
41
42
                "submit" => [
43 1
                    "type" => "submit",
44 1
                    "value" => "Uppdatera kommentar",
45 1
                    "callback" => [$this, "callbackSubmit"],
46
                    "class" => "btn"
47 1
                ],
48
            ]
49 1
        );
50 1
    }
51
52
53
    /**
54
     * Get details on item to load form with.
55
     *
56
     * @param integer $id get details on item with id.
57
     *
58
     * @return Comment true if okey, false if something went wrong.
59
     */
60 1
    public function getItemDetails($id)
61
    {
62 1
        $comment = new Comment();
63 1
        $comment->setDb($this->di->get("db"));
64 1
        $comment->find("id", $id);
65 1
        return $comment;
66
    }
67
68
69
    /**
70
     * Callback for submit-button which should return true if it could
71
     * carry out its work and false if something failed.
72
     *
73
     * @return boolean true if okey, false if something went wrong.
74
     */
75 View Code Duplication
    public function callbackSubmit()
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...
76
    {
77
        $comment = new Comment();
78
        $comment->setDb($this->di->get("db"));
79
        $comment->find("id", $this->form->value("id"));
80
        $comment->content   = $this->form->value("content");
81
        $comment->save();
82
83
        // Redirect to profile page
84
        $this->di->get("response")->redirect("comment");
85
    }
86
}
87