Test Failed
Branch feature/comments (9f6e99)
by Stone
08:48
created

EditCommentController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 25
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A editCommentSubmit() 0 26 3
A editComment() 0 10 1
A __construct() 0 3 1
A checkSecurity() 0 4 3
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\Type\CommentType;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
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)
37
    {
38
        $this->checkSecurity($comment);
39
        $commentForm = $this->createForm(CommentType::class, $comment, [
40
            'save_button_label' => 'Update',
41
        ]);
42
43
        return $this->render('comment/edit.html.twig', [
44
            'comment' => $comment,
45
            'commentForm' => $commentForm->createView(),
46
        ]);
47
48
    }
49
50
    /**
51
     * @Route("/comment/edit/{id}", name="comment.submit", methods={"POST"})
52
     */
53
    public function editCommentSubmit(Comment $comment, Request $request)
54
    {
55
        $this->checkSecurity($comment);
56
57
        $form = $this->createForm(CommentType::class, $comment, [
58
            'save_button_label' => 'Update',
59
        ]);
60
61
        $form->handleRequest($request);
62
63
        if ($form->isSubmitted() && $form->isValid()) {
64
65
            $event = new CommentEditedEvent($comment);
66
            $this->dispatcher->dispatch(CommentEditedEvent::NAME, $event);
67
68
            return $this->redirectToRoute('trick.show', [
69
                'id' => $comment->getTrick()->getId(),
70
                'slug' => $comment->getTrick()->getSlug(),
71
                '_fragment' => 'comment-'.$comment->getId(),
72
            ]);
73
        }
74
75
        //This should never be called unless we have some strange error
76
        return $this->render('comment/edit.html.twig', [
77
            'comment' => $comment,
78
            'commentForm' => $form->createView(),
79
        ]);
80
81
    }
82
83
    /**
84
     * @param Comment $comment
85
     * Checks if the user is admin or author of the comment.
86
     * Thows a redirect to the trick show page
87
     */
88
    private function checkSecurity(Comment $comment){
89
        if(!($this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() === $comment->getUser()->getId()))
90
        {
91
            Throw new RedirectException($this->generateUrl('trick.show', ['id'=> $comment->getTrick()->getId(), 'slug'=> $comment->getTrick()->getSlug()]),"You are not allowed to edit this comment");
92
        }
93
    }
94
95
96
}