CommentController::updateAction()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 26
ccs 0
cts 15
cp 0
crap 12
rs 9.7998
1
<?php
2
3
namespace Lyco\Comment;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Lyco\Comment\HTMLForm\CreateForm;
8
use Lyco\Comment\HTMLForm\UpdateForm;
9
use Lyco\Post\Post;
10
use Lyco\User\User;
11
use Lyco\Tag\Tag;
12
use \Michelf\MarkdownExtra;
13
14
15
// use Anax\Route\Exception\ForbiddenException;
16
// use Anax\Route\Exception\NotFoundException;
17
// use Anax\Route\Exception\InternalErrorException;
18
19
/**
20
 * A sample controller to show how a controller class can be implemented.
21
 */
22
class CommentController implements ContainerInjectableInterface
23
{
24
    use ContainerInjectableTrait;
25
26
    /**
27
     * @var $data description
28
     */
29
30
    /**
31
     * Handler with form to create a new item.
32
     *
33
     * @return object as a response object
34
     */
35
     public function createAction(int $id) : object
36
     {
37
         $userId = $this->di->get("session")->get("userId");
38
         if (!$userId) {
39
             $this->di->get("response")->redirect("user/login");
40
         }
41
42
         //Form
43
         $form = new CreateForm($this->di, $id);
44
         $form->check();
45
46
         //Post
47
         $post = new Post();
48
         $post->setDb($this->di->get("dbqb"));
49
50
         //Post
51
         $comment = new Comment();
52
         $comment->setDb($this->di->get("dbqb"));
53
54
         // tags
55
         $tag = new Tag();
56
         $tag->setDb($this->di->get("dbqb"));
57
         $tags = $tag->findAllTagsWhere("post.postId", $id);
58
59
60
         $data = [
61
             "comments" => $comment->findAllCommentsWhere("postId", $id),
62
             "post" => $post->find("postId", $id),
63
             "userId" => $userId,
64
             "tags" => $tags,
65
             "filter" => new MarkdownExtra()
66
         ];
67
68
         //Page
69
         $page = $this->di->get("page");
70
         $page->add("post/crud/view-post", $data);
71
72
         $page->add("post/crud/create", [
73
             "form" => $form->getHTML(),
74
         ]);
75
76
         return $page->render([
77
             "title" => "comment",
78
         ]);
79
     }
80
81
82
    /**
83
     * Handler with form to update an item.
84
     *
85
     * @param int $id the id to update.
86
     *
87
     * @return object as a response object
88
     */
89
     public function updateAction(int $id) : object
90
     {
91
         $userId = $this->di->get("session")->get("userId");
92
93
         //Comment
94
         $comment = new Comment();
95
         $comment->setDb($this->di->get("dbqb"));
96
         $comment->find("commentId", $id);
97
98
         if (!$userId || ($comment->id != $userId)) {
99
             $this->di->get("response")->redirect("user/login");
100
         }
101
102
         //Form
103
         $form = new UpdateForm($this->di, $id);
104
         $form->check();
105
106
         //Page
107
         $page = $this->di->get("page");
108
         $page->add("post/crud/update", [
109
             "form" => $form->getHTML(),
110
             "type" => "comment"
111
         ]);
112
113
         return $page->render([
114
             "title" => "Edit comment",
115
         ]);
116
     }
117
}
118