EditCommentHandler.php
2.55 KB
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
<?php
namespace FootyRoom\App\Comment;
use FootyRoom\User\User;
use FootyRoom\Core\CoreException;
use FootyRoom\Core\Comment\CommentPolicy;
use Illuminate\Contracts\Events\Dispatcher;
use FootyRoom\Repositories\CommentRepository;
class EditCommentHandler
{
/**
* @var \FootyRoom\Core\Comment\CommentingService
*/
protected $commentingService;
/**
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;
/**
* @var \FootyRoom\Core\Comment\CommentPolicy
*/
protected $commentPolicy;
/**
* @var \FootyRoom\Repositories\CommentRepository
*/
protected $commentRepo;
/**
* Constructor.
*
* @param \FootyRoom\Repositories\CommentRepository $commentRepo
* @param \FootyRoom\App\Comment\CommentingService $commentingService
* @param \FootyRoom\Core\Comment\CommentPolicy $commentPolicy
* @param \Illuminate\Contracts\Events\Dispatcher $events
*/
public function __construct(
CommentRepository $commentRepo,
CommentingService $commentingService,
CommentPolicy $commentPolicy,
Dispatcher $events
) {
$this->commentRepo = $commentRepo;
$this->commentPolicy = $commentPolicy;
$this->commentingService = $commentingService;
$this->events = $events;
}
/**
* Edit comment.
*
* @param \FootyRoom\App\Comment\EditCommentCommand $command
* @param \FootyRoom\User\User $user
*/
public function handle(EditCommentCommand $command, User $user)
{
// Get original comment.
$comment = $this->commentRepo->findById($command->commentId);
if (!$comment) {
throw new CoreException('This comment does not exist. It may have been removed while you were editing it.');
}
// Authorize this edit.
$this->commentPolicy->canEdit($user, $comment->getAuthor()->getUserId(), $comment->getDiscussionId(), true);
$this->commentPolicy->canComment($user, true);
// Check comment for blacklisted words.
$this->commentingService->checkBlacklistedWords([$command->content]);
$newContent = $this->commentingService->formatComment($command->content, $command->images);
$comment->editContent($newContent, $newContent, $user);
// Save modified comment and a previous revision.
$this->commentRepo->updateContent($comment->getId(), $comment->getContent(), $comment->getContent());
foreach ($comment->releaseEvents() as $event) {
$this->events->dispatch($event);
}
}
}