Passed
Push — master ( ceaca3...579756 )
by Kevin Olguin
01:46
created

Com2Update::callbackSubmit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Kifekenny\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Kifekenny\Comment\Comment;
8
9
/**
10
 * Form to update an item.
11
 */
12
class Com2Update 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
0 ignored issues
show
Bug introduced by
There is no parameter named $di. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
18
     * @param integer             $id to update
19
     */
20 4
    public function __construct(DIInterface $dis, $id)
21
    {
22 4
        parent::__construct($dis);
23 4
        $currId = $this->di->get("session")->get("user_id");
24 4
        $comment2 = $this->isgetItemDetails($id);
25 4
        if ($currId != 1) {
26 4
            if ($comment2->userId != $currId) {
27 4
                $this->di->get("response")->redirect("");
28 4
            }
29 4
        }
30 4
        $this->form->create(
31
            [
32 4
                "id" => __CLASS__,
33 4
                "legend" => "Update details of the book",
34 4
            ],
35
            [
36
                "id" => [
37 4
                    "type" => "hidden",
38 4
                    "value" => $comment2->id,
39 4
                ],
40
41
                "Title" => [
42 4
                    "type" => "text",
43 4
                    "validation" => ["not_empty"],
44 4
                    "value" => $comment2->title,
45 4
                ],
46
47
                "Content" => [
48 4
                    "type" => "textarea",
49 4
                    "validation" => ["not_empty"],
50 4
                    "value" => $comment2->content,
51 4
                    "class" => "editText",
52 4
                ],
53
54
                "submit" => [
55 4
                    "type" => "submit",
56 4
                    "value" => "Save",
57 4
                    "callback" => [$this, "callbackSubmit"]
58 4
                ],
59
60
                "reset" => [
61 4
                    "type"      => "reset",
62 4
                ],
63
            ]
64 4
        );
65 4
    }
66
67
68
69
    /**
70
     * Get details on item to load form with.
71
     *
72
     * @param integer $id get details on item with id.
73
     *
74
     * @return boolean true if okey, false if something went wrong.
75
     */
76 4 View Code Duplication
    public function isgetItemDetails($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...
77
    {
78 4
        $comment2 = new Comment();
79 4
        $comment2->setDb($this->di->get("db"));
80 4
        $comment2->find("id", $id);
81 4
        return $comment2;
82
    }
83
84
85
86
    /**
87
     * Callback for submit-button which should return true if it could
88
     * carry out its work and false if something failed.
89
     *
90
     * @return boolean true if okey, false if something went wrong.
91
     */
92
    public function callbackSubmit()
93
    {
94
        $comment = new Comment();
95
        $comment->setDb($this->di->get("db"));
96
        $comment->find("id", $this->form->value("id"));
97
        $comment->title = $this->form->value("Title");
98
        $comment->content = $this->form->value("Content");
99
        $comment->save();
100
        $this->di->get("response")->redirect("");
101
    }
102
}
103