1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Controller\Admin; |
6
|
|
|
|
7
|
|
|
use App\Controller\BaseController; |
8
|
|
|
use App\Entity\Property; |
9
|
|
|
use App\Form\Type\PropertyType; |
10
|
|
|
use App\Repository\FilterRepository; |
11
|
|
|
use App\Service\Admin\PropertyService; |
12
|
|
|
use App\Transformer\RequestToArrayTransformer; |
13
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
17
|
|
|
|
18
|
|
|
final class PropertyController extends BaseController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @Route("/admin/property", defaults={"page": "1"}, methods={"GET"}, name="admin_property") |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function index(Request $request, FilterRepository $repository, RequestToArrayTransformer $transformer): Response |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$searchParams = $transformer->transform($request); |
26
|
|
|
$properties = $repository->findByFilter($searchParams); |
27
|
|
|
|
28
|
|
|
return $this->render('admin/property/index.html.twig', [ |
29
|
|
|
'site' => $this->site(), |
30
|
|
|
'properties' => $properties, |
31
|
|
|
'searchParams' => $searchParams, |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @Route("/admin/property/new", name="admin_property_new") |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function new(Request $request, PropertyService $service): Response |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$property = new Property(); |
41
|
|
|
$form = $this->createForm(PropertyType::class, $property); |
42
|
|
|
$form->handleRequest($request); |
43
|
|
|
|
44
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
45
|
|
|
$service->create($property); |
46
|
|
|
|
47
|
|
|
return $this->redirectToRoute('admin_photo_edit', ['id' => $property->getId()]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->render('admin/property/new.html.twig', [ |
51
|
|
|
'site' => $this->site(), |
52
|
|
|
'property' => $property, |
53
|
|
|
'form' => $form->createView(), |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Displays a form to edit an existing Property entity. |
59
|
|
|
* |
60
|
|
|
* @Route("/admin/property/{id<\d+>}/edit",methods={"GET", "POST"}, name="admin_property_edit") |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function edit(Request $request, Property $property, PropertyService $service): Response |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$form = $this->createForm(PropertyType::class, $property); |
65
|
|
|
$form->handleRequest($request); |
66
|
|
|
|
67
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
68
|
|
|
$service->update($property); |
69
|
|
|
|
70
|
|
|
return $this->redirectToRoute('admin_property'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->render('admin/property/edit.html.twig', [ |
74
|
|
|
'site' => $this->site(), |
75
|
|
|
'form' => $form->createView(), |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Deletes a Property entity. |
81
|
|
|
* |
82
|
|
|
* @Route("/property/{id<\d+>}/delete", methods={"POST"}, name="admin_property_delete") |
83
|
|
|
* @IsGranted("ROLE_ADMIN") |
84
|
|
|
*/ |
85
|
|
|
public function delete(Request $request, Property $property, PropertyService $service): Response |
86
|
|
|
{ |
87
|
|
|
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) { |
88
|
|
|
return $this->redirectToRoute('admin_property'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$service->delete($property); |
92
|
|
|
|
93
|
|
|
return $this->redirectToRoute('admin_property'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.