-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathCommentsController.php
executable file
·88 lines (76 loc) · 2.6 KB
/
CommentsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace WebDevEtc\BlogEtc\Controllers;
use App\Http\Controllers\Controller;
use Auth;
use Gate;
use Illuminate\Http\Response;
use RuntimeException;
use WebDevEtc\BlogEtc\Gates\GateTypes;
use WebDevEtc\BlogEtc\Requests\AddNewCommentRequest;
use WebDevEtc\BlogEtc\Services\CaptchaService;
use WebDevEtc\BlogEtc\Services\CommentsService;
use WebDevEtc\BlogEtc\Services\PostsService;
/**
* Class BlogEtcCommentWriterController.
*/
class CommentsController extends Controller
{
/** @var PostsService */
private $postsService;
/** @var CommentsService */
private $commentsService;
/** @var CaptchaService */
private $captchaService;
/**
* BlogEtcCommentWriterController constructor.
*
* Note: The comment itself and the form are not in this controller but are sent as part of the
* main PostsController::show() response.
*
* This class deals only with the storing of new comments.
*/
public function __construct(
PostsService $postsService,
CommentsService $commentsService,
CaptchaService $captchaService
) {
$this->postsService = $postsService;
$this->commentsService = $commentsService;
$this->captchaService = $captchaService;
}
/**
* @deprecated - use store instead
*/
public function addNewComment(AddNewCommentRequest $request, $slug)
{
return $this->store($request, $slug);
}
/**
* Let a guest (or logged in user) submit a new comment for a blog post.
*/
public function store(AddNewCommentRequest $request, $slug)
{
if (CommentsService::COMMENT_TYPE_BUILT_IN !== config('blogetc.comments.type_of_comments_to_show')) {
throw new RuntimeException('Built in comments are disabled');
}
if (Gate::denies(GateTypes::ADD_COMMENT)) {
abort(Response::HTTP_FORBIDDEN, 'Unable to add comments');
}
$blogPost = $this->postsService->repository()->findBySlug($slug);
$captcha = $this->captchaService->getCaptchaObject();
if ($captcha && method_exists($captcha, 'runCaptchaBeforeAddingComment')) {
$captcha->runCaptchaBeforeAddingComment($request, $blogPost);
}
$comment = $this->commentsService->create(
$blogPost,
$request->validated(),
$request->ip(),
(int) Auth::id()
);
return response()->view('blogetc::saved_comment', [
'captcha' => $captcha,
'blog_post' => $blogPost,
'new_comment' => $comment,
])->setStatusCode(Response::HTTP_CREATED);
}
}