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