Test Setup Failed
Pull Request — master (#15)
by Stone
05:06 queued 01:55
created

TrickEditController::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Admin;
4
5
use App\Entity\Trick;
6
use App\Form\TrickType;
7
use App\Repository\TrickRepository;
8
use Doctrine\Common\Persistence\ObjectManager;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
class TrickEditController extends AbstractController
15
{
16
    /**
17
     * @var ObjectManager
18
     */
19
    private $em;
20
21
    private function deleteTrick(Trick $trick)
22
    {
23
        $this->em->remove($trick);
24
        $this->em->flush();
25
    }
26
27
    public function __construct(ObjectManager $em)
28
    {
29
        $this->em = $em;
30
    }
31
32
    /**
33
     * @Route("/trick/new", name="trick.new")
34
     */
35
    public function new(Request $request)
36
    {
37
        $trick = new Trick();
38
39
        $form = $this->createForm(TrickType::class, $trick);
40
        $form->add('save', SubmitType::class, [
41
            'label' => 'Save',
42
            'attr' => [
43
                'class' => 'waves-effect waves-light btn right'
44
            ]
45
        ]);
46
47
        $form->handleRequest($request);
48
49
        if ($form->isSubmitted() && $form->isValid()) {
50
            $this->em->persist($trick);
51
            $this->em->flush();
52
            return $this->redirectToRoute('trick.show', [
53
                'id' => $trick->getId(),
54
                'slug' => $trick->getSlug(),
55
            ]);
56
        }
57
58
        return $this->render('trick/admin/new.html.twig', [
59
            'form' => $form->createView(),
60
        ]);
61
    }
62
63
    /**
64
     * @Route("/trick/{id}/edit", name="trick.edit")
65
     */
66
    public function edit(Trick $trick, Request $request)
67
    {
68
69
        $form = $this->createForm(TrickType::class, $trick);
70
        $form
71
            ->add('save', SubmitType::class, [
72
                'label' => 'Save',
73
                'attr' => [
74
                    'class' => 'waves-effect waves-light btn right mr-2'
75
                ]
76
            ])
77
            ->add('delete', SubmitType::class, [
78
                'label' => 'Delete',
79
                'attr' => [
80
                    'class' => 'waves-effect waves-light btn right mr-2',
81
                    'onclick' =>'return confirm(\'are you sure?\')',
82
                ]
83
            ]);
84
85
        $form->handleRequest($request);
86
87
88
        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

88
        if ($form->/** @scrutinizer ignore-call */ getClickedButton() && $form->getClickedButton()->getName() === 'delete') {
Loading history...
89
            $this->deleteTrick($trick);
90
            return $this->redirectToRoute('trick.home');
91
        }
92
93
        if ($form->isSubmitted() && $form->isValid()) {
94
            $this->em->flush();
95
            return $this->redirectToRoute('trick.show', [
96
                'id' => $trick->getId(),
97
                'slug' => $trick->getSlug(),
98
            ]);
99
        }
100
101
        return $this->render('trick/admin/edit.html.twig', [
102
            'trick' => $trick,
103
            'form' => $form->createView(),
104
        ]);
105
    }
106
107
    /**
108
     * @Route("/trick/{id}/delete", name="trick.delete")
109
     */
110
    public function delete(Trick $trick)
111
    {
112
        $this->deleteTrick($trick);
113
        return $this->redirectToRoute('trick.home');
114
    }
115
116
117
}