Passed
Push — develop ( 710e27...6f29be )
by Stone
03:28
created

TrickEditController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 38 5
A new() 0 25 3
A __construct() 0 4 1
A deleteTrick() 0 4 1
A delete() 0 4 1
1
<?php
2
3
namespace App\Controller\Edit;
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
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
14
15
/**
16
 * Class TrickEditController
17
 * @package App\Controller\Edit
18
 *
19
 * Require the user to be connected for everything here
20
 * @IsGranted("ROLE_USER")
21
 */
22
class TrickEditController extends AbstractController
23
{
24
25
    /**
26
     * @var TrickRepository
27
     */
28
    private $repository;
29
    /**
30
     * @var ObjectManager
31
     */
32
    private $em;
33
34
    private function deleteTrick(Trick $trick)
35
    {
36
        $this->em->remove($trick);
37
        $this->em->flush();
38
    }
39
40
    public function __construct(TrickRepository $repository, ObjectManager $em)
41
    {
42
        $this->repository = $repository;
43
        $this->em = $em;
44
    }
45
46
    /**
47
     * @Route("/trick/new", name="trick.new")
48
     */
49
    public function new(Request $request)
50
    {
51
        $trick = new Trick();
52
53
        $form = $this->createForm(TrickType::class, $trick);
54
        $form->add('save', SubmitType::class, [
55
            'label' => 'Save',
56
            'attr' => [
57
                'class' => 'waves-effect waves-light btn right'
58
            ]
59
        ]);
60
61
        $form->handleRequest($request);
62
63
        if ($form->isSubmitted() && $form->isValid()) {
64
            $this->em->persist($trick);
65
            $this->em->flush();
66
            return $this->redirectToRoute('trick.show', [
67
                'id' => $trick->getId(),
68
                'slug' => $trick->getSlug(),
69
            ]);
70
        }
71
72
        return $this->render('trick/admin/new.html.twig', [
73
            'form' => $form->createView(),
74
        ]);
75
    }
76
77
    /**
78
     * @Route("/trick/{id}/edit", name="trick.edit")
79
     */
80
    public function edit(Trick $trick, Request $request)
81
    {
82
83
        $form = $this->createForm(TrickType::class, $trick);
84
        $form
85
            ->add('save', SubmitType::class, [
86
                'label' => 'Save',
87
                'attr' => [
88
                    'class' => 'waves-effect waves-light btn right mr-2'
89
                ]
90
            ])
91
            ->add('delete', SubmitType::class, [
92
                'label' => 'Delete',
93
                'attr' => [
94
                    'class' => 'waves-effect waves-light btn right mr-2',
95
                    'onclick' =>'return confirm(\'are you sure?\')',
96
                ]
97
            ]);
98
99
        $form->handleRequest($request);
100
101
102
        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

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