Completed
Push — master ( 8fc13e...c19aa3 )
by Axel
06:21
created

ConfigController::configAction()   B

Complexity

Conditions 11
Paths 51

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 29
nc 51
nop 3
dl 0
loc 47
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - 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\PermissionsModule\Controller;
15
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
21
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
22
use Zikula\PermissionsModule\Annotation\PermissionCheck;
23
use Zikula\PermissionsModule\Entity\RepositoryInterface\PermissionRepositoryInterface;
24
use Zikula\PermissionsModule\Form\Type\ConfigType;
25
use Zikula\ThemeModule\Engine\Annotation\Theme;
26
27
/**
28
 * Class ConfigController
29
 *
30
 * @Route("/config")
31
 * @PermissionCheck("admin")
32
 */
33
class ConfigController extends AbstractController
34
{
35
    /**
36
     * @Route("/config")
37
     * @Theme("admin")
38
     * @Template("@ZikulaPermissionsModule/Config/config.html.twig")
39
     *
40
     * @return array|Response
41
     */
42
    public function configAction(
43
        Request $request,
44
        VariableApiInterface $variableApi,
45
        PermissionRepositoryInterface $permissionRepository
46
    ) {
47
        $modVars = $variableApi->getAll('ZikulaPermissionsModule');
48
        $modVars['lockadmin'] = (bool)$modVars['lockadmin'];
49
        $modVars['filter'] = (bool)$modVars['filter'];
50
51
        $form = $this->createForm(ConfigType::class, $modVars);
52
        $form->handleRequest($request);
53
        if ($form->isSubmitted() && $form->isValid()) {
54
            if ($form->get('save')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

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

54
            if ($form->get('save')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
55
                $formData = $form->getData();
56
57
                $error = false;
58
59
                $lockadmin = isset($formData['lockadmin']) ? (bool)$formData['lockadmin'] : false;
60
                $variableApi->set('ZikulaPermissionsModule', 'lockadmin', $lockadmin);
61
62
                $adminId = isset($formData['adminid']) ? (int)$formData['adminid'] : 1;
63
                if (0 !== $adminId) {
64
                    $perm = $permissionRepository->find($adminId);
0 ignored issues
show
Bug introduced by
The method find() does not exist on Zikula\PermissionsModule...sionRepositoryInterface. It seems like you code against a sub-type of Zikula\PermissionsModule...sionRepositoryInterface such as Zikula\PermissionsModule...ry\PermissionRepository. ( Ignorable by Annotation )

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

64
                    /** @scrutinizer ignore-call */ 
65
                    $perm = $permissionRepository->find($adminId);
Loading history...
65
                    if (!$perm) {
66
                        $adminId = 0;
67
                        $error = true;
68
                    }
69
                }
70
                $variableApi->set('ZikulaPermissionsModule', 'adminid', $adminId);
71
72
                $filter = isset($formData['filter']) ? (bool)$formData['filter'] : false;
73
                $variableApi->set('ZikulaPermissionsModule', 'filter', $filter);
74
75
                if (true === $error) {
76
                    $this->addFlash('error', 'Error! Could not save configuration: unknown permission rule ID.');
77
                } else {
78
                    $this->addFlash('status', 'Done! Configuration updated.');
79
                }
80
            } elseif ($form->get('cancel')->isClicked()) {
81
                $this->addFlash('status', 'Operation cancelled.');
82
            }
83
84
            return $this->redirectToRoute('zikulapermissionsmodule_permission_list');
85
        }
86
87
        return [
88
            'form' => $form->createView()
89
        ];
90
    }
91
}
92