ManageCommentsController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
dl 0
loc 63
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A destroy() 0 7 1
A approve() 0 7 1
A index() 0 14 2
1
<?php
2
3
namespace WebDevEtc\BlogEtc\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use Exception;
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Http\Request;
9
use WebDevEtc\BlogEtc\Helpers;
10
use WebDevEtc\BlogEtc\Middleware\UserCanManageBlogPosts;
11
use WebDevEtc\BlogEtc\Models\Comment;
12
use WebDevEtc\BlogEtc\Services\CommentsService;
13
14
/**
15
 * Class BlogEtcCommentsAdminController.
16
 */
17
class ManageCommentsController extends Controller
18
{
19
    /** @var CommentsService */
20
    private $service;
21
22
    /**
23
     * BlogEtcCommentsAdminController constructor.
24
     */
25
    public function __construct(CommentsService $service)
26
    {
27
        $this->middleware(UserCanManageBlogPosts::class);
28
        $this->service = $service;
29
    }
30
31
    /**
32
     * Show all comments (and show buttons with approve/delete).
33
     *
34
     * @return mixed
35
     */
36
    public function index(Request $request)
37
    {
38
        //TODO - use service
39
        $comments = Comment::withoutGlobalScopes()
40
            ->orderBy('created_at', 'desc')
41
            ->with('post');
42
43
        if ($request->get('waiting_for_approval')) {
44
            $comments->where('approved', false);
45
        }
46
47
        $comments = $comments->paginate(100);
48
49
        return view('blogetc_admin::comments.index', ['comments' => $comments]);
50
    }
51
52
    /**
53
     * Approve a comment.
54
     *
55
     * @param $blogCommentID
56
     */
57
    public function approve(int $blogCommentID): RedirectResponse
58
    {
59
        $this->service->approve($blogCommentID);
60
61
        Helpers::flashMessage('Approved comment!');
62
63
        return back();
64
    }
65
66
    /**
67
     * Delete a submitted comment.
68
     *
69
     * @param $blogCommentID
70
     *
71
     * @throws Exception
72
     */
73
    public function destroy(int $blogCommentID): RedirectResponse
74
    {
75
        $this->service->delete($blogCommentID);
76
77
        Helpers::flashMessage('Deleted comment!');
78
79
        return back();
80
    }
81
}
82