Test Failed
Push — develop ( c35c51...0616cb )
by Stone
04:36 queued 10s
created

EditTrickController::edit()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 55
rs 8.4746
c 0
b 0
f 0
cc 7
nc 6
nop 2

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
6
use App\Entity\Image;
7
use App\Entity\Trick;
8
use App\Event\Image\ImageAddEvent;
9
use App\Event\Trick\TrickDeletedEvent;
10
use App\Event\Trick\TrickEditedEvent;
11
use App\Form\ImageTypeForm;
12
use App\Form\TrickTypeForm;
13
use App\History\TrickHistory;
14
use App\Repository\TagRepository;
15
use App\Serializer\TagSerializer;
16
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Routing\Annotation\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
22
23
/**
24
 * Class TrickEditController
25
 * @package App\Controller\Edit
26
 *
27
 * Require the user to be connected for everything here
28
 * @IsGranted("ROLE_USER")
29
 */
30
class EditTrickController extends AbstractController
31
{
32
    /**
33
     * @var EventDispatcherInterface
34
     */
35
    private $dispatcher;
36
    /**
37
     * @var TrickHistory
38
     */
39
    private $trickHistory;
40
41
    /**
42
     * @var TagRepository
43
     */
44
    private $tagRepository;
45
    /**
46
     * @var TagSerializer
47
     */
48
    private $tagSerializer;
49
50
    public function __construct(
51
        EventDispatcherInterface $dispatcher,
52
        TrickHistory $trickHistory,
53
        TagRepository $tagRepository,
54
        TagSerializer $tagSerializer
55
    ) {
56
        $this->dispatcher = $dispatcher;
57
        $this->trickHistory = $trickHistory;
58
        $this->tagRepository = $tagRepository;
59
        $this->tagSerializer = $tagSerializer;
60
    }
61
62
    /**
63
     * @Route("/trick/edit/{id}", name="trick.edit")
64
     */
65
    public function edit(Trick $trick, Request $request)
66
    {
67
68
        $form = $this->createForm(TrickTypeForm::class, $trick, [
69
            'all_tags_json' => $this->tagSerializer->allTagsJson(),
70
            'trick_tags_json' => $trick->getTagsJson(),
71
        ]);
72
        $form
73
            ->add('delete', SubmitType::class, [
74
                'label' => 'Delete',
75
                'attr' => [
76
                    'class' => 'waves-effect waves-light btn right mr-2',
77
                    'onclick' => 'return confirm(\'are you sure?\')',
78
                ]
79
            ]);
80
81
        $form->handleRequest($request);
82
83
        $trickImage = new Image();
84
        $imageForm = $this->createForm(ImageTypeForm::class, $trickImage);
85
        $imageForm->handleRequest($request);
86
87
        if ($imageForm->isSubmitted() && $imageForm->isValid()) {
88
            $event = new ImageAddEvent($trickImage, $trick);
89
            $this->dispatcher->dispatch(ImageAddEvent::NAME, $event);
90
91
            //Forcing the next form shown to be a new image
92
            $trickImage = new Image();
93
            $imageForm = $this->createForm(ImageTypeForm::class, $trickImage);
94
        }
95
96
97
        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

97
        if ($form->/** @scrutinizer ignore-call */ getClickedButton() && $form->getClickedButton()->getName() === 'delete') {
Loading history...
98
99
            $event = new TrickDeletedEvent($trick);
100
            $this->dispatcher->dispatch(TrickDeletedEvent::NAME, $event);
101
102
            return $this->redirectToRoute('home');
103
        }
104
105
        if ($form->isSubmitted() && $form->isValid()) {
106
107
            $event = new TrickEditedEvent($trick);
108
            $this->dispatcher->dispatch(TrickEditedEvent::NAME, $event);
109
110
            return $this->redirectToRoute('trick.show', [
111
                'id' => $trick->getId(),
112
                'slug' => $trick->getSlug(),
113
            ]);
114
        }
115
116
        return $this->render('trick/admin/edit.html.twig', [
117
            'trick' => $trick,
118
            'form' => $form->createView(),
119
            'imageForm' => $imageForm->createView(),
120
        ]);
121
    }
122
123
}