Completed
Push — master ( 2fb092...058edd )
by Craig
06:38
created

AbstractItemActionsMenu::menu()   C

Complexity

Conditions 10
Paths 34

Size

Total Lines 67
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 52
nc 34
nop 2
dl 0
loc 67
rs 6.1506
c 0
b 0
f 0

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
 * Routes.
4
 *
5
 * @copyright Zikula contributors (Zikula)
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 * @author Zikula contributors <[email protected]>.
8
 * @link http://www.zikula.org
9
 * @link http://zikula.org
10
 * @version Generated by ModuleStudio 0.7.1 (http://modulestudio.de).
11
 */
12
13
namespace Zikula\RoutesModule\Menu\Base;
14
15
use Knp\Menu\FactoryInterface;
16
use Knp\Menu\MenuItem;
17
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
19
use Zikula\Common\Translator\TranslatorTrait;
20
use Zikula\RoutesModule\Entity\RouteEntity;
21
22
/**
23
 * This is the item actions menu implementation class.
24
 */
25
class AbstractItemActionsMenu implements ContainerAwareInterface
26
{
27
    use ContainerAwareTrait;
28
    use TranslatorTrait;
29
30
    /**
31
     * Sets the translator.
32
     *
33
     * @param TranslatorInterface $translator Translator service instance
34
     */
35
    public function setTranslator(/*TranslatorInterface */$translator)
36
    {
37
        $this->translator = $translator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $translator of type object<Zikula\RoutesModu...se\TranslatorInterface> is incompatible with the declared type object<Zikula\Common\Translator\Translator> of property $translator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
    }
39
40
    /**
41
     * Builds the menu.
42
     *
43
     * @param FactoryInterface $factory Menu factory
44
     * @param array            $options Additional options
45
     *
46
     * @return MenuItem The assembled menu
47
     */
48
    public function menu(FactoryInterface $factory, array $options)
49
    {
50
        $menu = $factory->createItem('itemActions');
51
        if (!isset($options['entity']) || !isset($options['area']) || !isset($options['context'])) {
52
            return $menu;
53
        }
54
55
        $this->setTranslator($this->container->get('translator.default'));
56
57
        $entity = $options['entity'];
58
        $routeArea = $options['area'];
59
        $context = $options['context'];
60
61
        $permissionApi = $this->container->get('zikula_permissions_module.api.permission');
62
        $currentUserApi = $this->container->get('zikula_users_module.current_user');
0 ignored issues
show
Unused Code introduced by
$currentUserApi is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
        $menu->setChildrenAttribute('class', 'list-inline');
64
65
        if ($entity instanceof RouteEntity) {
66
            $component = 'ZikulaRoutesModule:Route:';
67
            $instance = $entity['id'] . '::';
68
            $routePrefix = 'zikularoutesmodule_route_';
69
        
70
            if ($routeArea == 'admin') {
71
                $menu->addChild($this->__('Preview'), [
72
                    'route' => $routePrefix . 'display',
73
                    'routeParameters' => ['id' => $entity['id']]
74
                ])->setAttribute('icon', 'fa fa-search-plus');
75
                $menu[$this->__('Preview')]->setLinkAttribute('target', '_blank');
76
                $menu[$this->__('Preview')]->setLinkAttribute('title', $this->__('Open preview page'));
77
            }
78
            if ($context != 'display') {
79
                $menu->addChild($this->__('Details'), [
80
                    'route' => $routePrefix . $routeArea . 'display',
81
                    'routeParameters' => ['id' => $entity['id']]
82
                ])->setAttribute('icon', 'fa fa-eye');
83
                $menu[$this->__('Details')]->setLinkAttribute('title', str_replace('"', '', $entity->getTitleFromDisplayPattern()));
84
            }
85
            if ($permissionApi->hasPermission($component, $instance, ACCESS_EDIT)) {
86
                $menu->addChild($this->__('Edit'), [
87
                    'route' => $routePrefix . $routeArea . 'edit',
88
                    'routeParameters' => ['id' => $entity['id']]
89
                ])->setAttribute('icon', 'fa fa-pencil-square-o');
90
                $menu[$this->__('Edit')]->setLinkAttribute('title', $this->__('Edit this route'));
91
                $menu->addChild($this->__('Reuse'), [
92
                    'route' => $routePrefix . $routeArea . 'edit',
93
                    'routeParameters' => ['astemplate' => $entity['id']]
94
                ])->setAttribute('icon', 'fa fa-files-o');
95
                $menu[$this->__('Reuse')]->setLinkAttribute('title', $this->__('Reuse for new route'));
96
            }
97
            if ($permissionApi->hasPermission($component, $instance, ACCESS_DELETE)) {
98
                $menu->addChild($this->__('Delete'), [
99
                    'route' => $routePrefix . $routeArea . 'delete',
100
                    'routeParameters' => ['id' => $entity['id']]
101
                ])->setAttribute('icon', 'fa fa-trash-o');
102
                $menu[$this->__('Delete')]->setLinkAttribute('title', $this->__('Delete this route'));
103
            }
104
            if ($context == 'display') {
105
                $title = $this->__('Back to overview');
106
                $menu->addChild($title, [
107
                    'route' => $routePrefix . $routeArea . 'view'
108
                ])->setAttribute('icon', 'fa fa-reply');
109
                $menu[$title]->setLinkAttribute('title', $title);
110
            }
111
        }
112
113
        return $menu;
114
    }
115
}
116