Passed
Pull Request — main (#7)
by
unknown
11:57
created

ConfigurationController::save()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace Xiidea\EasyConfigBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use Symfony\Component\Form\FormFactoryInterface;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Xiidea\EasyConfigBundle\Exception\FormValidationException;
11
use Xiidea\EasyConfigBundle\Services\Manager\ConfigManager;
12
13
class ConfigurationController extends AbstractController
14
{
15
    /**
16
     * @return Response
17
     */
18
    public function index(ConfigManager $manager): Response
19
    {
20
        $contents = [
21
            'contentHead' => 'Configurations ',
22
            'forms' => $manager->getConfigurationGroupForms(),
23
        ];
24
25
        return $this->render("@XiideaEasyConfig/index.html.twig", $contents);
26
    }
27
28
    /**
29
     * @return Response
30
     */
31
    public function list(ConfigManager $manager): Response
32
    {
33
        $contents = [
34
            'contentHead' => 'All Configuration ',
35
            'configurationGroup' => $manager->getConfigurationGroups(),
36
        ];
37
38
        return $this->render("@XiideaEasyConfig/list.html.twig", $contents);
39
    }
40
41
    /**
42
     * @param  string  $key
43
     * @return Response
44
     */
45
    public function form(string $key, ConfigManager $manager, FormFactoryInterface $formFactory): Response
46
    {
47
        $configurationGroup = $manager->getConfigurationGroup($key);
48
        $configurationGroupData = $manager->getConfigurationsByGroupKey($key);
49
        $form = $configurationGroup->getForm($formFactory, $configurationGroupData);
50
51
        return $this->render('@XiideaEasyConfig/index.html.twig', [
52
            'contentHead' => $configurationGroup->getLabel(),
53
            'forms' => [
54
                [
55
                    'key' => $key,
56
                    'label' => $configurationGroup->getLabel(),
57
                    'form' => $form->createView(),
58
                    'isEditable' => $this->isGranted($configurationGroup->getAuthorSecurityLevels()),
59
                ],
60
            ],
61
        ]);
62
    }
63
64
    /**
65
     * @param $key
66
     * @param  Request  $request
67
     * @return JsonResponse
68
     */
69
    public function save($key, Request $request, ConfigManager $manager): JsonResponse
70
    {
71
        $form = $manager->getConfigurationGroupForm($key);
72
        $form->handleRequest($request);
73
74
        if ($form->isSubmitted() && !$form->isValid()) {
75
            throw new FormValidationException($form);
76
        }
77
78
        if ($form->isValid()) {
79
            $manager->saveGroupData($key, $form);
80
        }
81
82
        return new JsonResponse([
83
            'success' => true,
84
            'message' => $manager->getConfigurationGroupLabel($key).' data updated!',
85
        ]);
86
    }
87
88
    /**
89
     * @param  string  $key
90
     * @return Response
91
     */
92
    public function userForm(string $key, ConfigManager $manager, FormFactoryInterface $formFactory): Response
93
    {
94
        $usernameKey = $manager->concatUsernameWithKey($key);
95
        $configurationGroup = $manager->getConfigurationGroup($usernameKey);
96
        $configurationGroupData = $manager->getUserConfigurationValuesByGroupKey($key);
97
        $form = $configurationGroup->getForm($formFactory, $configurationGroupData);
98
99
        return $this->render('@XiideaEasyConfig/user.html.twig', [
100
            'contentHead' => $configurationGroup->getLabel(),
101
            'forms' => [
102
                [
103
                    'key' => $usernameKey,
104
                    'label' => $configurationGroup->getLabel(),
105
                    'form' => $form->createView(),
106
                    'isEditable' => $this->isGranted($configurationGroup->getAuthorSecurityLevels()),
107
                ],
108
            ],
109
        ]);
110
    }
111
112
    /**
113
     * @param $key
114
     * @param  Request  $request
115
     * @return JsonResponse
116
     */
117
    public function saveUserConfig($key, Request $request, ConfigManager $manager): JsonResponse
118
    {
119
        $form = $manager->getConfigurationGroupForm($key);
120
        $form->handleRequest($request);
121
122
        if ($form->isSubmitted() && !$form->isValid()) {
123
            throw new FormValidationException($form);
124
        }
125
126
        if ($form->isValid()) {
127
            $manager->saveUserGroupData($key, $form);
128
        }
129
130
        return new JsonResponse([
131
            'success' => true,
132
            'message' => $manager->getConfigurationGroupLabel($key).' data updated!',
133
        ]);
134
    }
135
136
    public function getValueByKey(Request $request, ConfigManager $manager): JsonResponse
137
    {
138
        $result = $manager->getValueByKey($request->get('is_global'), $request->get('key'));
139
140
        return new JsonResponse($result);
141
    }
142
}
143