Passed
Push — Comments ( 0e09d9...73e073 )
by Stone
02:13
created

Post::addComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
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 $pagination;
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[] = 'pagination';
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 $slug
35
     * @throws \Exception
36
     * @throws \ReflectionException
37
     * @throws \Twig_Error_Loader
38
     * @throws \Twig_Error_Runtime
39
     * @throws \Twig_Error_Syntax
40
     */
41
    public function viewPost(string $slug, string $page = "page-1", int $linesPerPage = Constant::COMMENTS_PER_PAGE)
42
    {
43
44
        //TODO Implement comment pagination
45
46
        $postId = $this->postModel->getPostIdFromSlug($slug);
47
48
        $posts = $this->postModel->getSinglePost($postId);
49
        //only admins can view unpublished posts
50
        if (!$posts->published) {
51
            if (!$this->auth->isAdmin()) {
52
                throw new \Exception("File does not exist", "404");
53
            }
54
            $this->alertBox->setAlert('This post is not yet published', 'warning');
55
        }
56
57
58
        $totalComments = $this->commentModel->countCommentsOnPost($postId);
59
        $pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage);
60
61
        if ($linesPerPage !== Constant::COMMENTS_PER_PAGE) {
62
            $this->data['paginationPostsPerPage'] = $linesPerPage;
63
        }
64
65
66
        $this->sendSessionVars();
67
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
68
        $this->data['post'] = $posts;
69
        $this->data['postTags'] = $this->tagModel->getTagsOnPost($postId);
70
        $this->data['navigation'] = $this->siteConfig->getMenu();
71
        $this->data["comments"] = $this->commentModel->getCommentsListOnPost($postId, $pagination["offset"], $linesPerPage);
72
        $this->data['pagination'] = $pagination;
73
74
        $this->renderView('post');
75
76
    }
77
78
    /**
79
     * Add a comment to the post
80
     * @throws \Exception
81
     */
82
    public function addComment()
83
    {
84
        $this->onlyPost();
85
        $this->onlyUser();
86
87
        //get the session userId
88
        $userId = $this->session->get("userId");
89
        $comment = $this->request->getData("newComment");
90
        $postId = $this->request->getData("postId");
91
92
93
        $this->commentModel->addComment($postId, $userId, $comment);
0 ignored issues
show
Bug introduced by
It seems like $postId can also be of type null; however, parameter $postId of App\Models\CommentModel::addComment() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        $this->commentModel->addComment(/** @scrutinizer ignore-type */ $postId, $userId, $comment);
Loading history...
Bug introduced by
It seems like $comment can also be of type null; however, parameter $message of App\Models\CommentModel::addComment() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        $this->commentModel->addComment($postId, $userId, /** @scrutinizer ignore-type */ $comment);
Loading history...
Bug introduced by
It seems like $userId can also be of type null; however, parameter $userId of App\Models\CommentModel::addComment() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        $this->commentModel->addComment($postId, /** @scrutinizer ignore-type */ $userId, $comment);
Loading history...
94
95
        $refererUrl = $this->request->getReferer();
96
        $baseUrl = $this->request->getBaseUrl();
97
        $redirectUrl = $this->removeFromBeginning($refererUrl, $baseUrl);
0 ignored issues
show
Bug introduced by
It seems like $refererUrl can also be of type null; however, parameter $string of Core\Controller::removeFromBeginning() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $redirectUrl = $this->removeFromBeginning(/** @scrutinizer ignore-type */ $refererUrl, $baseUrl);
Loading history...
98
99
        $this->alertBox->setAlert("Your post will be published after moderation.");
100
101
        $this->response->redirect($redirectUrl);
102
    }
103
}