Test Setup Failed
Pull Request — master (#15)
by Stone
04:48 queued 31s
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
    /**
18
     * @var TrickRepository
19
     */
20
    private $repository;
21
    /**
22
     * @var ObjectManager
23
     */
24
    private $em;
25
26
    private function deleteTrick(Trick $trick)
27
    {
28
        $this->em->remove($trick);
29
        $this->em->flush();
30
    }
31
32
    public function __construct(TrickRepository $repository, ObjectManager $em)
33
    {
34
        $this->repository = $repository;
35
        $this->em = $em;
36
    }
37
38
    /**
39
     * @Route("/trick/new", name="trick.new")
40
     */
41
    public function new(Request $request)
42
    {
43
        $trick = new Trick();
44
45
        $form = $this->createForm(TrickType::class, $trick);
46
        $form->add('save', SubmitType::class, [
47
            'label' => 'Save',
48
            'attr' => [
49
                'class' => 'waves-effect waves-light btn right'
50
            ]
51
        ]);
52
53
        $form->handleRequest($request);
54
55
        if ($form->isSubmitted() && $form->isValid()) {
56
            $this->em->persist($trick);
57
            $this->em->flush();
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
            $this->deleteTrick($trick);
96
            return $this->redirectToRoute('trick.home');
97
        }
98
99
        if ($form->isSubmitted() && $form->isValid()) {
100
            $this->em->flush();
101
            return $this->redirectToRoute('trick.show', [
102
                'id' => $trick->getId(),
103
                'slug' => $trick->getSlug(),
104
            ]);
105
        }
106
107
        return $this->render('trick/admin/edit.html.twig', [
108
            'trick' => $trick,
109
            'form' => $form->createView(),
110
        ]);
111
    }
112
113
    /**
114
     * @Route("/trick/{id}/delete", name="trick.delete")
115
     */
116
    public function delete(Trick $trick)
117
    {
118
        $this->deleteTrick($trick);
119
        return $this->redirectToRoute('trick.home');
120
    }
121
122
123
}