NeighborhoodController::new()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 28
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Admin;
6
7
use App\Controller\BaseController;
8
use App\Entity\Neighborhood;
9
use App\Form\Type\NeighborhoodType;
10
use App\Repository\CityRepository;
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 NeighborhoodController extends BaseController
19
{
20
    /**
21
     * @Route("/admin/locations/neighborhood", name="admin_neighborhood")
22
     */
23
    public function index(Request $request, CityRepository $repository): Response
24
    {
25
        $cities = $repository->findAll();
26
27
        return $this->render('admin/neighborhood/index.html.twig', [
28
            'site' => $this->site($request),
29
            'cities' => $cities,
30
        ]);
31
    }
32
33
    /**
34
     * @Route("/admin/locations/neighborhood/new", name="admin_neighborhood_new")
35
     */
36
    public function new(Request $request): Response
37
    {
38
        $neighborhood = new Neighborhood();
39
40
        $form = $this->createForm(NeighborhoodType::class, $neighborhood)
41
            ->add('saveAndCreateNew', SubmitType::class);
42
        $form->handleRequest($request);
43
44
        if ($form->isSubmitted() && $form->isValid()) {
45
            $em = $this->doctrine->getManager();
46
            $em->persist($neighborhood);
47
            $em->flush();
48
49
            $this->addFlash('success', 'message.created');
50
51
            /** @var ClickableInterface $button */
52
            $button = $form->get('saveAndCreateNew');
53
            if ($button->isClicked()) {
54
                return $this->redirectToRoute('admin_neighborhood_new');
55
            }
56
57
            return $this->redirectToRoute('admin_neighborhood');
58
        }
59
60
        return $this->render('admin/neighborhood/new.html.twig', [
61
            'site' => $this->site($request),
62
            'neighborhood' => $neighborhood,
63
            'form' => $form->createView(),
64
        ]);
65
    }
66
67
    /**
68
     * Displays a form to edit an existing Neighborhood entity.
69
     *
70
     * @Route("/admin/locations/neighborhood/{id<\d+>}/edit",methods={"GET", "POST"}, name="admin_neighborhood_edit")
71
     */
72
    public function edit(Request $request, Neighborhood $neighborhood): Response
73
    {
74
        $form = $this->createForm(NeighborhoodType::class, $neighborhood);
75
        $form->handleRequest($request);
76
        if ($form->isSubmitted() && $form->isValid()) {
77
            $this->doctrine->getManager()->flush();
78
            $this->addFlash('success', 'message.updated');
79
80
            return $this->redirectToRoute('admin_neighborhood');
81
        }
82
83
        return $this->render('admin/neighborhood/edit.html.twig', [
84
            'site' => $this->site($request),
85
            'form' => $form->createView(),
86
        ]);
87
    }
88
89
    /**
90
     * Deletes a Neighborhood entity.
91
     *
92
     * @Route("/neighborhood/{id<\d+>}/delete", methods={"POST"}, name="admin_neighborhood_delete")
93
     * @IsGranted("ROLE_ADMIN")
94
     */
95
    public function delete(Request $request, Neighborhood $neighborhood): Response
96
    {
97
        if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {
98
            return $this->redirectToRoute('admin_neighborhood');
99
        }
100
101
        $em = $this->doctrine->getManager();
102
        $em->remove($neighborhood);
103
        $em->flush();
104
        $this->addFlash('success', 'message.deleted');
105
106
        return $this->redirectToRoute('admin_neighborhood');
107
    }
108
}
109