Passed
Push — master ( 2da97c...99f011 )
by Magnus
02:14
created

CommentController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 124
Duplicated Lines 40.32 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 42.67%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 50
loc 124
ccs 32
cts 75
cp 0.4267
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteComment() 0 9 2
A viewAllPosts() 16 16 1
A editComment() 17 17 1
A newPost() 17 17 1
A newComment() 0 23 2
B postAndComments() 0 32 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Radchasay\Comment;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
use \Radchasay\Comment\Post;
8
use \Radchasay\Comment\Comment;
9
use \Radchasay\User\User;
10
use \Radchasay\Comment\HTMLForm\CreatePostForm;
11
use \Radchasay\Comment\HTMLForm\CreateCommentForm;
12
use \Radchasay\Comment\HTMLForm\UpdateCommentForm;
13
14
/**
15
 * CommentModel
16
 */
17
class CommentController implements InjectionAwareInterface
18
{
19
    use InjectionAwareTrait;
20
21
    public function deleteComment($commentId)
0 ignored issues
show
Coding Style introduced by
deleteComment uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
22
    {
23
        $comment = new Comment();
24
        $comment->setDb($this->di->get("db"));
25
        $comment->delete("idcomment", $commentId);
26
        $createUrl = $this->di->get("url")->create("comment/viewAllPosts");
27
        $url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl;
28
        $this->di->get("response")->redirect($url);
29
    }
30
31 View Code Duplication
    public function editComment($commentid)
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...
32
    {
33
        $title = "Update comment";
34
        $view = $this->di->get("view");
35
        $pageRender = $this->di->get("pageRender");
36
        $form = new UpdateCommentForm($this->di, $commentid);
37
38
        $form->check();
39
40
        $data = [
41
            "form" => $form->getHTML(),
42
        ];
43
44
        $view->add("comment/editComment", $data);
45
46
        return $pageRender->renderPage(["title" => $title]);
47
    }
48
49 1 View Code Duplication
    public function viewAllPosts()
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...
50
    {
51 1
        $title = "Retrieve all posts";
52 1
        $view = $this->di->get("view");
53 1
        $pageRender = $this->di->get("pageRender");
54 1
        $post = new Post();
55 1
        $post->setDb($this->di->get("db"));
56
57
        $data = [
58 1
            "items" => $post->findAll(),
59 1
        ];
60
61 1
        $view->add("comment/viewAllPosts", $data);
62
63 1
        return $pageRender->renderPage(["title" => $title]);
64
    }
65
66 1 View Code Duplication
    public function newPost()
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...
67
    {
68 1
        $title = "Create new post";
69 1
        $view = $this->di->get("view");
70 1
        $pageRender = $this->di->get("pageRender");
71 1
        $form = new CreatePostForm($this->di);
72
73 1
        $form->check();
74
75
        $data = [
76 1
            "form" => $form->getHTML(),
77 1
        ];
78
79 1
        $view->add("comment/addNewPost", $data);
80
81 1
        return $pageRender->renderPage(["title" => $title]);
82
    }
83
84 1
    public function newComment($id)
85
    {
86 1
        if ($this->di->get("session")->has("email")) {
87 1
            $title = "Create new comment";
88 1
            $view = $this->di->get("view");
89 1
            $pageRender = $this->di->get("pageRender");
90 1
            $form = new CreateCommentForm($this->di, $id);
91
92 1
            $form->check();
93
94
            $data = [
95 1
                "form" => $form->getHTML(),
96 1
            ];
97
98 1
            $view->add("comment/addNewComment", $data);
99
100 1
            return $pageRender->renderPage(["title" => $title]);
101
        } else {
102
            $login = $this->di->get("url")->create("user/login");
103
            $this->di->get("response")->redirect($login);
104
            return false;
105
        }
106 1
    }
107
108
    public function postAndComments($id)
109
    {
110
        $title = "Retrieve one post with comments";
111
        $view = $this->di->get("view");
112
        $pageRender = $this->di->get("pageRender");
113
        $post = new Post();
114
        $post->setDb($this->di->get("db"));
115
        if ($this->di->get("session")->has("email")) {
116
            $user = new User();
117
            $user->setDb($this->di->get("db"));
118
            $email = $this->di->get("session")->get("email");
119
            $userInfo = $user->find("email", $email);
120
            $permissions = $userInfo->permissions;
121
        } else {
122
            $permissions = "user";
123
        }
124
125
        $comment = new Comment();
126
        $comment->setDb($this->di->get("db"));
127
        $sql = "Call CheckComment(?)";
128
129
130
        $data = [
131
            "post" => $post->find("id", $id),
132
            "comments" => $comment->getAllCommentsFromSpecificPost($sql, [$id]),
133
            "permissions" => $permissions
134
        ];
135
136
        $view->add("comment/onePostWithComment", $data);
137
138
        return $pageRender->renderPage(["title" => $title]);
139
    }
140
}
141