CommentController::getCommentToEdit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 12
loc 12
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Mafd16\Comment;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
8
/**
9
 * A controller for the Comment System.
10
 *
11
 * @SuppressWarnings(PHPMD.ExitExpression)
12
 */
13
//class CommentController implements AppInjectableInterface
14
class CommentController implements InjectionAwareInterface
15
{
16
    use InjectionAwareTrait;
17
18
19
    /**
20
     * Get ALL comments from an article.
21
     *
22
     * @return void
23
     */
24 View Code Duplication
    public function getComments()
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...
25
    {
26
        $key = "comPage";
27
        // Get comments from model
28
        $comments = $this->di->get("com")->getComments($key);
29
        // Add views to a specific region, add comments
30
        $this->di->get("view")->add("comment/index", ["comments"=>$comments], "main");
31
        // Render a standard page using layout
32
        $this->di->get("pageRender")->renderPage(["title" => "Kommentarssystem"]);
33
    }
34
35
36
    /**
37
     * Get ONE comment from an article.
38
     *
39
     * @param string $key for the article
0 ignored issues
show
Bug introduced by
There is no parameter named $key. 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...
40
     * @param int    $id for the comment id
41
     *
42
     * @return void
43
     */
44 2
    public function getComment($id)
45
    {
46 2
        $comment = $this->di->get("com")->getComment($id);
47 2
        return $comment;
48
    }
49
50
51
    /**
52
     * Get ONE comment for editing.
53
     *
54
     * @return void
55
     */
56 View Code Duplication
    public function getCommentToEdit()
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...
57
    {
58
        $id = $this->di->get("request")->getGet("id");
59
        // Get the comment from Model.
60
        $comment = $this->di->get("com")->getComment($id);
61
        // Add views to a specific region
62
        $this->di->get("view")->add("comment/edit", ["comment"=>$comment], "main");
63
        // Render a standard page using layout
64
        $this->di->get("pageRender")->renderPage([
65
            "title" => "Redigera kommentar",
66
        ]);
67
    }
68
69
70
    /**
71
     * Edit a comment.
72
     *
73
     * @return void
74
     */
75
    public function editComment()
76
    {
77
        // Get post-variables
78
        $post = $this->di->get("request")->getPost();
79
        // Instruct Model to edit comment:
80
        // Edited comment:
81
        $comment = [
82
            "user_id" => $post["user_id"],
83
            "name" => $post["name"],
84
            "email" => $post["email"],
85
            "comment" => $post["comment"],
86
            "id" => $post["id"]
87
        ];
88
        $this->di->get("com")->updateComment($post["id"], $comment);
89
        // Send user back to comment page.
90
        $url = $this->di->get("url")->create("comment");
91
        $this->di->get("response")->redirect($url);
92
    }
93
94
95
96
    /**
97
     * Post a comment, with name and email.
98
     *
99
     * @return void
100
     */
101 View Code Duplication
    public function postComment()
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...
102
    {
103
        // Catch post variables
104
        $post = $this->di->get("request")->getPost();
105
        // Instruct Model to add comment:
106
        $this->di->get("com")->addComment($post);
107
        // Send user back to comment page.
108
        $url = $this->di->get("url")->create("comment");
109
        $this->di->get("response")->redirect($url);
110
    }
111
112
113
    /**
114
     * Update old comment with new comment
115
     *
116
     * @param int       $id         id for comment
117
     * @param array     $comment    the comment-array (name, email, comment, id)
118
     *
119
     * @return void
120
     */
121 1
    public function updateComment($id, $comment)
122
    {
123 1
        $this->di->get("com")->updateComment($id, $comment);
124 1
    }
125
126
127
    /**
128
     * Delete comment with id
129
     *
130
     * @return void
131
     */
132 View Code Duplication
    public function deleteComment()
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...
133
    {
134
        // Get id-variable from request.
135
        $id = $this->di->get("request")->getGet("id");
136
        // Instruct Model to delete comment:
137
        $this->di->get("com")->deleteComment($id);
138
        // Send user back to comment page.
139
        $url = $this->di->get("url")->create("comment");
140
        $this->di->get("response")->redirect($url);
141
    }
142
}
143