Passed
Push — master ( 627324...37a157 )
by Magnus
02:13
created

CommentController::editCommentComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
ccs 0
cts 10
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 2
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\Comment\CommentComments;
10
use \Radchasay\Comment\PostCategory;
11
use \Radchasay\User\User;
12
use \Radchasay\Comment\HTMLForm\CreatePostForm;
13
use \Radchasay\Comment\HTMLForm\CreateCommentForm;
14
use \Radchasay\Comment\HTMLForm\UpdateCommentForm;
15
use \Radchasay\Comment\HTMLForm\CreateCommentCommentForm;
16
use \Radchasay\Comment\HTMLForm\UpdateCommentCommentForm;
17
18
19
/**
20
 * CommentModel
21
 */
22
class CommentController implements InjectionAwareInterface
23
{
24
    use InjectionAwareTrait;
25
26
    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...
27
    {
28
        $comment = new Comment();
29
        $comment->setDb($this->di->get("db"));
30
31
32
        $commentComments = new CommentComments();
33
        $commentComments->setDb($this->di->get("db"));
34
35
        $test = $commentComments->getAllCommentFromComments([$commentId]);
36
37
        $commentComments->getNext();
38
39
        if (!empty($test)) {
40
            foreach ($test as $t) {
41
                $this->deleteCommentComment($t->idcommentc, true);
42
                $commentComments->getNext();
43
            }
44
        }
45
46
        $comment->delete("idcomment", $commentId);
47
        $createUrl = $this->di->get("url")->create("comment/viewAllPosts");
48
        $url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl;
49
        $this->di->get("response")->redirect($url);
50
    }
51
52 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...
53
    {
54
        $title = "Update comment";
55
        $view = $this->di->get("view");
56
        $pageRender = $this->di->get("pageRender");
57
        $form = new UpdateCommentForm($this->di, $commentid);
58
59
        $form->check();
60
61
        $data = [
62
            "form" => $form->getHTML(),
63
        ];
64
65
        $view->add("comment/editComment", $data);
66
67
        return $pageRender->renderPage(["title" => $title]);
68
    }
69
70
71 View Code Duplication
    public function editCommentComment($idcommentc) {
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...
72
        $title = "Update commentcomment";
73
        $view = $this->di->get("view");
74
        $pageRender = $this->di->get("pageRender");
75
        $form = new UpdateCommentCommentForm($this->di, $idcommentc);
76
77
        $form->check();
78
79
        $data = [
80
            "form" => $form->getHTML(),
81
        ];
82
83
        $view->add("comment/editComment", $data);
84
85
        return $pageRender->renderPage(["title" => $title]);
86
    }
87
88 1
    public function viewAllPosts()
89
    {
90
        $title = "Retrieve all posts";
91
        $view = $this->di->get("view");
92
        $pageRender = $this->di->get("pageRender");
93
        $post = new Post();
94
        $post->setDb($this->di->get("db"));
95
96
        $posts = $post->getAllPosts();
97
98
        $allPosts = [];
99
100 View Code Duplication
        foreach ($posts as $p) {
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...
101
            array_push($allPosts, [$p, $post->getTags([$p->postid])]);
102
        }
103
104
        $data = [
105
            "items" => $allPosts,
106 1
        ];
107
108
        $view->add("comment/viewAllPosts", $data);
109
110
        return $pageRender->renderPage(["title" => $title]);
111
    }
112
113 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...
114
    {
115 1
        $title = "Create new post";
116 1
        $view = $this->di->get("view");
117 1
        $pageRender = $this->di->get("pageRender");
118 1
        $form = new CreatePostForm($this->di);
119
120 1
        $form->check();
121
122
        $data = [
123 1
            "form" => $form->getHTML(),
124 1
        ];
125
126 1
        $view->add("comment/addNewPost", $data);
127
128 1
        return $pageRender->renderPage(["title" => $title]);
129
    }
130
131 1 View Code Duplication
    public function newComment($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...
132
    {
133 1
        if ($this->di->get("session")->has("email")) {
134 1
            $title = "Create new comment";
135 1
            $view = $this->di->get("view");
136 1
            $pageRender = $this->di->get("pageRender");
137 1
            $form = new CreateCommentForm($this->di, $id);
138
139 1
            $form->check();
140
141
            $data = [
142 1
                "form" => $form->getHTML(),
143 1
            ];
144
145 1
            $view->add("comment/addNewComment", $data);
146
147 1
            return $pageRender->renderPage(["title" => $title]);
148
        } else {
149
            $login = $this->di->get("url")->create("user/login");
150
            $this->di->get("response")->redirect($login);
151
            return false;
152
        }
153
    }
154
155
    public function postAndComments($id)
156
    {
157
        $title = "Retrieve one post with comments";
158
        $view = $this->di->get("view");
159
        $pageRender = $this->di->get("pageRender");
160
        $post = new Post();
161
        $post->setDb($this->di->get("db"));
162
        if ($post->checkId($id)) {
163
            if ($this->di->get("session")->has("email")) {
164
                $email = $this->di->get("session")->get("email");
165
                $user = new User();
166
                $user->setDb($this->di->get("db"));
167
                $permissions = $user->returnPermissions($email);
168
            } else {
169
                $permissions = "user";
170
            }
171
172
            $comment = new Comment();
173
            $comment->setDb($this->di->get("db"));
174
            $comments = $comment->getAllCommentsFromSpecificPost([$id]);
175
176
            $comment->getNext();
177
178
            $commentComments = new CommentComments();
179
            $commentComments->setDb($this->di->get("db"));
180
181
            $allComments = [];
182
183
            foreach ($comments as $comment) {
184
                array_push($allComments, [$comment,
185
                $commentComments->getAllCommentFromComments([$comment->idcomment])]);
186
                $commentComments->getNext();
187
            }
188
189
            $data = [
190
                "post" => $post->getPostInfo([$id]),
191
                "comments" => $allComments,
192
                "permissions" => $permissions,
193
                "tags" => $post->getTags([$id])
194
            ];
195
196
            $view->add("comment/onePostWithComment", $data);
197
198
            return $pageRender->renderPage(["title" => $title]);
199
        } else {
200
            $url = $this->di->get("url")->create("comment/viewAllPosts");
201
            $this->di->get("response")->redirect($url);
202
        }
203
    }
204
205 View Code Duplication
    public function newCommentComment($idcomment, $idpost)
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...
206
    {
207
        if ($this->di->get("session")->has("email")) {
208
            $title = "Create new comment";
209
            $view = $this->di->get("view");
210
            $pageRender = $this->di->get("pageRender");
211
            $form = new CreateCommentCommentForm($this->di, $idcomment, $idpost);
212
213
            $form->check();
214
215
            $data = [
216
                "form" => $form->getHTML(),
217
            ];
218
219
            $view->add("comment/addNewComment", $data);
220
221
            return $pageRender->renderPage(["title" => $title]);
222
        } else {
223
            $login = $this->di->get("url")->create("user/login");
224
            $this->di->get("response")->redirect($login);
225
            return false;
226
        }
227
    }
228
229
    public function deleteCommentComment($id, $nested = false)
0 ignored issues
show
Coding Style introduced by
deleteCommentComment 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...
230
    {
231
        if (!$nested) {
232
            $commentcomments = new CommentComments();
233
            $commentcomments->setDb($this->di->get("db"));
234
            $commentcomments->delete("idcommentc", $id);
235
            $createUrl = $this->di->get("url")->create("comment/viewAllPosts");
236
            $url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl;
237
            $this->di->get("response")->redirect($url);
238
        } else {
239
            $commentcomments = new CommentComments();
240
            $commentcomments->setDb($this->di->get("db"));
241
            $commentcomments->delete("idcommentc", $id);
242
        }
243
    }
244
245
246 View Code Duplication
    public function returnCatId($category)
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...
247
    {
248
        $cat = new PostCategory();
249
        $cat->setDb($this->di->get("db"));
250
        $id = $cat->getId($category);
251
        return $id;
252
    }
253
}
254