NewTrickController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 22
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A new() 0 25 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\TrickFormType;
8
use App\Repository\TagRepository;
9
use App\Serializer\TagSerializer;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
15
16
/**
17
 * Class TrickEditController
18
 * @package App\Controller\Edit
19
 *
20
 * Require the user to be connected for everything here
21
 * @IsGranted("ROLE_USER")
22
 */
23
class NewTrickController extends AbstractController
24
{
25
    /**
26
     * @var EventDispatcherInterface
27
     */
28
    private $dispatcher;
29
    /**
30
     * @var TagRepository
31
     */
32
    private $tagRepository;
33
    /**
34
     * @var TagSerializer
35
     */
36
    private $tagSerializer;
37
38
    public function __construct(
39
        EventDispatcherInterface $dispatcher,
40
        TagRepository $tagRepository,
41
        TagSerializer $tagSerializer
42
    ) {
43
        $this->dispatcher = $dispatcher;
44
        $this->tagRepository = $tagRepository;
45
        $this->tagSerializer = $tagSerializer;
46
    }
47
48
    /**
49
     * @Route("/trick/new", name="trick.new")
50
     */
51
    public function new(Request $request)
52
    {
53
        $trick = new Trick();
54
55
        $form = $this->createForm(TrickFormType::class, $trick, [
56
            'all_tags_json' => $this->tagSerializer->allTagsJson(),
57
            'trick_tags_json' => $trick->getTagsJson(),
58
        ]);
59
60
        $form->handleRequest($request);
61
62
        if ($form->isSubmitted() && $form->isValid()) {
63
64
            $event = new TrickCreatedEvent($trick);
65
            $this->dispatcher->dispatch(TrickCreatedEvent::NAME, $event);
66
67
            return $this->redirectToRoute('trick.show', [
68
                'id' => $trick->getId(),
69
                'slug' => $trick->getSlug(),
70
            ]);
71
        }
72
73
        return $this->render('trick/admin/new.html.twig', [
74
            'form' => $form->createView(),
75
            'trick' => $trick,
76
        ]);
77
    }
78
79
}