1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Service\Admin; |
6
|
|
|
|
7
|
|
|
use App\Repository\SettingsRepository; |
8
|
|
|
use App\Service\AbstractService; |
9
|
|
|
use App\Service\FileUploader; |
10
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
15
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
16
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
17
|
|
|
|
18
|
|
|
final class SettingsService extends AbstractService |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var SettingsRepository |
22
|
|
|
*/ |
23
|
|
|
private $repository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var FileUploader |
27
|
|
|
*/ |
28
|
|
|
private $fileUploader; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
CsrfTokenManagerInterface $tokenManager, |
32
|
|
|
SessionInterface $session, |
33
|
|
|
SettingsRepository $repository, |
34
|
|
|
FileUploader $fileUploader |
35
|
|
|
) { |
36
|
|
|
parent::__construct($tokenManager, $session); |
37
|
|
|
$this->repository = $repository; |
38
|
|
|
$this->fileUploader = $fileUploader; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Update settings in database. |
43
|
|
|
*/ |
44
|
|
|
public function updateSettings(array $formData): void |
45
|
|
|
{ |
46
|
|
|
$this->repository->updateSettings($formData); |
47
|
|
|
$this->addFlash('success', 'message.updated'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Upload custom header image. |
52
|
|
|
* |
53
|
|
|
* @param string $type |
54
|
|
|
* @param Request $request |
55
|
|
|
* |
56
|
|
|
* @return Response |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
|
public function uploadImage(string $type = 'header_image', Request $request): Response |
60
|
|
|
{ |
61
|
|
|
/** @var UploadedFile $uploadedFile */ |
62
|
|
|
$uploadedFile = $request->files->get('file'); |
63
|
|
|
|
64
|
|
|
if (!$this->isImageValid($uploadedFile)) { |
65
|
|
|
return new JsonResponse(['status' => 'error']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$fileName = $this->fileUploader->upload($uploadedFile); |
69
|
|
|
$this->repository->updateSetting($type, $fileName); |
70
|
|
|
|
71
|
|
|
return new JsonResponse(['status' => 'ok']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Validate header image. |
76
|
|
|
*/ |
77
|
|
|
private function isImageValid(UploadedFile $uploadedFile): bool |
78
|
|
|
{ |
79
|
|
|
$violations = $this->fileUploader->validate($uploadedFile); |
80
|
|
|
|
81
|
|
|
if ($violations->count() > 0) { |
82
|
|
|
/** @var ConstraintViolation $violation */ |
83
|
|
|
$violation = $violations[0]; |
84
|
|
|
$this->addFlash('danger', $violation->getMessage()); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Reset a header image to the default image. |
94
|
|
|
* |
95
|
|
|
* @param string $type |
96
|
|
|
* @param Request $request |
97
|
|
|
*/ |
98
|
|
|
public function resetImage(string $type = 'header_image', Request $request): void |
99
|
|
|
{ |
100
|
|
|
$setting = $this->repository->findOneBy(['setting_name' => $type]); |
101
|
|
|
|
102
|
|
|
if ($setting && $this->isCsrfTokenValid('delete', $request->request->get('token'))) { |
103
|
|
|
// Find filename |
104
|
|
|
$filename = $setting->getSettingValue(); |
105
|
|
|
|
106
|
|
|
if ($filename) { |
107
|
|
|
// Delete |
108
|
|
|
$this->deleteImage($filename, $type); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Delete header image. |
115
|
|
|
* |
116
|
|
|
* @param string $filename |
117
|
|
|
* @param string $type |
118
|
|
|
*/ |
119
|
|
|
private function deleteImage(string $filename, string $type = 'header_image'): void |
120
|
|
|
{ |
121
|
|
|
// Delete file from folder |
122
|
|
|
$this->fileUploader->remove($filename); |
123
|
|
|
// Delete from db |
124
|
|
|
$this->repository->updateSetting($type, ''); |
125
|
|
|
// Add flash message |
126
|
|
|
$this->addFlash('success', 'message.deleted'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.