PostController::indexActionGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Lyco\Post;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Lyco\Post\HTMLForm\CreateForm;
8
use Lyco\Post\HTMLForm\UpdateForm;
9
use Lyco\Post\Post;
10
use Lyco\User\User;
11
use Lyco\Tag\Tag;
12
use Lyco\Comment\Comment;
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 PostController implements ContainerInjectableInterface
23
{
24
    use ContainerInjectableTrait;
25
    /**
26
     * Show all items.
27
     *
28
     * @return object as a response object
29
     */
30
    public function indexActionGet() : object
31
    {
32
        $page = $this->di->get("page");
33
        $post = new Post();
34
        $post->setDb($this->di->get("dbqb"));
35
36
        $page->add("post/crud/view-all", [
37
            "items" => $post->findAll(),
38
        ]);
39
40
        return $page->render([
41
            "title" => "A collection of items",
42
        ]);
43
    }
44
45
46
47
    /**
48
     * Handler with form to create a new item.
49
     *
50
     * @return object as a response object
51
     */
52
    public function createAction() : object
53
    {
54
        if (!$this->di->get("session")->has("userId")) {
55
            $this->di->get("response")->redirect("user/login");
56
        }
57
        $page = $this->di->get("page");
58
        $form = new CreateForm($this->di);
59
        $form->check();
60
61
        $page->add("post/crud/create", [
62
            "form" => $form->getHTML(),
63
        ]);
64
65
        return $page->render([
66
            "title" => "Create a post",
67
        ]);
68
    }
69
70
    /**
71
     * Handler with form to update an item.
72
     *
73
     * @param int $id the id to update.
74
     *
75
     * @return object as a response object
76
     */
77
    public function updateAction(int $id) : object
78
    {
79
        $page = $this->di->get("page");
80
        $form = new UpdateForm($this->di, $id);
81
        $form->check();
82
83
        $page->add("post/crud/update", [
84
            "form" => $form->getHTML(),
85
        ]);
86
87
        return $page->render([
88
            "title" => "Update a post",
89
        ]);
90
    }
91
92
    /**
93
     * Handler with form to view an item.
94
     *
95
     * @param int $id the id to view.
96
     *
97
     * @return object as a response object
98
     */
99
     public function viewAction(int $id) : object
100
     {
101
         $page = $this->di->get("page");
102
103
         // posts
104
         $post = new Post();
105
         $post->setDb($this->di->get("dbqb"));
106
         $posts = $post->find("postId", $id);
107
108
         // tags
109
         $tag = new Tag();
110
         $tag->setDb($this->di->get("dbqb"));
111
         $tags = $tag->findAllTagsWhere("post.postId", $id);
112
113
         // comments
114
         $comment = new Comment();
115
         $comment->setDb($this->di->get("dbqb"));
116
         $comments = $comment->findAllCommentsWhere("postId", $id);
117
118
         $data = [
119
             "post" => $posts,
120
             "comments" => $comments,
121
             "tags" => $tags,
122
             "userId" => $this->di->get("session")->get("userId"),
123
             "get" => $_GET
124
         ];
125
126
         $page->add("post/crud/view-post", $data);
127
128
         return $page->render([
129
             "title" => "Showing post",
130
         ]);
131
     }
132
}
133