Test Setup Failed
Push — master ( 57adac...109a56 )
by Valery
05:54 queued 11s
created

SettingsController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 16
loc 104
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A settings() 16 16 3
A changeHeaderImage() 0 18 3
A uploadHeaderImage() 0 5 1
A uploadLogoImage() 0 5 1
A deleteHeaderImage() 0 7 1
A deleteLogoImage() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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