Test Failed
Push — develop ( 01b725...e43081 )
by Stone
04:47
created

EditTrickController::edit()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 29
nc 4
nop 2
dl 0
loc 52
rs 8.8337
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Controller\Trick\Admin;
4
5
use App\Entity\Trick;
6
use App\Event\Trick\TrickDeletedEvent;
7
use App\Event\Trick\TrickEditedEvent;
8
use App\Form\TrickType;
9
use App\History\TrickHistory;
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 EditTrickController extends AbstractController
25
{
26
    /**
27
     * @var EventDispatcherInterface
28
     */
29
    private $dispatcher;
30
    /**
31
     * @var TrickHistory
32
     */
33
    private $trickHistory;
34
35
    public function __construct(EventDispatcherInterface $dispatcher, TrickHistory $trickHistory)
36
    {
37
        $this->dispatcher = $dispatcher;
38
        $this->trickHistory = $trickHistory;
39
    }
40
41
    /**
42
     * @Route("/trick/edit/{id}", name="trick.edit")
43
     */
44
    public function edit(Trick $trick, Request $request)
45
    {
46
47
        $form = $this->createForm(TrickType::class, $trick);
48
        $form
49
            ->add('save', SubmitType::class, [
50
                'label' => 'Save',
51
                'attr' => [
52
                    'class' => 'waves-effect waves-light btn right mr-2'
53
                ]
54
            ])
55
            ->add('delete', SubmitType::class, [
56
                'label' => 'Delete',
57
                'attr' => [
58
                    'class' => 'waves-effect waves-light btn right mr-2',
59
                    'onclick' => 'return confirm(\'are you sure?\')',
60
                ]
61
            ]);
62
63
        $form->handleRequest($request);
64
65
66
        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

66
        if ($form->/** @scrutinizer ignore-call */ getClickedButton() && $form->getClickedButton()->getName() === 'delete') {
Loading history...
67
68
            $event = new TrickDeletedEvent($trick);
69
            $this->dispatcher->dispatch(TrickDeletedEvent::NAME, $event);
70
71
            return $this->redirectToRoute('home');
72
        }
73
74
        if ($form->isSubmitted() && $form->isValid()) {
75
76
            $event = new TrickEditedEvent($trick);
77
            $this->dispatcher->dispatch(TrickEditedEvent::NAME, $event);
78
79
            return $this->redirectToRoute('trick.show', [
80
                'id' => $trick->getId(),
81
                'slug' => $trick->getSlug(),
82
            ]);
83
        }
84
85
        $history = array();
86
        //Only load the history if we are admin. Ease the load.
87
        if($this->isGranted('ROLE_ADMIN')){
88
            $history = $this->trickHistory->getHistory($trick->getId());
89
        }
90
91
92
        return $this->render('trick/admin/edit.html.twig', [
93
            'trick' => $trick,
94
            'form' => $form->createView(),
95
            'history' => $history,
96
        ]);
97
    }
98
99
}