ItemCRUDController::itemAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 11
nc 3
nop 0
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 Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
9
use Sonata\AdminBundle\Exception\ModelManagerException;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
13
/**
14
 * @author Alexis BUSSIERES <[email protected]>
15
 */
16
class ItemCRUDController extends Controller
17
{
18
    private function deleteItems($items)
19
    {
20
        $menu = $this->get('alpixel_menu.menu.menu');
21
        $menu->deleteItems($items);
22
    }
23
24
    public function batchActionDelete(ProxyQueryInterface $query)
25
    {
26
        $this->admin->checkAccess('batchDelete');
27
28
        try {
29
            $this->deleteItems($query->execute());
30
            $this->addFlash('sonata_flash_success', 'flash_batch_delete_success');
31
        } catch (ModelManagerException $e) {
0 ignored issues
show
Bug introduced by
The class Sonata\AdminBundle\Exception\ModelManagerException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
32
            $this->handleModelManagerException($e);
33
            $this->addFlash('sonata_flash_error', 'flash_batch_delete_error');
34
        }
35
36
        return new RedirectResponse($this->admin->generateUrl(
37
            'list',
38
            array('filter' => $this->admin->getFilterParameters())
39
        ));
40
    }
41
42
    public function deleteAction($id)
43
    {
44
        $request = $this->getRequest();
45
        $object = $this->admin->getObject($id);
46
47
        if (!$object) {
48
            throw $this->createNotFoundException(sprintf('unable to find the object with id : %s', $id));
49
        }
50
51
        $this->admin->checkAccess('delete', $object);
52
53
        $preResponse = $this->preDelete($request, $object);
54
        if ($preResponse !== null) {
55
            return $preResponse;
56
        }
57
58
        if ($this->getRestMethod() == 'DELETE') {
59
            // check the csrf token
60
            $this->validateCsrfToken('sonata.delete');
61
62
            $objectName = $this->admin->toString($object);
63
64
            try {
65
                $this->deleteItems([$object]);
66
67
                if ($this->isXmlHttpRequest()) {
68
                    return $this->renderJson(array('result' => 'ok'), 200, array());
69
                }
70
71
                $this->addFlash(
72
                    'sonata_flash_success',
73
                    $this->admin->trans(
74
                        'flash_delete_success',
75
                        array('%name%' => $this->escapeHtml($objectName)),
76
                        'SonataAdminBundle'
77
                    )
78
                );
79
            } catch (ModelManagerException $e) {
0 ignored issues
show
Bug introduced by
The class Sonata\AdminBundle\Exception\ModelManagerException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
                $this->handleModelManagerException($e);
81
82
                if ($this->isXmlHttpRequest()) {
83
                    return $this->renderJson(array('result' => 'error'), 200, array());
84
                }
85
86
                $this->addFlash(
87
                    'sonata_flash_error',
88
                    $this->admin->trans(
89
                        'flash_delete_error',
90
                        array('%name%' => $this->escapeHtml($objectName)),
91
                        'SonataAdminBundle'
92
                    )
93
                );
94
            }
95
96
            return $this->redirectTo($object);
97
        }
98
99
        return $this->render($this->admin->getTemplate('delete'), array(
100
            'object' => $object,
101
            'action' => 'delete',
102
            'csrf_token' => $this->getCsrfToken('sonata.delete'),
103
        ), null);
104
    }
105
106 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...
107
    {
108
        $request = $this->getRequest();
109
110
        $url = false;
111
        $params = [];
112
113
        if ($object instanceof Item) {
114
            if ($object->getParent() !== null) {
115
                $itemId = $object->getParent()->getId();
116
                $params['list']['item'] = $itemId;
117
                $params['create']['create-item'] = $itemId;
118
            }
119
            $menuId = $object->getMenu()->getId();
120
            $params['list']['menu'] = $menuId;
121
            $params['create']['create-menu'] = $menuId;
122
        } elseif ($object instanceof Menu) {
123
            $menuId = $object->getId();
124
            $params['list']['menu'] = $menuId;
125
            $params['create']['create-menu'] = $menuId;
126
        }
127
128
        if (null !== $request->get('btn_update_and_list')) {
129
            $url = $this->admin->generateUrl('list', $params['list']);
130
        } elseif (null !== $request->get('btn_create_and_list')) {
131
            $url = $this->admin->generateUrl('list', $params['list']);
132
        }
133
134
        if (null !== $request->get('btn_create_and_create')) {
135
            if ($this->admin->hasActiveSubClass()) {
136
                $params['create']['subclass'] = $request->get('subclass');
137
            }
138
            $url = $this->admin->generateUrl('create', $params['create']);
139
        }
140
141
        if ($this->getRestMethod() === 'DELETE') {
142
            if ($object instanceof Item) {
143
                $url = $this->admin->generateUrl('list', $params['list']);
144
            } else {
145
                $router = $this->get('router');
146
                $url = $router->generate('admin_alpixel_menu_menu_list');
147
            }
148
        }
149
150
        if (!$url) {
151
            foreach (['edit', 'show'] as $route) {
152
                if ($this->admin->hasRoute($route) && $this->admin->isGranted(strtoupper($route), $object)) {
153
                    $url = $this->admin->generateObjectUrl($route, $object);
154
                    break;
155
                }
156
            }
157
        }
158
159
        if (!$url) {
160
            $router = $this->get('router');
161
            $url = $router->generate('admin_alpixel_menu_menu_list');
162
        }
163
164
        return new RedirectResponse($url);
165
    }
166
167 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...
168
    {
169
        $object = $this->admin->getSubject();
170
        $router = $this->container->get('router');
171
        $parameters = [];
172
173
        if ($object instanceof Menu) {
174
            $parameters['menu'] = $object->getId();
175
        } elseif ($object instanceof Item) {
176
            $parameters['menu'] = $object->getMenu()->getId();
177
            $parameters['item'] = $object->getId();
178
        }
179
180
        $url = $router->generate('admin_alpixel_menu_item_list', $parameters, UrlGeneratorInterface::ABSOLUTE_PATH);
181
182
        return new RedirectResponse($url);
183
    }
184
}
185