Completed
Push — master ( 14a844...cfe0e1 )
by Fèvre
17s queued 17s
created

CommentController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
c 0
b 0
f 0
dl 0
loc 98
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 25 2
A show() 0 24 2
A delete() 0 18 2
1
<?php
2
namespace Xetaravel\Http\Controllers\Blog;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Xetaravel\Events\Badges\CommentEvent;
8
use Xetaravel\Http\Controllers\Controller;
9
use Xetaio\Mentions\Parser\MentionParser;
10
use Xetaravel\Models\Article;
11
use Xetaravel\Models\Comment;
12
use Xetaravel\Models\Repositories\CommentRepository;
13
use Xetaravel\Models\User;
14
use Xetaravel\Models\Validators\CommentValidator;
15
16
class CommentController extends Controller
17
{
18
    /**
19
     * Create a comment for an article.
20
     *
21
     * @param \Illuminate\Http\Request $request
22
     *
23
     * @return \Illuminate\Http\RedirectResponse
24
     */
25
    public function create(Request $request): RedirectResponse
26
    {
27
        Article::findOrFail($request->article_id);
28
29
        if (Comment::isFlooding('xetaravel.flood.blog.comment')) {
30
            return back()
31
                ->withInput()
32
                ->with('danger', 'Wow, keep calm bro, and try to not flood !');
33
        }
34
35
        CommentValidator::create($request->all())->validate();
36
        $comment = CommentRepository::create($request->all());
37
38
        $parser = new MentionParser($comment);
39
        $content = $parser->parse($comment->content);
40
41
        $comment->content = $content;
42
        $comment->save();
43
44
        // We must find the user else we won't see the updated comment_count.
45
        event(new CommentEvent(User::find(Auth::id())));
0 ignored issues
show
Bug introduced by
It seems like Xetaravel\Models\User::f...ort\Facades\Auth::id()) can also be of type boolean; however, parameter $user of Xetaravel\Events\Badges\...entEvent::__construct() does only seem to accept Xetaravel\Models\User, 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

45
        event(new CommentEvent(/** @scrutinizer ignore-type */ User::find(Auth::id())));
Loading history...
46
47
        return redirect()
48
            ->route('blog.comment.show', ['id' => $comment->getKey()])
49
            ->with('success', 'Your comment has been posted successfully !');
50
    }
51
52
    /**
53
     * Redirect an user to an article, page and comment.
54
     *
55
     * @param \Illuminate\Http\Request $request
56
     * @param int $id The ID of the comment.
57
     *
58
     * @return \Illuminate\Http\RedirectResponse
59
     */
60
    public function show(Request $request, int $id): RedirectResponse
61
    {
62
        $comment = Comment::findOrFail($id);
63
64
        $commentsBefore = Comment::where([
65
            ['article_id', $comment->article_id],
66
            ['created_at', '<', $comment->created_at]
67
        ])->count();
68
69
        $commentsPerPage = config('xetaravel.pagination.blog.comment_per_page');
70
71
        $page = floor($commentsBefore / $commentsPerPage) + 1;
72
        $page = ($page > 1) ? $page : 1;
73
74
        $request->session()->keep(['primary', 'danger', 'warning', 'success', 'info']);
75
76
        return redirect()
77
            ->route(
78
                'blog.article.show',
79
                [
80
                    'slug' => $comment->article->slug,
81
                    'id' => $comment->article->id,
82
                    'page' => $page,
83
                    '#comment-' . $comment->getKey()
84
                ]
85
            );
86
    }
87
88
    /**
89
     * Handle a delete action for the comment.
90
     *
91
     * @param \Illuminate\Http\Request $request
92
     * @param int $id
93
     *
94
     * @return \Illuminate\Http\RedirectResponse
95
     */
96
    public function delete(int $id): RedirectResponse
97
    {
98
        $comment = Comment::findOrFail($id);
99
100
        $this->authorize('delete', $comment);
101
102
        if ($comment->delete()) {
103
            return redirect()
104
                ->route(
105
                    'blog.article.show',
106
                    ['id' => $comment->article->getKey(), 'slug' => $comment->article->slug]
107
                )
108
                ->with('success', 'This comment has been deleted successfully !');
109
        }
110
111
        return redirect()
112
            ->route('blog.comment.show', ['id' => $comment->getKey()])
113
            ->with('danger', 'An error occurred while deleting this comment !');
114
    }
115
}
116