Passed
Push — develop ( 34195e...b04746 )
by Stone
08:36 queued 03:47
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\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * Class EditCommentController
17
 * @package App\Controller\Comment
18
 * @IsGranted("ROLE_USER")
19
 */
20
class EditCommentController extends AbstractController
21
{
22
    /**
23
     * @var EventDispatcherInterface
24
     */
25
    private $dispatcher;
26
27
    public function __construct(EventDispatcherInterface $dispatcher)
28
    {
29
        $this->dispatcher = $dispatcher;
30
    }
31
32
    /**
33
     * @Route("/comment/edit/{id}", name="comment.edit", methods={"GET"})
34
     */
35
    public function editComment(Comment $comment)
36
    {
37
        $this->checkSecurity($comment);
38
        $commentForm = $this->createForm(CommentType::class, $comment, [
39
            'save_button_label' => 'Update',
40
        ]);
41
42
        return $this->render('comment/edit.html.twig', [
43
            'comment' => $comment,
44
            'commentForm' => $commentForm->createView(),
45
        ]);
46
47
    }
48
49
    /**
50
     * @Route("/comment/edit/{id}", name="comment.submit", methods={"POST"})
51
     */
52
    public function editCommentSubmit(Comment $comment, Request $request)
53
    {
54
        $this->checkSecurity($comment);
55
56
        $form = $this->createForm(CommentType::class, $comment, [
57
            'save_button_label' => 'Update',
58
        ]);
59
60
        $form->handleRequest($request);
61
62
        if ($form->isSubmitted() && $form->isValid()) {
63
64
            $event = new CommentEditedEvent($comment);
65
            $this->dispatcher->dispatch(CommentEditedEvent::NAME, $event);
66
67
            return $this->redirectToRoute('trick.show', [
68
                'id' => $comment->getTrick()->getId(),
69
                'slug' => $comment->getTrick()->getSlug(),
70
                '_fragment' => 'comment-'.$comment->getId(),
71
            ]);
72
        }
73
74
        //This should never be called unless we have some strange error
75
        return $this->render('comment/edit.html.twig', [
76
            'comment' => $comment,
77
            'commentForm' => $form->createView(),
78
        ]);
79
80
    }
81
82
    /**
83
     * @param Comment $comment
84
     * Checks if the user is admin or author of the comment.
85
     * Thows a redirect to the trick show page
86
     */
87
    private function checkSecurity(Comment $comment){
88
        if(!($this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() === $comment->getUser()->getId()))
89
        {
90
            Throw new RedirectException($this->generateUrl('trick.show', ['id'=> $comment->getTrick()->getId(), 'slug'=> $comment->getTrick()->getSlug()]),"You are not allowed to edit this comment");
91
        }
92
    }
93
94
95
}