Passed
Push — master ( 3daffa...5c6602 )
by Emil
01:34
created

CommentController   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 277
Duplicated Lines 2.17 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 25.17%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 2
dl 6
loc 277
ccs 38
cts 151
cp 0.2517
rs 8.2857
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 1
B showComments() 0 31 4
B replyComment() 0 40 5
B editComment() 3 42 6
B deleteComment() 3 35 6
B voteComment() 0 20 5
A getComments() 0 11 2
A sortBy() 0 6 2
A buildCommentTree() 0 16 3
B sortBranchComments() 0 22 5

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 Emsa\Comment;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
use \LRC\Form\ModelForm as Modelform;
8
9
/**
10
 * A controller for the comment system.
11
 */
12
class CommentController implements InjectionAwareInterface
13
{
14
    use InjectionAwareTrait;
15
16
17
18
    /**
19
     * @var \LRC\Repository\SoftDbRepository  Comment repository.
20
     */
21
    private $comments;
22
23
24
25
    /**
26
     * Configuration.
27
     */
28 2
    public function init()
29
    {
30 2
        $commentRepository = $this->di->manager->createRepository(Comment::class, [
31 2
            'db' => $this->di->db,
32 2
            'type' => 'db-soft',
33 2
            'table' => 'rv1_Comment'
34
        ]);
35 2
        $this->comments = $commentRepository;
36 2
        return $commentRepository;
37
    }
38
39
40
41
    public function showComments($postid)
42
    {
43
        $loggedInUser = $this->di->userController->getLoggedInUserId();
44
45
        $newForm = new ModelForm('new-comment-form', Comment::class);
46
47
        if ($this->di->request->getMethod() == 'POST' && $loggedInUser) {
48
            $comment = $newForm->populateModel();
49
            $comment->user = $loggedInUser;
50
            $newForm->validate();
51
            if ($newForm->isValid()) {
52
                $this->comments->save($comment);
53
                $this->di->response->redirect("comment/$postid#{$comment->id}");
54
            }
55
        }
56
57
        $comments = $this->getComments($postid, $loggedInUser);
58
        $sortBy = $this->sortBy();
59
60
        $viewData = [
61
            "comments" => $this->buildCommentTree($comments, $sortBy),
62
            "textfilter" => $this->di->textfilter,
63
            "postid" => $postid,
64
            "action" => "",
65
            "actionID" => "",
66
            "newForm" => $newForm,
67
            "isLoggedIn" => $loggedInUser
68
        ];
69
70
        $this->di->view->add("comment/comment-section", $viewData, "main", 2);
71
    }
72
73
74
75
    public function replyComment($postid)
76
    {
77
        $loggedInUser = $this->di->userController->getLoggedInUserId();
78
        if (is_null($loggedInUser)) {
79
            $this->di->response->redirect("comment/$postid");
80
        }
81
82
        $actionID = (int)$this->di->request->getGet("id");
83
        if (!$this->comments->findSoft('id', $actionID)) {
84
            $this->di->response->redirect("comment/$postid");
85
        }
86
87
        $replyForm = new ModelForm('reply-comment-form', Comment::class);
88
89
        if ($this->di->request->getMethod() == 'POST') {
90
            $comment = $replyForm->populateModel();
91
            $comment->user = $loggedInUser;
92
            $replyForm->validate();
93
            if ($replyForm->isValid()) {
94
                $this->comments->save($comment);
95
                $this->di->response->redirect("comment/$postid#{$comment->id}");
96
            }
97
        }
98
99
        $comments = $this->getComments($postid, $loggedInUser);
100
        $sortBy = $this->sortBy();
101
102
        $viewData = [
103
            "comments" => $this->buildCommentTree($comments, $sortBy),
104
            "textfilter" => $this->di->textfilter,
105
            "postid" => $postid,
106
            "action" => "reply",
107
            "actionID" => $actionID,
108
            "newForm" => new ModelForm('new-comment-form', Comment::class),
109
            "replyForm" => $replyForm,
110
            "isLoggedIn" => $loggedInUser
111
        ];
112
113
        $this->di->view->add("comment/comment-section", $viewData, "main", 2);
114
    }
115
116
117
118
    public function editComment($postid)
119
    {
120
        $actionID = (int)$this->di->request->getGet("id");
121
        $currentComment = $this->comments->findSoft('id', $actionID);
122
        if (!$currentComment) {
123
            $this->di->response->redirect("comment/$postid");
124
        }
125
126
        $loggedInUser = $this->di->userController->getLoggedInUserId();
127 View Code Duplication
        if ($loggedInUser != $currentComment->user && !$this->di->session->has("admin")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128
            $this->di->response->redirect("comment/$postid");
129
        }
130
131
        $editForm = new ModelForm('edit-comment-form', $currentComment);
132
133
        if ($this->di->request->getMethod() == 'POST') {
134
            $comment = $editForm->populateModel(null, ['id', 'post_id', 'parent_id']);
135
            //Prevent edited column from being set to NULL
136
            unset($comment->edited);
137
            $editForm->validate();
138
            if ($editForm->isValid()) {
139
                $this->comments->save($comment);
140
                $this->di->response->redirect("comment/$postid#{$comment->id}");
141
            }
142
        }
143
144
        $comments = $this->getComments($postid, $loggedInUser);
145
        $sortBy = $this->sortBy();
146
147
        $viewData = [
148
            "comments" => $this->buildCommentTree($comments, $sortBy),
149
            "textfilter" => $this->di->textfilter,
150
            "postid" => $postid,
151
            "action" => "edit",
152
            "actionID" => $actionID,
153
            "newForm" => new ModelForm('new-comment-form', Comment::class),
154
            "editForm" => $editForm,
155
            "isLoggedIn" => $loggedInUser
156
        ];
157
158
        $this->di->view->add("comment/comment-section", $viewData, "main", 2);
159
    }
160
161
162
163
    public function deleteComment($postid)
164
    {
165
        $actionID = (int)$this->di->request->getGet("id");
166
        $currentComment = $this->comments->findSoft('id', $actionID);
167
        if (!$currentComment) {
168
            $this->di->response->redirect("comment/$postid");
169
        }
170
171
        $loggedInUser = $this->di->userController->getLoggedInUserId();
172 View Code Duplication
        if ($loggedInUser != $currentComment->user && !$this->di->session->has("admin")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
173
            $this->di->response->redirect("comment/$postid");
174
        }
175
176
        if ($this->di->request->getMethod() == 'POST') {
177
            if ($this->di->request->getPost('delete') == 'delete') {
178
                $this->comments->deleteSoft($currentComment);
179
            }
180
            $this->di->response->redirect("comment/$postid#{$currentComment->id}");
181
        }
182
183
        $comments = $this->getComments($postid, $loggedInUser);
184
        $sortBy = $this->sortBy();
185
186
        $viewData = [
187
            "comments" => $this->buildCommentTree($comments, $sortBy),
188
            "textfilter" => $this->di->textfilter,
189
            "postid" => $postid,
190
            "action" => "delete",
191
            "actionID" => $actionID,
192
            "newForm" => new ModelForm('new-comment-form', Comment::class),
193
            "isLoggedIn" => $loggedInUser
194
        ];
195
196
        $this->di->view->add("comment/comment-section", $viewData, "main", 2);
197
    }
198
199
200
201
    public function voteComment($postid)
202
    {
203
        if (is_null($this->di->userController->getLoggedInUserId())) {
204
            $this->di->response->redirect("comment/$postid");
205
        }
206
207
        $actionID = (int)$this->di->request->getGet("id");
208
        $currentComment = $this->comments->findSoft('id', $actionID);
209
        if (!$currentComment) {
210
            $this->di->response->redirect("comment/$postid");
211
        }
212
213
        if ($this->di->request->getPost("upvote")) {
214
            $currentComment->upvote += 1;
215
        } elseif ($this->di->request->getPost("downvote")) {
216
            $currentComment->downvote += 1;
217
        }
218
        $this->comments->save($currentComment);
219
        $this->di->response->redirect("comment/$postid#{$currentComment->id}");
220
    }
221
222
223
224 1
    public function getComments($postid, $loggedInUser)
225
    {
226 1
        $comments = $this->comments->getAll('post_id = ?', [$postid]);
227
228 1
        foreach ($comments as $comment) {
229 1
            $comment->isUserOwner = ($loggedInUser == $comment->userObject->id);
230 1
            $comment->isUserAdmin = $this->di->session->has("admin");
231
        }
232
233 1
        return $comments;
234
    }
235
236
237
238 1
    public function sortBy()
239
    {
240 1
        $sortRequest = $this->di->request->getGet("sort");
241 1
        $sortRules = ["best", "old", "new"];
242 1
        return in_array($sortRequest, $sortRules) ? $sortRequest : "best";
243
    }
244
245
246
247 1
    public function buildCommentTree(array &$elements, $sortBy, $parentId = 0)
248
    {
249 1
        $branch = array();
250
251 1
        foreach ($elements as $element) {
252 1
            if ($element->parent_id == $parentId) {
253 1
                $element->children = $this->buildCommentTree($elements, $sortBy, $element->id);
254
                // $children = $this->buildCommentTree($elements, $sortBy, $element->id);
255
                // if (!empty($children)) {
256
                //     $element->children = $children;
257
                // }
258 1
                $branch[$element->id] = $element;
259
            }
260
        }
261 1
        return $this->sortBranchComments($branch, $sortBy);
262
    }
263
264
265
266 2
    public function sortBranchComments(array &$branch, $sortBy = "best")
267
    {
268 2
        $sortOrder = SORT_DESC;
269 2
        $sortArray = array();
270 2
        foreach ($branch as $key => $comment) {
271
            switch ($sortBy) {
272 2
                case 'old':
273 1
                    $sortOrder = SORT_ASC;
274
                    //Intentional fall through
275 2
                case 'new':
276 1
                    $sortArray[$key] = $comment->created;
277 1
                    break;
278 2
                case 'best':
279
                    //Intentional fall through
280
                default:
281 2
                    $sortArray[$key] = ($comment->upvote - $comment->downvote);
282 2
                    break;
283
            }
284
        }
285 2
        array_multisort($sortArray, $sortOrder, $branch);
286 2
        return $branch;
287
    }
288
}
289