Test Setup Failed
Push — develop ( dc2cdb...883a72 )
by Stone
04:31
created

TrickEditController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Edit;
4
5
use App\Entity\Trick;
6
use App\Event\Trick\TrickCreatedEvent;
7
use App\Event\Trick\TrickDeletedEvent;
8
use App\Event\Trick\TrickEditedEvent;
9
use App\Form\TrickType;
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
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
16
17
/**
18
 * Class TrickEditController
19
 * @package App\Controller\Edit
20
 *
21
 * Require the user to be connected for everything here
22
 * @IsGranted("ROLE_USER")
23
 */
24
class TrickEditController extends AbstractController
25
{
26
    /**
27
     * @var EventDispatcherInterface
28
     */
29
    private $dispatcher;
30
31
    public function __construct(EventDispatcherInterface $dispatcher)
32
    {
33
        $this->dispatcher = $dispatcher;
34
    }
35
36
    /**
37
     * @Route("/trick/new", name="trick.new")
38
     */
39
    public function new(Request $request)
40
    {
41
        $trick = new Trick();
42
43
        $form = $this->createForm(TrickType::class, $trick);
44
        $form->add('save', SubmitType::class, [
45
            'label' => 'Save',
46
            'attr' => [
47
                'class' => 'waves-effect waves-light btn right'
48
            ]
49
        ]);
50
51
        $form->handleRequest($request);
52
53
        if ($form->isSubmitted() && $form->isValid()) {
54
55
            $event = new TrickCreatedEvent($trick);
56
            $this->dispatcher->dispatch(TrickCreatedEvent::NAME, $event);
57
58
            return $this->redirectToRoute('trick.show', [
59
                'id' => $trick->getId(),
60
                'slug' => $trick->getSlug(),
61
            ]);
62
        }
63
64
        return $this->render('trick/admin/new.html.twig', [
65
            'form' => $form->createView(),
66
        ]);
67
    }
68
69
    /**
70
     * @Route("/trick/{id}/edit", name="trick.edit")
71
     */
72
    public function edit(Trick $trick, Request $request)
73
    {
74
75
        $form = $this->createForm(TrickType::class, $trick);
76
        $form
77
            ->add('save', SubmitType::class, [
78
                'label' => 'Save',
79
                'attr' => [
80
                    'class' => 'waves-effect waves-light btn right mr-2'
81
                ]
82
            ])
83
            ->add('delete', SubmitType::class, [
84
                'label' => 'Delete',
85
                'attr' => [
86
                    'class' => 'waves-effect waves-light btn right mr-2',
87
                    'onclick' => 'return confirm(\'are you sure?\')',
88
                ]
89
            ]);
90
91
        $form->handleRequest($request);
92
93
94
        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

94
        if ($form->/** @scrutinizer ignore-call */ getClickedButton() && $form->getClickedButton()->getName() === 'delete') {
Loading history...
95
96
            $event = new TrickDeletedEvent($trick);
97
            $this->dispatcher->dispatch(TrickDeletedEvent::NAME, $event);
98
99
            return $this->redirectToRoute('trick.home');
100
        }
101
102
        if ($form->isSubmitted() && $form->isValid()) {
103
104
            $event = new TrickEditedEvent($trick);
105
            $this->dispatcher->dispatch(TrickEditedEvent::NAME, $event);
106
107
            return $this->redirectToRoute('trick.show', [
108
                'id' => $trick->getId(),
109
                'slug' => $trick->getSlug(),
110
            ]);
111
        }
112
113
        return $this->render('trick/admin/edit.html.twig', [
114
            'trick' => $trick,
115
            'form' => $form->createView(),
116
        ]);
117
    }
118
119
    /**
120
     * @Route("/trick/{id}/delete", name="trick.delete")
121
     */
122
    public function delete(Trick $trick)
123
    {
124
        $event = new TrickDeletedEvent($trick);
125
        $this->dispatcher->dispatch(TrickDeletedEvent::NAME, $event);
126
127
        return $this->redirectToRoute('trick.home');
128
    }
129
130
131
}