Passed
Push — Comments ( f56c48...58658e )
by Stone
02:01
created

Comments::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Admin;
4
5
use App\Models\CommentModel;
6
use Core\AdminController;
7
use Core\Constant;
8
use Core\Container;
9
use Core\Traits\StringFunctions;
10
11
class Comments extends AdminController{
12
13
    use StringFunctions;
1 ignored issue
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Controllers\Admin\Comments.
Loading history...
14
15
    protected $siteConfig;
16
    protected $pagination;
17
18
    private $commentModel;
19
20
    public function __construct(Container $container)
21
    {
22
        $this->loadModules[] = 'SiteConfig';
23
        $this->loadModules[] = 'pagination';
24
        parent::__construct($container);
25
        $this->commentModel = new CommentModel($this->container);
26
    }
27
28
29
    public function viewComments(string $page = "page-1", int $linesPerPage = Constant::LIST_PER_PAGE)
30
    {
31
        $this->onlyAdmin();
32
33
        $totalComments = $this->commentModel->countComments();
34
        $pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage);
35
36
        if ($linesPerPage !== Constant::LIST_PER_PAGE) {
37
            $this->data['paginationPostsPerPage'] = $linesPerPage;
38
        }
39
40
        $this->data['pagination'] = $pagination;
41
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
42
        $this->data["comments"] = $this->commentModel->getCommentsList($pagination["offset"], $linesPerPage);
43
44
45
        $this->renderView('Admin/Comments');
46
    }
47
48
    public function pendingComments(string $page = "page-1", int $linesPerPage = Constant::LIST_PER_PAGE)
49
    {
50
        $this->onlyAdmin();
51
52
        $totalComments = $this->commentModel->countPendingComments();
53
        $pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage);
54
55
        if ($linesPerPage !== Constant::LIST_PER_PAGE) {
56
            $this->data['paginationPostsPerPage'] = $linesPerPage;
57
        }
58
59
        $this->data['pagination'] = $pagination;
60
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
61
        $this->data["comments"] = $this->commentModel->getPendingCommentsList($pagination["offset"], $linesPerPage);
62
        $this->data['pendingView'] = true;
63
64
        $this->renderView('Admin/Comments');
65
66
    }
67
68
    public function moderateComment(int $commentId)
69
    {
70
        $this->onlyAdmin();
71
72
        $this->data["comment"] = $this->commentModel->getCommentById($commentId);
73
74
        $this->renderView('Admin/ViewComment');
75
    }
76
77
    public function delete(int $commentId)
78
    {
79
        $this->onlyAdmin();
80
        $removedComment = $this->commentModel->delete($commentId);
81
82
        if ($removedComment) {
83
            $this->alertBox->setAlert("Comment  deleted");
84
        }
85
86
        $refererUrl = $this->request->getReferer();
87
        $baseUrl = $this->request->getBaseUrl();
88
        $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 App\Controllers\Admin\Co...::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

88
        $redirectUrl = $this->removeFromBeginning(/** @scrutinizer ignore-type */ $refererUrl, $baseUrl);
Loading history...
89
90
        $this->response->redirect($redirectUrl);
91
92
    }
93
}