FeatureController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 38
dl 0
loc 87
rs 10
c 1
b 0
f 1
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 12 2
A new() 0 28 4
A edit() 0 14 3
A index() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Admin;
6
7
use App\Controller\BaseController;
8
use App\Entity\Feature;
9
use App\Form\Type\FeatureType;
10
use App\Repository\FeatureRepository;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
12
use Symfony\Component\Form\ClickableInterface;
13
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
final class FeatureController extends BaseController
19
{
20
    /**
21
     * @Route("/admin/feature", name="admin_feature")
22
     */
23
    public function index(Request $request, FeatureRepository $repository): Response
24
    {
25
        return $this->render('admin/feature/index.html.twig', [
26
            'site' => $this->site($request),
27
            'features' => $repository->findAll(),
28
        ]);
29
    }
30
31
    /**
32
     * @Route("/admin/feature/new", name="admin_feature_new")
33
     */
34
    public function new(Request $request): Response
35
    {
36
        $feature = new Feature();
37
38
        $form = $this->createForm(FeatureType::class, $feature)
39
            ->add('saveAndCreateNew', SubmitType::class);
40
        $form->handleRequest($request);
41
42
        if ($form->isSubmitted() && $form->isValid()) {
43
            $em = $this->doctrine->getManager();
44
            $em->persist($feature);
45
            $em->flush();
46
47
            $this->addFlash('success', 'message.created');
48
49
            /** @var ClickableInterface $button */
50
            $button = $form->get('saveAndCreateNew');
51
            if ($button->isClicked()) {
52
                return $this->redirectToRoute('admin_feature_new');
53
            }
54
55
            return $this->redirectToRoute('admin_feature');
56
        }
57
58
        return $this->render('admin/feature/new.html.twig', [
59
            'site' => $this->site($request),
60
            'feature' => $feature,
61
            'form' => $form->createView(),
62
        ]);
63
    }
64
65
    /**
66
     * Displays a form to edit an existing Feature entity.
67
     *
68
     * @Route("/admin/feature/{id<\d+>}/edit",methods={"GET", "POST"}, name="admin_feature_edit")
69
     */
70
    public function edit(Request $request, Feature $feature): Response
71
    {
72
        $form = $this->createForm(FeatureType::class, $feature);
73
        $form->handleRequest($request);
74
        if ($form->isSubmitted() && $form->isValid()) {
75
            $this->doctrine->getManager()->flush();
76
            $this->addFlash('success', 'message.updated');
77
78
            return $this->redirectToRoute('admin_feature');
79
        }
80
81
        return $this->render('admin/feature/edit.html.twig', [
82
            'site' => $this->site($request),
83
            'form' => $form->createView(),
84
        ]);
85
    }
86
87
    /**
88
     * Deletes a Feature entity.
89
     *
90
     * @Route("/feature/{id<\d+>}/delete", methods={"POST"}, name="admin_feature_delete")
91
     * @IsGranted("ROLE_ADMIN")
92
     */
93
    public function delete(Request $request, Feature $feature): Response
94
    {
95
        if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {
96
            return $this->redirectToRoute('admin_feature');
97
        }
98
99
        $em = $this->doctrine->getManager();
100
        $em->remove($feature);
101
        $em->flush();
102
        $this->addFlash('success', 'message.deleted');
103
104
        return $this->redirectToRoute('admin_feature');
105
    }
106
}
107