Completed
Push — master ( df362d...2da97c )
by Magnus
01:52
created

CommentController::newComment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 4.6796

Importance

Changes 0
Metric Value
dl 23
loc 23
ccs 2
cts 16
cp 0.125
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 1
crap 4.6796
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
    public function editComment($commentid)
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
        $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 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
        $title = "Create new post";
69
        $view = $this->di->get("view");
70
        $pageRender = $this->di->get("pageRender");
71
        $form = new CreatePostForm($this->di);
72
73
        $form->check();
74
75
        $data = [
76
            "form" => $form->getHTML(),
77
        ];
78
79
        $view->add("comment/addNewPost", $data);
80
81
        $pageRender->renderPage(["title" => $title]);
82
    }
83
84 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...
85
    {
86
        if ($this->di->get("session")->has("email")) {
87
            $title = "Create new comment";
88
            $view = $this->di->get("view");
89
            $pageRender = $this->di->get("pageRender");
90
            $form = new CreateCommentForm($this->di, $id);
91
92
            $form->check();
93
94
            $data = [
95
                "form" => $form->getHTML(),
96
            ];
97
98
            $view->add("comment/addNewComment", $data);
99
100
            $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
        $pageRender->renderPage(["title" => $title]);
139
    }
140
}
141