|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller\Comment; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Comment; |
|
6
|
|
|
use App\Entity\Trick; |
|
7
|
|
|
use App\Event\Comment\CommentCreatedEvent; |
|
8
|
|
|
use App\Exception\RedirectException; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CreateCommentController |
|
17
|
|
|
* @package App\Controller\Comment |
|
18
|
|
|
* @IsGranted("ROLE_USER") |
|
19
|
|
|
*/ |
|
20
|
|
|
class CreateCommentController extends AbstractController |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var EventDispatcherInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $dispatcher; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->dispatcher = $dispatcher; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @Route("/comment/add/{id}", name="comment.create", methods={"POST"}) |
|
36
|
|
|
*/ |
|
37
|
|
|
public function createComment(Trick $trick, Request $request) |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
$receivedComment = $request->request->get('comment'); |
|
41
|
|
|
|
|
42
|
|
|
if (!$this->isCsrfTokenValid('CommentForm', $receivedComment['_token'])) { |
|
43
|
|
|
throw new RedirectException($this->generateUrl('home'), 'Bad CSRF Token'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$comment = new Comment(); |
|
47
|
|
|
|
|
48
|
|
|
//Set the user to the current logged in user |
|
49
|
|
|
$comment->setUser($this->getUser()); |
|
50
|
|
|
$comment->setTrick($trick); |
|
51
|
|
|
$comment->setComment($receivedComment['comment']); |
|
52
|
|
|
|
|
53
|
|
|
$event = new CommentCreatedEvent($comment); |
|
54
|
|
|
$this->dispatcher->dispatch(CommentCreatedEvent::NAME, $event); |
|
55
|
|
|
|
|
56
|
|
|
return $this->redirectToRoute('trick.show', [ |
|
57
|
|
|
'id' => $trick->getId(), |
|
58
|
|
|
'slug' => $trick->getSlug(), |
|
59
|
|
|
'_fragment' => 'comment-'.$comment->getId(), |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
} |