Test Failed
Branch master (354693)
by Valery
12:29
created

AreaController::new()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 27
rs 9.7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Admin;
6
7
use App\Entity\Area;
8
use App\Form\Type\AreaType;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\Form\ClickableInterface;
12
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
final class AreaController extends AbstractController
18
{
19
    /**
20
     * @Route("/admin/area/new", name="admin_area_new")
21
     */
22
    public function new(Request $request): Response
23
    {
24
        $area = new Area();
25
26
        $form = $this->createForm(AreaType::class, $area)
27
            ->add('saveAndCreateNew', SubmitType::class);
28
        $form->handleRequest($request);
29
30
        if ($form->isSubmitted() && $form->isValid()) {
31
            $em = $this->getDoctrine()->getManager();
32
            $em->persist($area);
33
            $em->flush();
34
35
            $this->addFlash('success', 'message.created');
36
37
            /** @var ClickableInterface $button */
38
            $button = $form->get('saveAndCreateNew');
39
            if ($button->isClicked()) {
40
                return $this->redirectToRoute('admin_area_new');
41
            }
42
43
            return $this->redirectToRoute('admin_locality');
44
        }
45
46
        return $this->render('admin/area/new.html.twig', [
47
            'area' => $area,
48
            'form' => $form->createView(),
49
        ]);
50
    }
51
52
    /**
53
     * Displays a form to edit an existing Area entity.
54
     *
55
     * @Route("/admin/area/{id<\d+>}/edit",methods={"GET", "POST"}, name="admin_area_edit")
56
     */
57
    public function edit(Request $request, Area $area): Response
58
    {
59
        $form = $this->createForm(AreaType::class, $area);
60
        $form->handleRequest($request);
61
        if ($form->isSubmitted() && $form->isValid()) {
62
            $this->getDoctrine()->getManager()->flush();
63
            $this->addFlash('success', 'message.updated');
64
65
            return $this->redirectToRoute('admin_locality');
66
        }
67
68
        return $this->render('admin/area/edit.html.twig', [
69
            'form' => $form->createView(),
70
        ]);
71
    }
72
73
    /**
74
     * Deletes an Area entity.
75
     *
76
     * @Route("/area/{id<\d+>}/delete", methods={"POST"}, name="admin_area_delete")
77
     * @IsGranted("ROLE_ADMIN")
78
     */
79
    public function delete(Request $request, Area $area): Response
80
    {
81
        if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {
82
            return $this->redirectToRoute('admin_locality');
83
        }
84
85
        $em = $this->getDoctrine()->getManager();
86
        $em->remove($area);
87
        $em->flush();
88
        $this->addFlash('success', 'message.deleted');
89
90
        return $this->redirectToRoute('admin_locality');
91
    }
92
}
93