Completed
Pull Request — master (#168)
by
unknown
02:57
created

PersonalSettingsController::changeSettingAction()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 26
rs 8.5806
cc 4
eloc 18
nc 5
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\SettingsBundle\Settings\Personal\PersonalSettingsManager;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
/**
21
 * Controller for managing General (private) settings.
22
 */
23
class PersonalSettingsController extends Controller
24
{
25
    /**
26
     * Main action for changing settings.
27
     *
28
     * @param Request $request
29
     *
30
     * @return \Symfony\Component\HttpFoundation\Response
31
     * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
32
     */
33
    public function settingsAction(Request $request)
34
    {
35
        $manager = $this->getPersonalSettingsManager();
36
        // Handle form.
37
        $settingsData = $manager->getSettings();
38
        $settingsMap = $manager->getSettingsMap();
39
40
        if (empty($settingsData)) {
41
            foreach ($settingsMap as $name => $setting) {
42
                $settingsData[$name] = false;
43
            }
44
            $manager->setSettings($settingsData);
45
            $manager->save();
46
        }
47
        // Build settings layout within categories.
48
        $categoryMap = $manager->getCategoryMap();
49
        foreach ($settingsMap as $settingId => $setting) {
50
            $categoryMap[$setting['category']]['settings'][$settingId] = array_merge(
51
                $setting,
52
                [
53
                    'link' => $request->getUriForPath(
54
                        $this->generateUrl(
55
                            'ongr_settings_personal_settings_change',
56
                            [
57
                                'encodedName' => base64_encode($settingId),
58
                            ]
59
                        )
60
                    ),
61
                ]
62
            );
63
        }
64
65
        // Render.
66
        return $this->render(
67
            'ONGRSettingsBundle:Settings:settings.html.twig',
68
            [
69
                'settings' => $settingsData,
70
                'categories' => $categoryMap,
71
            ]
72
        );
73
    }
74
75
    /**
76
     * Creates new Setting.
77
     *
78
     * @param Request $request     Request to process, not used here.
79
     * @param string  $encodedName Base64 encoded setting name.
80
     *
81
     * @return JsonResponse
82
     */
83
    public function changeSettingAction(Request $request, $encodedName)
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...
84
    {
85
        $manager = $this->getPersonalSettingsManager();
86
87
        $name = base64_decode($encodedName);
88
89
        $settingsStructure = $manager->getSettingsMap();
90
        if (array_key_exists($name, $settingsStructure)) {
91
            $settings = $manager->getSettings();
92
            if (array_key_exists($name, $settings)) {
93
                $settings[$name] = !$settings[$name];
94
            } else {
95
                $settings[$name] = true;
96
            }
97
98
            $manager->setSettings($settings);
99
            try {
100
                $manager->save();
101
            } catch (\BadMethodCallException $e) {
102
                return new JsonResponse($e->getMessage(), 400);
103
            }
104
            return new JsonResponse();
105
        } else {
106
            return new JsonResponse(Response::$statusTexts[403], 403);
107
        }
108
    }
109
110
    /**
111
     * @return PersonalSettingsManager
112
     */
113
    protected function getPersonalSettingsManager()
114
    {
115
        $manager = $this->get('ongr_settings.settings.personal_settings_manager');
116
        $manager->setSettingsFromStash();
117
        return $manager;
118
    }
119
}
120