Completed
Push — master ( f5c851...12cc61 )
by Craig
07:00
created

ConfigController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 49
rs 10
wmc 6
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B configAction() 0 35 6
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\GroupsModule\Controller;
13
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19
use Zikula\Core\Controller\AbstractController;
20
use Zikula\GroupsModule\Form\Type\ConfigType;
21
use Zikula\ThemeModule\Engine\Annotation\Theme;
22
23
/**
24
 * Class ConfigController
25
 * @Route("/config")
26
 */
27
class ConfigController extends AbstractController
28
{
29
    /**
30
     * @Route("/config")
31
     * @Theme("admin")
32
     * @Template
33
     *
34
     * This is a standard function to modify the configuration parameters of the module.
35
     *
36
     * @param Request $request
37
     * @throws AccessDeniedException Thrown if the user doesn't have admin access to the module
38
     * @return array|RedirectResponse
39
     */
40
    public function configAction(Request $request)
41
    {
42
        if (!$this->hasPermission('ZikulaGroupsModule::', '::', ACCESS_ADMIN)) {
43
            throw new AccessDeniedException();
44
        }
45
46
        // build a groups array suitable for the form choices
47
        $groupsList = [];
48
        $groups = $this->get('zikula_groups_module.group_repository')->findAll();
49
        foreach ($groups as $group) {
50
            $groupsList[$group->getName()] = $group->getGid();
51
        }
52
53
        $form = $this->createForm(ConfigType::class, $this->getVars(), [
54
                'translator' => $this->get('translator.default'),
55
                'groups' => $groupsList
56
            ]
57
        );
58
59
        if ($form->handleRequest($request)->isValid()) {
60
            if ($form->get('save')->isClicked()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Form\FormInterface as the method isClicked() does only exist in the following implementations of said interface: Symfony\Component\Form\SubmitButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
                $this->setVars($form->getData());
62
                $this->addFlash('status', $this->__('Done! Module configuration updated.'));
63
            }
64
            if ($form->get('cancel')->isClicked()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Form\FormInterface as the method isClicked() does only exist in the following implementations of said interface: Symfony\Component\Form\SubmitButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
65
                $this->addFlash('status', $this->__('Operation cancelled.'));
66
            }
67
68
            return $this->redirectToRoute('zikulagroupsmodule_group_adminlist');
69
        }
70
71
        return [
72
            'form' => $form->createView()
73
        ];
74
    }
75
}
76