Completed
Push — master ( d6d75c...5451f5 )
by Magnus
02:30
created

CommentController::newComment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 2.0393

Importance

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