CommentController::deleteItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Alvo\Comment;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
use \Alvo\Comment\HTMLForm\AddCommentForm;
8
use \Alvo\Comment\HTMLForm\EditCommentForm;
9
use \Alvo\User\User;
10
11
class CommentController implements InjectionAwareInterface
12
{
13
    use InjectionAwareTrait;
14
15
16
17 1
    public function init()
18
    {
19 1
        $this->db = $this->di->get("db");
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20 1
        $this->view = $this->di->get("view");
0 ignored issues
show
Bug Best Practice introduced by
The property view does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21 1
        $this->pageRender = $this->di->get("pageRender");
0 ignored issues
show
Bug Best Practice introduced by
The property pageRender does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22 1
        $this->response = $this->di->get("response");
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
        // $this->comment = new Comment();
24 1
    }
25
26
27
28
    public function getIndex()
29
    {
30
        $title = "Comments";
31
32
        $comment = new Comment();
33
        $comment->setDb($this->di->get("db"));
34
        $posts = $comment->getAllComments() ? $comment->getAllComments() : [];
35
        // debug($posts);
36
37
        $user = $this->di->get("user");
38
        $user->setDb($this->db);
39
        $user = $user->getUser();
40
41
        $form = new AddCommentForm($this->di);
42
        $form->check();
43
44
        $this->view->add("comments/index", [
45
            "posts" => $posts,
46
            "form" => $form->getHTML(),
47
            "user" => $user
48
        ]);
49
50
        $this->pageRender->renderPage(["title" => $title]);
51
    }
52
53
    public function editItem($id)
54
    {
55
        $title = "Edit comment";
56
57
        // $user = $this->di->get("user");
58
        // $user->setDb($this->db);
59
        // $user = $user->getUser();
60
61
        $form = new EditCommentForm($this->di, $id);
62
        $form->check();
63
64
        $this->view->add("comments/edit", [
65
            "form" => $form->getHTML(),
66
            // "user" => $user
67
        ]);
68
69
        $this->pageRender->renderPage(["title" => $title]);
70
    }
71
72
    public function deleteItem($id)
73
    {
74
        $comment = new Comment();
75
        $comment->setDb($this->db);
76
        $comment->find("id", $id);
77
        $comment->deleted = date("Y-m-d H:i:s");
0 ignored issues
show
Bug introduced by
The property deleted does not seem to exist on Alvo\Comment\Comment.
Loading history...
78
        $comment->save();
79
80
        $this->response->redirect("comments");
81
    }
82
83
    // public function loginRequired()
84
    // {
85
        // $user = $this->di->get("user");
86
        // if (!$user->isLoggedIn()) {
87
        //     $this->response->redirect("user/login");
88
        // }
89
    // }
90
}
91
92
93
// TODO:
94
// Users CRUD
95