EditCommentController::editCommentSubmit()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 26
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 2
1
<?php
2
3
namespace App\Controller\Comment;
4
5
use App\Entity\Comment;
6
use App\Event\Comment\CommentEditedEvent;
7
use App\Exception\RedirectException;
8
use App\Form\CommentFormType;
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\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * Class EditCommentController
18
 * @package App\Controller\Comment
19
 * @IsGranted("ROLE_USER")
20
 */
21
class EditCommentController extends AbstractController
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
     * @Route("/comment/edit/{id}", name="comment.edit", methods={"GET"})
35
     */
36
    public function editComment(Comment $comment, Request $request)
37
    {
38
        $this->checkSecurity($comment);
39
        $commentForm = $this->createForm(CommentFormType::class, $comment, [
40
            'save_button_label' => 'Update',
41
        ]);
42
43
        if($request->isXmlHttpRequest()){
44
            $render = $this->renderView('comment/_comment-form.html.twig', [
45
                'comment' => $comment,
46
                'commentForm' => $commentForm->createView(),
47
                'actionPath' => $this->generateUrl('comment.edit', ['id' => $comment->getId()]),
48
            ]);
49
            $jsonResponse = array(
50
                'render' => $render,
51
            );
52
53
            return new JsonResponse($jsonResponse);
54
        }
55
        return $this->render('comment/edit.html.twig', [
56
            'comment' => $comment,
57
            'commentForm' => $commentForm->createView(),
58
        ]);
59
60
    }
61
62
    /**
63
     * @Route("/comment/edit/{id}", name="comment.submit", methods={"POST"})
64
     */
65
    public function editCommentSubmit(Comment $comment, Request $request)
66
    {
67
        $this->checkSecurity($comment);
68
69
        $form = $this->createForm(CommentFormType::class, $comment, [
70
            'save_button_label' => 'Update',
71
        ]);
72
73
        $form->handleRequest($request);
74
75
        if ($form->isSubmitted() && $form->isValid()) {
76
77
            $event = new CommentEditedEvent($comment);
78
            $this->dispatcher->dispatch(CommentEditedEvent::NAME, $event);
79
80
            return $this->redirectToRoute('trick.show', [
81
                'id' => $comment->getTrick()->getId(),
82
                'slug' => $comment->getTrick()->getSlug(),
83
                '_fragment' => 'comment-'.$comment->getId(),
84
            ]);
85
        }
86
87
        //This should never be called unless we have some strange error
88
        return $this->render('comment/edit.html.twig', [
89
            'comment' => $comment,
90
            'commentForm' => $form->createView(),
91
        ]);
92
93
    }
94
95
    /**
96
     * @param Comment $comment
97
     * Checks if the user is admin or author of the comment.
98
     * Thows a redirect to the trick show page
99
     */
100
    private function checkSecurity(Comment $comment){
101
        if(!($this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() === $comment->getUser()->getId()))
102
        {
103
            Throw new RedirectException($this->generateUrl('trick.show', ['id'=> $comment->getTrick()->getId(), 'slug'=> $comment->getTrick()->getSlug()]),"You are not allowed to edit this comment");
104
        }
105
    }
106
107
108
}