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

NewTrickController::new()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 2
nop 1
dl 0
loc 27
rs 9.7333
c 0
b 0
f 0
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
}