Passed
Push — develop ( d944ff...2efefd )
by Stone
07:19 queued 10s
created

EditTrickController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A edit() 0 44 5
1
<?php
2
3
namespace App\Controller\Trick\Admin;
4
5
use App\Entity\Trick;
6
use App\Event\Trick\TrickDeletedEvent;
7
use App\Event\Trick\TrickEditedEvent;
8
use App\Form\TrickType;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
15
16
/**
17
 * Class TrickEditController
18
 * @package App\Controller\Edit
19
 *
20
 * Require the user to be connected for everything here
21
 * @IsGranted("ROLE_USER")
22
 */
23
class EditTrickController extends AbstractController
24
{
25
    /**
26
     * @var EventDispatcherInterface
27
     */
28
    private $dispatcher;
29
30
    public function __construct(EventDispatcherInterface $dispatcher)
31
    {
32
        $this->dispatcher = $dispatcher;
33
    }
34
35
    /**
36
     * @Route("/trick/{id}/edit", name="trick.edit")
37
     */
38
    public function edit(Trick $trick, Request $request)
39
    {
40
41
        $form = $this->createForm(TrickType::class, $trick);
42
        $form
43
            ->add('save', SubmitType::class, [
44
                'label' => 'Save',
45
                'attr' => [
46
                    'class' => 'waves-effect waves-light btn right mr-2'
47
                ]
48
            ])
49
            ->add('delete', SubmitType::class, [
50
                'label' => 'Delete',
51
                'attr' => [
52
                    'class' => 'waves-effect waves-light btn right mr-2',
53
                    'onclick' => 'return confirm(\'are you sure?\')',
54
                ]
55
            ]);
56
57
        $form->handleRequest($request);
58
59
60
        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

60
        if ($form->/** @scrutinizer ignore-call */ getClickedButton() && $form->getClickedButton()->getName() === 'delete') {
Loading history...
61
62
            $event = new TrickDeletedEvent($trick);
63
            $this->dispatcher->dispatch(TrickDeletedEvent::NAME, $event);
64
65
            return $this->redirectToRoute('home');
66
        }
67
68
        if ($form->isSubmitted() && $form->isValid()) {
69
70
            $event = new TrickEditedEvent($trick);
71
            $this->dispatcher->dispatch(TrickEditedEvent::NAME, $event);
72
73
            return $this->redirectToRoute('trick.show', [
74
                'id' => $trick->getId(),
75
                'slug' => $trick->getSlug(),
76
            ]);
77
        }
78
79
        return $this->render('trick/admin/edit.html.twig', [
80
            'trick' => $trick,
81
            'form' => $form->createView(),
82
        ]);
83
    }
84
85
}