SettingsController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 14.04 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 12
c 3
b 1
f 0
lcom 1
cbo 3
dl 16
loc 114
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A enableProfileAction() 0 22 3
A listAction() 0 4 1
A updateValueAction() 0 10 1
A deleteAction() 16 16 2
B submitAction() 0 28 5

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
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\SettingsBundle\Controller;
13
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * Class SettingsListController. Is used for managing settings in General env.
21
 */
22
class SettingsController extends Controller
23
{
24
25
    public function enableProfileAction($key)
26
    {
27
        $profileName = $key;
28
29
        $cookie = $this->get('ongr_settings.cookie.active_profiles');
30
        $currentActiveProfiles = $cookie->getValue();
31
32
        if (is_array($currentActiveProfiles) && !array_intersect($currentActiveProfiles, [$profileName])) {
33
            $currentActiveProfiles[] = $profileName;
34
            $cookie->setValue($currentActiveProfiles);
35
        } else {
36
            $cookie->setValue([$profileName]);
37
        }
38
39
        $settingsManager = $this->get('ongr_settings.settings_manager');
40
        $settings = $settingsManager->getProfileSettings($profileName);
41
42
        return $this->render('ONGRSettingsBundle:Settings:enableProfile.html.twig', [
43
            'profile' => $profileName,
44
            'profiles' => $settings,
45
        ]);
46
    }
47
48
    /**
49
     * Renders settings list page.
50
     *
51
     * @return Response
52
     */
53
    public function listAction()
54
    {
55
        return $this->render('ONGRSettingsBundle:Settings:list.html.twig');
56
    }
57
58
    /**
59
     * Setting update action.
60
     *
61
     * @param Request $request
62
     *
63
     * @return JsonResponse
64
     */
65
    public function updateValueAction(Request $request)
66
    {
67
        $name = $request->get('name');
68
        $value = $request->get('value');
69
70
        $manager = $this->get('ongr_settings.settings_manager');
71
        $manager->update($name, ['value' => $value]);
72
73
        return new JsonResponse(['error' => false]);
74
    }
75
76
    /**
77
     * Setting delete action
78
     *
79
     * @param Request $request
80
     *
81
     * @return JsonResponse
82
     */
83 View Code Duplication
    public function deleteAction(Request $request)
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...
84
    {
85
        try {
86
            $manager = $this->get('ongr_settings.settings_manager');
87
            $manager->delete($request->get('name'));
88
89
            return new JsonResponse(['error' => false]);
90
        } catch (\Exception $e) {
91
            return new JsonResponse(
92
                [
93
                    'error' => true,
94
                    'message' => 'Error occurred please try to delete setting again.'
95
                ]
96
            );
97
        }
98
    }
99
100
    /**
101
     * Submit action to create or edit setting if not exists.
102
     *
103
     * @param Request $request
104
     *
105
     * @return JsonResponse
106
     */
107
    public function submitAction(Request $request)
108
    {
109
        try {
110
            $manager = $this->get('ongr_settings.settings_manager');
111
            $data = $request->get('setting');
112
113
            if (!empty($data['value']) && !is_string($data['value'])) {
114
                $data['value'] = json_encode($data['value']);
115
            }
116
117
            if ($request->get('force')) {
118
                $name = $request->get('name');
119
                $manager->update($name, $data);
120
            } else {
121
                $manager->create($data);
122
            }
123
124
            return new JsonResponse(['error' => false]);
125
        } catch (\Exception $e) {
126
            return new JsonResponse(
127
                [
128
                    'error' => true,
129
                    'message' => 'Error occurred! Something is wrong with provided data. '.
130
                        'Please try to submit form again.'
131
                ]
132
            );
133
        }
134
    }
135
}
136