Passed
Push — develop ( 5c069a...34195e )
by Stone
04:14
created

EditTrickController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 35
dl 0
loc 73
rs 10
c 0
b 0
f 0

2 Methods

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

73
        if ($form->/** @scrutinizer ignore-call */ getClickedButton() && $form->getClickedButton()->getName() === 'delete') {
Loading history...
74
75
            $event = new TrickDeletedEvent($trick);
76
            $this->dispatcher->dispatch(TrickDeletedEvent::NAME, $event);
77
78
            return $this->redirectToRoute('home');
79
        }
80
81
        if ($form->isSubmitted() && $form->isValid()) {
82
83
            $event = new TrickEditedEvent($trick);
84
            $this->dispatcher->dispatch(TrickEditedEvent::NAME, $event);
85
86
            return $this->redirectToRoute('trick.show', [
87
                'id' => $trick->getId(),
88
                'slug' => $trick->getSlug(),
89
            ]);
90
        }
91
92
        $allTags = $this->em->getRepository(Tag::class)->findAll();
93
94
        return $this->render('trick/admin/edit.html.twig', [
95
            'allTags' => $allTags,
96
            'tricktags' => $trick->getTags(),
97
            'trick' => $trick,
98
            'form' => $form->createView(),
99
        ]);
100
    }
101
102
}