Passed
Push — master ( e769ca...f1e182 )
by Stone
04:38
created

EditTrickController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 29
dl 0
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A edit() 0 39 5
1
<?php
2
3
namespace App\Controller\Trick\Admin;
4
5
6
use App\Entity\Trick;
7
use App\Event\Trick\TrickDeletedEvent;
8
use App\Event\Trick\TrickEditedEvent;
9
use App\Form\TrickType;
10
use App\History\TrickHistory;
11
use App\Repository\TagRepository;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Routing\Annotation\Route;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
18
19
/**
20
 * Class TrickEditController
21
 * @package App\Controller\Edit
22
 *
23
 * Require the user to be connected for everything here
24
 * @IsGranted("ROLE_USER")
25
 */
26
class EditTrickController extends AbstractController
27
{
28
    /**
29
     * @var EventDispatcherInterface
30
     */
31
    private $dispatcher;
32
    /**
33
     * @var TrickHistory
34
     */
35
    private $trickHistory;
36
37
    /**
38
     * @var TagRepository
39
     */
40
    private $tagRepository;
41
42
    public function __construct(EventDispatcherInterface $dispatcher, TrickHistory $trickHistory, TagRepository $tagRepository )
43
    {
44
        $this->dispatcher = $dispatcher;
45
        $this->trickHistory = $trickHistory;
46
        $this->tagRepository = $tagRepository;
47
    }
48
49
    /**
50
     * @Route("/trick/edit/{id}", name="trick.edit")
51
     */
52
    public function edit(Trick $trick, Request $request)
53
    {
54
55
        $form = $this->createForm(TrickType::class, $trick);
56
        $form
57
            ->add('delete', SubmitType::class, [
58
                'label' => 'Delete',
59
                'attr' => [
60
                    'class' => 'waves-effect waves-light btn right mr-2',
61
                    'onclick' => 'return confirm(\'are you sure?\')',
62
                ]
63
            ]);
64
65
        $form->handleRequest($request);
66
67
68
        if ($form->getClickedButton() && $form->getClickedButton()->getName() === 'delete') {
0 ignored issues
show
Bug introduced by
The method getClickedButton() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\Form. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        if ($form->/** @scrutinizer ignore-call */ getClickedButton() && $form->getClickedButton()->getName() === 'delete') {
Loading history...
69
70
            $event = new TrickDeletedEvent($trick);
71
            $this->dispatcher->dispatch(TrickDeletedEvent::NAME, $event);
72
73
            return $this->redirectToRoute('home');
74
        }
75
76
        if ($form->isSubmitted() && $form->isValid()) {
77
78
            $event = new TrickEditedEvent($trick);
79
            $this->dispatcher->dispatch(TrickEditedEvent::NAME, $event);
80
81
            return $this->redirectToRoute('trick.show', [
82
                'id' => $trick->getId(),
83
                'slug' => $trick->getSlug(),
84
            ]);
85
        }
86
87
        return $this->render('trick/admin/edit.html.twig', [
88
            'allTags' => $this->tagRepository->findAll(),
89
            'trick' => $trick,
90
            'form' => $form->createView(),
91
        ]);
92
    }
93
94
}