1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Controller\Admin; |
6
|
|
|
|
7
|
|
|
use App\Form\Type\FilterSettingsType; |
8
|
|
|
use App\Form\Type\SettingsType; |
9
|
|
|
use App\Repository\SettingsRepository; |
10
|
|
|
use App\Service\Admin\SettingsService; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
|
16
|
|
|
final class SettingsController extends AbstractController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var SettingsRepository |
20
|
|
|
*/ |
21
|
|
|
private $repository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SettingsService |
25
|
|
|
*/ |
26
|
|
|
private $service; |
27
|
|
|
|
28
|
|
|
public function __construct(SettingsRepository $repository, SettingsService $service) |
29
|
|
|
{ |
30
|
|
|
$this->repository = $repository; |
31
|
|
|
$this->service = $service; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @Route("/admin/settings", name="admin_settings") |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function settings(Request $request): Response |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$settings = $this->repository->findAllAsArray(); |
40
|
|
|
|
41
|
|
|
$form = $this->createForm(SettingsType::class, $settings); |
42
|
|
|
$form->handleRequest($request); |
43
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
44
|
|
|
$this->service->updateSettings($form->getNormData()); |
45
|
|
|
|
46
|
|
|
return $this->redirectToRoute('admin_settings'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->render('admin/settings/settings.html.twig', [ |
50
|
|
|
'form' => $form->createView(), |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @Route("/admin/setting/header", name="admin_header_settings") |
56
|
|
|
*/ |
57
|
|
|
public function changeHeaderImage(Request $request): Response |
58
|
|
|
{ |
59
|
|
|
$settings = $this->repository->findAllAsArray(); |
60
|
|
|
|
61
|
|
|
$form = $this->createForm(FilterSettingsType::class, $settings); |
62
|
|
|
$form->handleRequest($request); |
63
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
64
|
|
|
$this->service->updateSettings($form->getNormData()); |
65
|
|
|
|
66
|
|
|
return $this->redirectToRoute('admin_header_settings'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->render('admin/settings/header_settings.html.twig', [ |
70
|
|
|
'header_image' => $settings['header_image'], |
71
|
|
|
'logo_image' => $settings['logo_image'], |
72
|
|
|
'form' => $form->createView(), |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @Route("/admin/setting/upload_header_image", methods={"POST"}, name="admin_setting_upload_header_image") |
78
|
|
|
* |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
|
|
public function uploadHeaderImage(Request $request): Response |
82
|
|
|
{ |
83
|
|
|
// Upload custom header image |
84
|
|
|
return $this->service->uploadImage('header_image', $request); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @Route("/admin/setting/upload_logo_image", methods={"POST"}, name="admin_setting_upload_logo_image") |
89
|
|
|
* |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
|
|
public function uploadLogoImage(Request $request): Response |
93
|
|
|
{ |
94
|
|
|
// Upload custom header image |
95
|
|
|
return $this->service->uploadImage('logo_image', $request); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @Route("/admin/setting/delete_header_image", methods={"POST"}, name="admin_setting_delete_header_image") |
100
|
|
|
*/ |
101
|
|
|
public function deleteHeaderImage(Request $request): Response |
102
|
|
|
{ |
103
|
|
|
// Reset a header image to the default image. |
104
|
|
|
$this->service->resetImage('header_image', $request); |
105
|
|
|
|
106
|
|
|
return $this->redirectToRoute('admin_header_settings'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @Route("/admin/setting/delete_logo_image", methods={"POST"}, name="admin_setting_delete_logo_image") |
111
|
|
|
*/ |
112
|
|
|
public function deleteLogoImage(Request $request): Response |
113
|
|
|
{ |
114
|
|
|
// Reset a header image to the default image. |
115
|
|
|
$this->service->resetImage('logo_image', $request); |
116
|
|
|
|
117
|
|
|
return $this->redirectToRoute('admin_header_settings'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.