Completed
Pull Request — master (#170)
by Simonas
03:06
created

SettingsController::updateValueAction()   B

Complexity

Conditions 2
Paths 8

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 30
rs 8.8571
cc 2
eloc 15
nc 8
nop 2
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 ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue;
15
use ONGR\ElasticsearchBundle\Service\Repository;
16
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation;
17
use ONGR\SettingsBundle\Document\Setting;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * Class SettingsListController. Is used for managing settings in General env.
25
 */
26
class SettingsController extends Controller
27
{
28
    /**
29
     * Renders settings list page.
30
     *
31
     * @param Request $request
32
     *
33
     * @return Response
34
     */
35
    public function listAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        /** @var Repository $repo */
38
        $repo = $this->get($this->getParameter('ongr_settings.repo'));
39
        $className = $repo->getClassName();
40
        $form = $this->createForm($this->getParameter('ongr_settings.type.setting.class'), new $className);
41
42
        return $this->render(
43
            'ONGRSettingsBundle:Settings:list.html.twig',
44
            [
45
                'form' => $form->createView(),
46
            ]
47
        );
48
    }
49
50
    /**
51
     * Setting update action.
52
     *
53
     * @param Request $request
54
     * @param $id
55
     *
56
     * @return JsonResponse
57
     */
58
    public function updateAction(Request $request, $id)
59
    {
60
        try {
61
            /** @var Repository $repo */
62
            $repo = $this->get($this->getParameter('ongr_settings.repo'));
63
64
            /** @var Setting $setting */
65
            $setting = $repo->find($id);
66
67
            $form = $this->createForm($this->getParameter('ongr_settings.type.setting.class'), $setting);
68
            $form->handleRequest($request);
69
            if ($form->isSubmitted() && $form->isValid()) {
70
                $em = $repo->getManager();
71
                $em->persist($setting);
72
                $em->commit();
73
            } else {
74
                return new JsonResponse(
75
                    [
76
                        'error' => true,
77
                        'message' => 'Not valid posted data.'
78
                    ],
79
                    Response::HTTP_BAD_REQUEST
80
                );
81
            }
82
83
            return new JsonResponse(
84
                [
85
                    'error' => false
86
                ]
87
            );
88
89
        } catch (\Exception $e) {
90
            return new JsonResponse(
91
                [
92
                    'error' => true,
93
                    'message' => 'Error occurred please try to update setting again.'
94
                ],
95
                Response::HTTP_BAD_REQUEST
96
            );
97
        }
98
    }
99
100
    /**
101
     * Setting update action.
102
     *
103
     * @param Request $request
104
     * @param $id
105
     *
106
     * @return JsonResponse
107
     */
108
    public function updateValueAction(Request $request, $id)
109
    {
110
        try {
111
            /** @var Repository $repo */
112
            $repo = $this->get($this->getParameter('ongr_settings.repo'));
113
114
            /** @var Setting $setting */
115
            $setting = $repo->find($id);
116
            $setting->setValue($request->get('value'));
117
118
            $em = $repo->getManager();
119
            $em->persist($setting);
120
            $em->commit();
121
122
            return new JsonResponse(
123
                [
124
                    'error' => false
125
                ]
126
            );
127
128
        } catch (\Exception $e) {
129
            return new JsonResponse(
130
                [
131
                    'error' => true,
132
                    'message' => 'Error occurred please try to update setting again.'
133
                ],
134
                Response::HTTP_BAD_REQUEST
135
            );
136
        }
137
    }
138
139
    /**
140
     * Setting delete action
141
     *
142
     * @param Request $request
143
     * @param $id
144
     *
145
     * @return JsonResponse
146
     */
147
    public function deleteAction(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        try {
150
            /** @var Repository $repo */
151
            $repo = $this->get($this->getParameter('ongr_settings.repo'));
152
153
            $repo->remove($id);
154
155
            return new JsonResponse(['error' => false]);
156
157
        } catch (\Exception $e) {
158
            return new JsonResponse(
159
                [
160
                    'error' => true,
161
                    'message' => 'Error occurred please try to delete setting again.'
162
                ]
163
            );
164
        }
165
    }
166
167
    /**
168
     * Setting delete action
169
     *
170
     * @param Request $request
171
     *
172
     * @return JsonResponse
173
     */
174
    public function createAction(Request $request)
175
    {
176
        try {
177
            /** @var Repository $repo */
178
            $repo = $this->get($this->getParameter('ongr_settings.repo'));
179
            $manager = $repo->getManager();
180
            $data = $request->get('setting');
181
182
            $setting = new Setting();
183
            $setting->setName($data['name']);
184
            $setting->setDescription($data['description']);
185
            $setting->setType($data['type']);
186
            $setting->setValue($data['value']);
187
188
            $setting->setProfile($data['profile']);
189
190
            $manager->persist($setting);
191
192
            $manager->commit();
193
194
            return new JsonResponse(['error' => false]);
195
196
        } catch (\Exception $e) {
197
            return new JsonResponse(
198
                [
199
                    'error' => true,
200
                    'message' => 'Error occurred please try to delete setting again.'
201
                ]
202
            );
203
        }
204
    }
205
}
206