CRUDController::redirectTo()   F
last analyzed

Complexity

Conditions 15
Paths 864

Size

Total Lines 60
Code Lines 39

Duplication

Lines 60
Ratio 100 %

Importance

Changes 0
Metric Value
dl 60
loc 60
c 0
b 0
f 0
rs 3.3333
cc 15
eloc 39
nc 864
nop 1

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
namespace Alpixel\Bundle\MenuBundle\Controller;
4
5
use Alpixel\Bundle\MenuBundle\Entity\Item;
6
use Alpixel\Bundle\MenuBundle\Entity\Menu;
7
use Pix\SortableBehaviorBundle\Controller\SortableAdminController as Controller;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
10
11
class CRUDController extends Controller
12
{
13 View Code Duplication
    public function itemAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
14
    {
15
        $object = $this->admin->getSubject();
16
        $router = $this->container->get('router');
17
        $parameters = [];
18
19
        if ($object instanceof Menu) {
20
            $parameters['menu'] = $object->getId();
21
        } elseif ($object instanceof Item) {
22
            $parameters['menu'] = $object->getMenu()->getId();
23
            $parameters['item'] = $object->getId();
24
        }
25
26
        $url = $router->generate('admin_alpixel_menu_item_list', $parameters, UrlGeneratorInterface::ABSOLUTE_PATH);
27
28
        return new RedirectResponse($url);
29
    }
30
31 View Code Duplication
    protected function redirectTo($object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
32
    {
33
        $request = $this->getRequest();
34
35
        $url = false;
36
        $params = [];
37
38
        if ($object instanceof Item) {
39
            if ($object->getParent() !== null) {
40
                $itemId = $object->getParent()->getId();
41
                $params['list']['item'] = $itemId;
42
                $params['create']['create-item'] = $itemId;
43
            }
44
            $menuId = $object->getMenu()->getId();
45
            $params['list']['menu'] = $menuId;
46
            $params['create']['create-menu'] = $menuId;
47
        } elseif ($object instanceof Menu) {
48
            $menuId = $object->getId();
49
            $params['list']['menu'] = $menuId;
50
            $params['create']['create-menu'] = $menuId;
51
        }
52
53
        if (null !== $request->get('btn_update_and_list')) {
54
            $url = $this->admin->generateUrl('list', $params['list']);
55
        } elseif (null !== $request->get('btn_create_and_list')) {
56
            $url = $this->admin->generateUrl('list', $params['list']);
57
        }
58
59
        if (null !== $request->get('btn_create_and_create')) {
60
            if ($this->admin->hasActiveSubClass()) {
61
                $params['create']['subclass'] = $request->get('subclass');
62
            }
63
            $url = $this->admin->generateUrl('create', $params['create']);
64
        }
65
66
        if ($this->getRestMethod() === 'DELETE') {
67
            if ($object instanceof Item) {
68
                $url = $this->admin->generateUrl('list', $params['list']);
69
            } else {
70
                $router = $this->get('router');
71
                $url = $router->generate('admin_alpixel_menu_menu_list');
72
            }
73
        }
74
75
        if (!$url) {
76
            foreach (['edit', 'show'] as $route) {
77
                if ($this->admin->hasRoute($route) && $this->admin->isGranted(strtoupper($route), $object)) {
78
                    $url = $this->admin->generateObjectUrl($route, $object);
79
                    break;
80
                }
81
            }
82
        }
83
84
        if (!$url) {
85
            $router = $this->get('router');
86
            $url = $router->generate('admin_alpixel_menu_menu_list');
87
        }
88
89
        return new RedirectResponse($url);
90
    }
91
}
92