Passed
Pull Request — master (#61)
by Stone
08:36 queued 05:08
created

Post   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A viewPost() 0 37 5
A addComment() 0 31 2
1
<?php
2
3
namespace App\Controllers;
4
5
use App\Models\CommentModel;
6
use App\Models\PostModel;
7
use App\Models\TagModel;
8
use Core\Constant;
9
use Core\Controller;
10
use Core\Container;
11
12
class Post extends Controller
13
{
14
15
    protected $siteConfig;
16
    protected $sendMail;
17
18
    private $commentModel;
19
    private $tagModel;
20
    private $postModel;
21
22
    public function __construct(Container $container)
23
    {
24
        $this->loadModules[] = 'SiteConfig';
25
        $this->loadModules[] = 'SendMail';
26
        parent::__construct($container);
27
        $this->commentModel = new CommentModel($this->container);
28
        $this->tagModel = new TagModel($this->container);
29
        $this->postModel = new PostModel($this->container);
30
31
    }
32
33
    /**
34
     * @param string $slug
35
     * @param string $page
36
     * @param int $linesPerPage
37
     * @throws \ReflectionException
38
     * @throws \Twig_Error_Loader
39
     * @throws \Twig_Error_Runtime
40
     * @throws \Twig_Error_Syntax
41
     */
42
    public function viewPost(string $slug, string $page = "page-1", int $linesPerPage = Constant::COMMENTS_PER_PAGE)
43
    {
44
45
        $postId = (int)$this->postModel->getPostIdFromSlug($slug);
46
47
        $posts = $this->postModel->getSinglePost($postId);
48
49
        if($posts === false)
1 ignored issue
show
introduced by
The condition $posts === false is always false.
Loading history...
50
        {
51
            throw new \Exception("Page no longer exists", "404");
52
        }
53
54
        //only admins can view unpublished posts
55
        if (!$posts->published) {
56
            if (!$this->auth->isAdmin()) {
57
                throw new \Exception("File does not exist", "404");
58
            }
59
            $this->alertBox->setAlert('This post is not yet published', 'warning');
60
        }
61
62
        $totalComments = $this->commentModel->countCommentsOnPost($postId);
63
        $pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage);
64
65
        if ($linesPerPage !== Constant::COMMENTS_PER_PAGE) {
66
            $this->data['paginationPostsPerPage'] = $linesPerPage;
67
        }
68
69
        $this->sendSessionVars();
70
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
71
        $this->data['post'] = $posts;
72
        $this->data['postTags'] = $this->tagModel->getTagsOnPost($postId);
73
        $this->data['navigation'] = $this->siteConfig->getMenu();
74
        $this->data["comments"] = $this->commentModel->getCommentsListOnPost($postId, $pagination["offset"],
75
            $linesPerPage);
76
        $this->data['pagination'] = $pagination;
77
78
        $this->renderView('post');
79
80
    }
81
82
    /**
83
     * Add a comment to the post
84
     * @throws \Exception
85
     */
86
    public function addComment()
87
    {
88
        $this->onlyPost();
89
        $this->onlyUser();
90
91
        //get the session userId
92
        $userId = (int)$this->session->get("userId");
93
        $comment = (string)$this->request->getData("newComment");
94
        $postId = (int)$this->request->getData("postId");
95
96
        //check if we are admin, Admins do not need moderation
97
        $admin = $this->session->get('user_role_level') >= Constant::ADMIN_LEVEL;
98
        $commentId = $this->commentModel->addComment($postId, $userId, $comment, $admin);
99
100
        if (!$admin) //if we are not an admin, send an email to alert and add an alertBox
101
        {
102
            $siteConfig = $this->siteConfig->getSiteConfig();
103
            $post = $this->postModel->getSinglePost($postId);
104
            $baseUrl = $this->request->getBaseUrl();
105
106
            $emailMessage = "<h1>New comment on post " . $post->title . "</a></h1>";
107
            $emailMessage .= "<p>Check it out <a href='" . $baseUrl . "admin/comments/moderate-comment/" . $commentId . "'>here</a> </p>";
108
109
            $this->sendMail->send($siteConfig["admin_email_address"], "New comment added", $emailMessage);
110
111
            $this->alertBox->setAlert("Your post will be published after moderation.");
112
        }
113
114
        $postSlug = $this->postModel->getPostSlugFromId($postId);
115
116
        $this->response->redirect("/post/view-post/" . $postSlug);
117
    }
118
}