SettingsService::deleteImage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
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\RequestStack;
14
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
15
use Symfony\Component\Validator\ConstraintViolation;
16
17
final class SettingsService extends AbstractService
18
{
19
    private SettingsRepository $repository;
20
    private FileUploader $fileUploader;
21
22
    public function __construct(
23
        CsrfTokenManagerInterface $tokenManager,
24
        RequestStack $requestStack,
25
        SettingsRepository $repository,
26
        FileUploader $fileUploader
27
    ) {
28
        parent::__construct($tokenManager, $requestStack);
29
        $this->repository = $repository;
30
        $this->fileUploader = $fileUploader;
31
    }
32
33
    /**
34
     * Update settings in database.
35
     */
36
    public function updateSettings(array $formData): void
37
    {
38
        $this->repository->updateSettings($formData);
39
        $this->addFlash('success', 'message.updated');
40
    }
41
42
    /**
43
     * Upload custom header image.
44
     *
45
     * @throws \Exception
46
     */
47
    public function uploadImage(string $type, Request $request): JsonResponse
48
    {
49
        /** @var UploadedFile $uploadedFile */
50
        $uploadedFile = $request->files->get('file');
51
52
        if (!$this->isImageValid($uploadedFile)) {
53
            return new JsonResponse(['status' => 'fail'], 422);
54
        }
55
56
        $fileName = $this->fileUploader->upload($uploadedFile);
57
        $this->repository->updateSetting($type, $fileName);
58
59
        return new JsonResponse(['status' => 'ok']);
60
    }
61
62
    /**
63
     * Validate header image.
64
     */
65
    private function isImageValid(UploadedFile $uploadedFile): bool
66
    {
67
        $violations = $this->fileUploader->validate($uploadedFile);
68
69
        if ($violations->count() > 0) {
70
            /** @var ConstraintViolation $violation */
71
            $violation = $violations[0];
72
            $this->addFlash('danger', $violation->getMessage());
73
74
            return false;
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * Reset a header image to the default image.
82
     */
83
    public function resetImage(string $type, Request $request): void
84
    {
85
        $setting = $this->repository->findOneBy(['setting_name' => $type]);
86
87
        if ($setting && $this->isCsrfTokenValid('delete', $request->request->get('token'))) {
88
            // Find filename
89
            $filename = $setting->getSettingValue();
90
91
            if ($filename) {
92
                // Delete
93
                $this->deleteImage($filename, $type);
94
            }
95
        }
96
    }
97
98
    /**
99
     * Delete header image.
100
     */
101
    private function deleteImage(string $filename, string $type = 'header_image'): void
102
    {
103
        // Delete file from folder
104
        $this->fileUploader->remove($filename);
105
        // Delete from db
106
        $this->repository->updateSetting($type, '');
107
        // Add flash message
108
        $this->addFlash('success', 'message.deleted');
109
    }
110
}
111