Passed
Push — master ( 786b21...4a8f5f )
by Stone
06:47 queued 42s
created

NewTrickController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A new() 0 27 3
1
<?php
2
3
namespace App\Controller\Trick\Admin;
4
5
use App\Entity\Trick;
6
use App\Event\Trick\TrickCreatedEvent;
7
use App\Form\TrickType;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
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 NewTrickController extends AbstractController
23
{
24
    /**
25
     * @var EventDispatcherInterface
26
     */
27
    private $dispatcher;
28
29
    public function __construct(EventDispatcherInterface $dispatcher)
30
    {
31
        $this->dispatcher = $dispatcher;
32
    }
33
34
    /**
35
     * @Route("/trick/new", name="trick.new")
36
     */
37
    public function new(Request $request)
38
    {
39
        $trick = new Trick();
40
41
        $form = $this->createForm(TrickType::class, $trick);
42
        $form->add('save', SubmitType::class, [
43
            'label' => 'Save',
44
            'attr' => [
45
                'class' => 'waves-effect waves-light btn right'
46
            ]
47
        ]);
48
49
        $form->handleRequest($request);
50
51
        if ($form->isSubmitted() && $form->isValid()) {
52
53
            $event = new TrickCreatedEvent($trick);
54
            $this->dispatcher->dispatch(TrickCreatedEvent::NAME, $event);
55
56
            return $this->redirectToRoute('trick.show', [
57
                'id' => $trick->getId(),
58
                'slug' => $trick->getSlug(),
59
            ]);
60
        }
61
62
        return $this->render('trick/admin/new.html.twig', [
63
            'form' => $form->createView(),
64
        ]);
65
    }
66
67
}