Passed
Push — master ( 6494fd...7e1229 )
by Nicklas
02:09
created

EditCommentForm   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 123
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 35 1
A getCommentDetails() 0 7 1
A controlAuthority() 0 14 2
A callbackDelete() 0 13 2
A callbackSubmit() 0 13 2
1
<?php
2
3
namespace Nicklas\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Nicklas\Comment\User;
8
use \Nicklas\Comment\Comment;
9
10
/**
11
 * Form to update an item.
12
 */
13
class EditCommentForm 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
        $this->comment = $this->getCommentDetails($id);
0 ignored issues
show
Bug introduced by
The property comment does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->form->create(
26
            [
27
                "id" => __CLASS__,
28
                "legend" => "Update details of the (comment {$this->comment->id})",
29
                "fieldset" => true
30
            ],
31
            [
32
                "comment" => [
33
                    "type" => "textarea",
34
                    "validation" => ["not_empty"],
35
                    "value" => $this->comment->comment,
36
                ],
37
38
                "submit" => [
39
                    "type" => "submit",
40
                    "value" => "Save",
41
                    "callback" => [$this, "callbackSubmit"]
42
                ],
43
44
                "reset" => [
45
                    "type"      => "reset",
46
                ],
47
48
                "delete" => [
49
                    "type" => "submit",
50
                    "value" => "Delete",
51
                    "callback" => [$this, "callBackDelete"]
52
                ],
53
            ]
54
        );
55
    }
56
57
58
59
    /**
60
     * Get details on item to load form with.
61
     *
62
     * @param integer $id get details on item with id.
63
     *
64
     * @return object true if okey, false if something went wrong.
65
     */
66
    public function getCommentDetails($id)
67
    {
68
        $comment = new Comment();
69
        $comment->setDb($this->di->get("db"));
70
        $comment->find("id", $id);
71
        return $comment;
72
    }
73
74
75
    /**
76
     * get details for the user based on sessio
77
     *
78
     *
79
     * @return object the user
80
     */
81
    public function controlAuthority()
82
    {
83
        // Get logged in users details, based on session
84
        $userName = $this->di->get("session")->get("user");
85
        $user = new User();
86
        $user->setDb($this->di->get("db"));
87
        $user->find("name", $userName);
88
89
        // IF AUTHORITY == admin, then continue
90
        if ($user->authority != "admin") {
91
            return ($user->name == $this->comment->user);
92
        }
93
        return true;
94
    }
95
96
    /**
97
     * Callback for submit-button which should return true if it could
98
     * carry out its work and false if something failed.
99
     *
100
     * @return void
101
     */
102
    public function callbackDelete()
103
    {
104
        // Form values
105
        $comment = $this->comment;
106
107
        if (!$this->controlAuthority($comment)) {
0 ignored issues
show
Unused Code introduced by
The call to EditCommentForm::controlAuthority() has too many arguments starting with $comment.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
            $this->form->addOutput("You can't edit comment #$comment->id");
109
            return false;
110
        }
111
112
        $comment->delete();
113
        $this->di->get("response")->redirect("comment");
114
    }
115
116
    /**
117
     * Callback for submit-button which should return true if it could
118
     * carry out its work and false if something failed.
119
     *
120
     * @return void
121
     */
122
    public function callbackSubmit()
123
    {
124
        $comment = $this->comment;
125
126
        if (!$this->controlAuthority($comment)) {
0 ignored issues
show
Unused Code introduced by
The call to EditCommentForm::controlAuthority() has too many arguments starting with $comment.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
127
            $this->form->addOutput("You can't edit comment #$comment->id");
128
            return false;
129
        }
130
131
        $comment->comment = $this->form->value("comment");
132
        $comment->save();
133
        $this->di->get("response")->redirect("comment/edit/{$comment->id}");
134
    }
135
}
136