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

ActionsMenu::userMenu()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 57
Code Lines 46

Duplication

Lines 12
Ratio 21.05 %

Importance

Changes 0
Metric Value
cc 13
eloc 46
c 0
b 0
f 0
nc 24
nop 2
dl 12
loc 57
rs 6.5962

How to fix   Long Method    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
/*
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\Menu;
13
14
use Knp\Menu\FactoryInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
use Zikula\Common\Translator\TranslatorTrait;
19
use Zikula\GroupsModule\Entity\GroupEntity;
20
use Zikula\GroupsModule\Helper\CommonHelper;
21
22
class ActionsMenu implements ContainerAwareInterface
23
{
24
    use ContainerAwareTrait;
25
    use TranslatorTrait;
26
27
    public function setTranslator($translator)
28
    {
29
        $this->translator = $translator;
30
    }
31
32
    public function adminMenu(FactoryInterface $factory, array $options)
33
    {
34
        $this->setTranslator($this->container->get('translator'));
35
        $permissionApi = $this->container->get('zikula_permissions_module.api.permission');
36
        $defaultGroup = $this->container->get('zikula_extensions_module.api.variable')->get('ZikulaGroupsModule', 'defaultgroup');
37
        $primaryAdminGroup = $this->container->get('zikula_extensions_module.api.variable')->get('ZikulaGroupsModule', 'primaryadmingroup', 2);
38
        /** @var GroupEntity $group */
39
        $group = $options['group'];
40
        $gid = $group->getGid();
41
        $routeParams = ['gid' => $gid];
42
        $menu = $factory->createItem('adminActions');
43
        $menu->setChildrenAttribute('class', 'list-inline');
44
        $menu->addChild($this->__f('Edit ":name" group', [':name' => $group->getName()]), [
45
            'route' => 'zikulagroupsmodule_group_edit',
46
            'routeParameters' => $routeParams,
47
        ])->setAttribute('icon', 'fa fa-pencil');
48
        if ($permissionApi->hasPermission('ZikulaGroupsModule::', $gid . '::', ACCESS_DELETE)
49
            && $gid != $defaultGroup && $gid != $primaryAdminGroup) {
50
            $menu->addChild($this->__f('Delete ":name" group', [':name' => $group->getName()]), [
51
                'route' => 'zikulagroupsmodule_group_remove',
52
                'routeParameters' => $routeParams,
53
            ])->setAttribute('icon', 'fa fa-trash-o');
54
        }
55
        $menu->addChild($this->__('Group membership'), [
56
            'route' => 'zikulagroupsmodule_membership_adminlist',
57
            'routeParameters' => $routeParams,
58
        ])->setAttribute('icon', 'fa fa-users');
59
60
        return $menu;
61
    }
62
63
    public function userMenu(FactoryInterface $factory, array $options)
64
    {
65
        $this->setTranslator($this->container->get('translator'));
66
        $permissionApi = $this->container->get('zikula_permissions_module.api.permission');
67
        /** @var GroupEntity $group */
68
        $group = $options['group'];
69
        $gid = $group->getGid();
70
        $menu = $factory->createItem('userActions');
71
        $menu->setChildrenAttribute('class', 'list-inline');
72
        $requestAttributes = $this->container->get('request')->attributes->all();
73
        $currentUserId = $this->container->get('zikula_users_module.current_user')->get('uid');
74
        if (null !== $currentUserId) {
75
            $currentUser = $this->container->get('zikula_users_module.user_repository')->find($currentUserId);
76
        }
77
78
        if ($permissionApi->hasPermission('ZikulaGroupsModule::', $gid . '::', ACCESS_READ)
79
            && ('zikulagroupsmodule_membership_list' != $requestAttributes['_route'])
80
            && ($group->getGtype() == CommonHelper::GTYPE_PUBLIC
81
                || ($group->getGtype() == CommonHelper::GTYPE_PRIVATE && isset($currentUser) && $group->getUsers()->contains($currentUser)))
82
        ) {
83
            $menu->addChild($this->__f('View membership of ":name" group', [':name' => $group->getName()]), [
84
                'route' => 'zikulagroupsmodule_membership_list',
85
                'routeParameters' => ['gid' => $gid],
86
            ])->setAttribute('icon', 'fa fa-users');
87
        }
88
        if (isset($currentUser)) {
89
            if ($group->getUsers()->contains($currentUser)) {
90
                $menu->addChild($this->__f('Leave ":name" group', [':name' => $group->getName()]), [
91
                    'route' => 'zikulagroupsmodule_membership_leave',
92
                    'routeParameters' => ['gid' => $gid],
93
                ])->setAttribute('icon', 'fa fa-user-times text-danger');
94
            } elseif ($group->getGtype() == CommonHelper::GTYPE_PRIVATE) {
95
                $existingApplication = $this->container->get('zikula_groups_module.group_application_repository')->findOneBy(['group' => $group, 'user' => $currentUser]);
96
                if ($existingApplication) {
97
                    $menu->addChild($this->__('Applied!'));
98 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
                    $menu->addChild($this->__f('Apply to membership of ":name" group', [':name' => $group->getName()]), [
100
                        'route' => 'zikulagroupsmodule_application_create',
101
                        'routeParameters' => ['gid' => $gid],
102
                    ])->setAttribute('icon', 'fa fa-paper-plane');
103
                }
104 View Code Duplication
            } elseif ($group->getState() !== CommonHelper::STATE_CLOSED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
                $menu->addChild($this->__f('Join ":name" group', [':name' => $group->getName()]), [
106
                    'route' => 'zikulagroupsmodule_membership_join',
107
                    'routeParameters' => ['gid' => $gid],
108
                ])->setAttribute('icon', 'fa fa-user-plus text-success');
109
            }
110
        } else {
111
            $returnUrl = $this->container->get('router')->generate('zikulagroupsmodule_membership_list', ['gid' => $gid], UrlGeneratorInterface::ABSOLUTE_URL);
112
            $menu->addChild($this->__('Log in or register'), [
113
                'route' => 'zikulausersmodule_access_login',
114
                'routeParameters' => ['returnUrl' => $returnUrl]
115
            ])->setAttribute('icon', 'fa fa-key');
116
        }
117
118
        return $menu;
119
    }
120
}
121