Completed
Pull Request — master (#4462)
by Craig
05:44
created

AjaxController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 28
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A changeUserStyleAction() 0 23 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\DefaultTheme\Controller;
15
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Symfony\Component\Yaml\Yaml;
21
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
22
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
23
24
/**
25
 * @Route("/ajax")
26
 */
27
class AjaxController extends AbstractController
28
{
29
    /**
30
     * @Route("/changeUserStyle", methods = {"POST"}, options={"expose"=true})
31
     */
32
    public function changeUserStyleAction(
33
        Request $request,
34
        ZikulaHttpKernelInterface $kernel
35
    ): JsonResponse {
36
        if (!$request->isXmlHttpRequest()) {
37
            return $this->json($this->trans('Only ajax access is allowed!'), Response::HTTP_BAD_REQUEST);
38
        }
39
40
        $themeBundle = $kernel->getBundle('ZikulaDefaultTheme');
41
        $themeVarsPath = $themeBundle->getConfigPath() . '/variables.yaml';
0 ignored issues
show
Bug introduced by
The method getConfigPath() does not exist on Symfony\Component\HttpKe...\Bundle\BundleInterface. It seems like you code against a sub-type of Symfony\Component\HttpKe...\Bundle\BundleInterface such as Zikula\ExtensionsModule\AbstractExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $themeVarsPath = $themeBundle->/** @scrutinizer ignore-call */ getConfigPath() . '/variables.yaml';
Loading history...
42
        $variableDefinitions = Yaml::parse(file_get_contents($themeVarsPath));
43
        $availableStyles = $variableDefinitions['theme_style']['options']['choices'];
44
45
        $style = $request->request->get('style', '');
46
        if (!$style || !in_array($style, array_values($availableStyles), true)) {
47
            return $this->json(['result' => false]);
48
        }
49
50
        if ($request->hasSession() && ($session = $request->getSession())) {
51
            $session->set('currentBootstrapStyle', $style);
52
        }
53
54
        return $this->json(['result' => true]);
55
    }
56
}
57