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

PersonalSettingsController::attachCookies()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 9
nc 6
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\Form\Type\SettingsType;
15
use ONGR\SettingsBundle\Settings\Personal\PersonalSettingsManager;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
21
/**
22
 * Controller for managing General (private) settings.
23
 */
24
class PersonalSettingsController extends Controller
25
{
26
    /**
27
     * Main action for changing settings.
28
     *
29
     * @param Request $request
30
     *
31
     * @return \Symfony\Component\HttpFoundation\Response
32
     * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
33
     */
34
    public function settingsAction(Request $request)
35
    {
36
        $manager = $this->getPersonalSettingsManager();
37
        // Handle form.
38
        $settingsData = $manager->getSettings();//$this->get('stash')->getItem('ongr_settings')->get();
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
        $settingsMap = $manager->getSettingsMap();
40
        $options = [
41
            'settingsStructure' => $settingsMap,
42
        ];
43
44
        $form = $this->createForm(SettingsType::class, $settingsData, $options);
45
        $form->handleRequest($request);
46
        if ($form->isValid()) {
47
            $manager->setSettingsFromForm($form->getData());
48
            $redirectResponse = $this->redirect($request->getUri());
49
            $manager->save();
50
            return $redirectResponse;
51
        }
52
53
        // Build settings layout within categories.
54
        $categoryMap = $manager->getCategoryMap();
55
        foreach ($settingsMap as $settingId => $setting) {
56
            $categoryMap[$setting['category']]['settings'][$settingId] = array_merge(
57
                $setting,
58
                [
59
                    'link' => $request->getUriForPath(
60
                        $this->generateUrl(
61
                            'ongr_settings_personal_settings_change',
62
                            [
63
                                'encodedName' => base64_encode($settingId),
64
                            ]
65
                        )
66
                    ),
67
                ]
68
            );
69
        }
70
71
        // Render.
72
        return $this->render(
73
            'ONGRSettingsBundle:Settings:settings.html.twig',
74
            [
75
                'form' => $form->createView(),
76
                'categories' => $categoryMap,
77
            ]
78
        );
79
    }
80
81
    /**
82
     * Creates new Setting.
83
     *
84
     * @param Request $request     Request to process, not used here.
85
     * @param string  $encodedName Base64 encoded setting name.
86
     *
87
     * @return JsonResponse
88
     */
89
    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...
90
    {
91
        $manager = $this->getPersonalSettingsManager();
92
93
        $name = base64_decode($encodedName);
94
95
        $settingsStructure = $manager->getSettingsMap();
96
        if (array_key_exists($name, $settingsStructure)) {
97
            $settings = $manager->getSettings();
98
            if (array_key_exists($name, $settings)) {
99
                $settings[$name] = !$settings[$name];
100
            } else {
101
                $settings[$name] = true;
102
            }
103
104
            $manager->setSettingsFromStash($settings);
105
            $manager->save();
106
107
            return new JsonResponse();
108
        } else {
109
            return new JsonResponse(Response::$statusTexts[403], 403);
110
        }
111
    }
112
113
    /**
114
     * @return PersonalSettingsManager
115
     */
116
    protected function getPersonalSettingsManager()
117
    {
118
        return $this->get('ongr_settings.settings.personal_settings_manager');
119
    }
120
}
121