UpdateForm::getItemDetails()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Vibe\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Vibe\Comment\Comment;
8
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateForm 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, $userId)
21
    {
22
        parent::__construct($di);
23
        $comment = $this->getItemDetails($id, $userId);
24
25
        /* If comment doesn't belong to user, redirect to all comments */
26
        if (!$comment) {
27
            $this->di->get("response")->redirect("comment");
28
        }
29
30
        $this->form->create(
31
            [
32
                "id" => __CLASS__,
33
                "legend" => "Update details of the item",
34
            ],
35
            [
36
                "id" => [
37
                    "type" => "text",
38
                    "validation" => ["not_empty"],
39
                    "readonly" => true,
40
                    "value" => $comment->id,
41
                ],
42
43
                "text" => [
44
                    "type" => "text",
45
                    "validation" => ["not_empty"],
46
                    "value" => $comment->text,
47
                ],
48
49
                "submit" => [
50
                    "type" => "submit",
51
                    "value" => "Save",
52
                    "callback" => [$this, "callbackSubmit"]
53
                ],
54
55
                "reset" => [
56
                    "type"      => "reset",
57
                ],
58
            ]
59
        );
60
    }
61
62
63
64
    /**
65
     * Get details on item to load form with.
66
     *
67
     * @param integer $id get details on item with id.
68
     *
69
     * @return Comment
70
     */
71
    public function getItemDetails($id, $userId)
72
    {
73
        $comment = new Comment();
74
        $comment->setDb($this->di->get("database"));
75
        $comment->find("id", $id);
76
        if ($comment->userId == $userId) {
77
            return $comment;
78
        }
79
    }
80
81
82
83
    /**
84
     * Callback for submit-button which should return true if it could
85
     * carry out its work and false if something failed.
86
     *
87
     * @return boolean true if okey, false if something went wrong.
88
     */
89 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...
90
    {
91
        $comment = new Comment();
92
        $comment->setDb($this->di->get("database"));
93
        $comment->find("id", $this->form->value("id"));
94
        $comment->text = $this->form->value("text");
95
        $comment->save();
96
        $this->di->get("response")->redirect("comment");
97
    }
98
}
99