Completed
Push — master ( 6c15b7...f5f8be )
by Sander
12:00
created

MenuItemAdminListController::addAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Kunstmaan\MenuBundle\Controller;
4
5
use Kunstmaan\AdminBundle\Entity\EntityInterface;
6
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractAdminListConfigurator;
7
use Kunstmaan\AdminListBundle\AdminList\Configurator\AdminListConfiguratorInterface;
8
use Kunstmaan\AdminListBundle\AdminList\ItemAction\SimpleItemAction;
9
use Kunstmaan\AdminListBundle\Controller\AdminListController;
10
use Kunstmaan\MenuBundle\Entity\BaseMenu;
11
use Symfony\Component\Routing\Annotation\Route;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class MenuItemAdminListController extends AdminListController
17
{
18
    /**
19
     * @var AdminListConfiguratorInterface
20
     */
21
    private $configurator;
22
23
    /**
24
     * @param Request $request
25
     * @param int     $menuid
26
     * @param int     $entityId
0 ignored issues
show
Documentation introduced by
Should the type for parameter $entityId not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
27
     *
28
     * @return AbstractAdminListConfigurator
29
     */
30
    public function getAdminListConfigurator(Request $request, $menuid, $entityId = null)
31
    {
32
        if (!isset($this->configurator)) {
33
            $menu = $this->getDoctrine()->getManager()->getRepository(
34
                $this->container->getParameter('kunstmaan_menu.entity.menu.class')
35
            )->find($menuid);
36
            $rootNode = $this->container->get('kunstmaan_admin.domain_configuration')->getRootNode();
37
38
            $configuratorClass = $this->container->getParameter('kunstmaan_menu.adminlist.menuitem_configurator.class');
39
            $this->configurator = new $configuratorClass($this->getEntityManager(), null, $menu);
40
41
            $adminType = $this->container->getParameter('kunstmaan_menu.form.menuitem_admintype.class');
42
            $menuItemClass = $this->container->getParameter('kunstmaan_menu.entity.menuitem.class');
43
            $this->configurator->setAdminType($adminType);
44
            $this->configurator->setAdminTypeOptions(array('menu' => $menu, 'rootNode' => $rootNode, 'menuItemClass' => $menuItemClass, 'entityId' => $entityId, 'locale' => $request->getLocale()));
45
        }
46
47
        return $this->configurator;
48
    }
49
50
    /**
51
     * The index action
52
     *
53
     * @param Request $request
54
     * @param int     $menuid
55
     *
56
     * @return Response
57
     *
58
     * @Route("/{menuid}/items", name="kunstmaanmenubundle_admin_menuitem")
59
     */
60
    public function indexAction(Request $request, $menuid)
61
    {
62
        $menuRepo = $this->getDoctrine()->getManager()->getRepository(
63
            $this->container->getParameter('kunstmaan_menu.entity.menu.class')
64
        );
65
66
        /** @var BaseMenu $menu */
67
        $menu = $menuRepo->find($menuid);
68
        if ($menu->getLocale() != $request->getLocale()) {
69
            /** @var BaseMenu $translatedMenu */
70
            $translatedMenu = $menuRepo->findOneBy(['locale' => $request->getLocale(), 'name' => $menu->getName()]);
71
            $menuid = $translatedMenu->getId();
72
        }
73
74
        $configurator = $this->getAdminListConfigurator($request, $menuid);
75
        $itemRoute = function (EntityInterface $item) use ($menuid) {
76
            return array(
77
                'path' => 'kunstmaanmenubundle_admin_menuitem_move_up',
78
                'params' => array(
79
                    'menuid' => $menuid,
80
                    'item' => $item->getId(),
81
                ),
82
            );
83
        };
84
        $configurator->addItemAction(new SimpleItemAction($itemRoute, 'arrow-up', 'kuma_admin_list.action.move_up'));
85
86
        $itemRoute = function (EntityInterface $item) use ($menuid) {
87
            return array(
88
                'path' => 'kunstmaanmenubundle_admin_menuitem_move_down',
89
                'params' => array(
90
                    'menuid' => $menuid,
91
                    'item' => $item->getId(),
92
                ),
93
            );
94
        };
95
        $configurator->addItemAction(new SimpleItemAction($itemRoute, 'arrow-down', 'kuma_admin_list.action.move_down'));
96
97
        return parent::doIndexAction($configurator, $request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (doIndexAction() instead of indexAction()). Are you sure this is correct? If so, you might want to change this to $this->doIndexAction().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
98
    }
99
100
    /**
101
     * The add action
102
     *
103
     * @Route("/{menuid}/items/add", name="kunstmaanmenubundle_admin_menuitem_add", methods={"GET", "POST"})
104
     *
105
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
106
     */
107
    public function addAction(Request $request, $menuid)
108
    {
109
        return parent::doAddAction($this->getAdminListConfigurator($request, $menuid), null, $request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (doAddAction() instead of addAction()). Are you sure this is correct? If so, you might want to change this to $this->doAddAction().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
110
    }
111
112
    /**
113
     * The edit action
114
     *
115
     * @param int $id
116
     *
117
     * @Route("{menuid}/items/{id}/edit", requirements={"id" = "\d+"}, name="kunstmaanmenubundle_admin_menuitem_edit", methods={"GET", "POST"})
118
     *
119
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
120
     */
121
    public function editAction(Request $request, $menuid, $id)
122
    {
123
        return parent::doEditAction($this->getAdminListConfigurator($request, $menuid, $id), $id, $request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (doEditAction() instead of editAction()). Are you sure this is correct? If so, you might want to change this to $this->doEditAction().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
124
    }
125
126
    /**
127
     * The delete action
128
     *
129
     * @param int $id
130
     *
131
     * @Route("{menuid}/items/{id}/delete", requirements={"id" = "\d+"}, name="kunstmaanmenubundle_admin_menuitem_delete", methods={"GET", "POST"})
132
     *
133
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
134
     */
135
    public function deleteAction(Request $request, $menuid, $id)
136
    {
137
        return parent::doDeleteAction($this->getAdminListConfigurator($request, $menuid), $id, $request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (doDeleteAction() instead of deleteAction()). Are you sure this is correct? If so, you might want to change this to $this->doDeleteAction().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
138
    }
139
140
    /**
141
     * Move an item up in the list.
142
     *
143
     * @Route("{menuid}/items/{item}/move-up", name="kunstmaanmenubundle_admin_menuitem_move_up", methods={"GET"})
144
     *
145
     * @return RedirectResponse
146
     */
147 View Code Duplication
    public function moveUpAction(Request $request, $menuid, $item)
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...
148
    {
149
        $em = $this->getEntityManager();
150
        $repo = $em->getRepository($this->container->getParameter('kunstmaan_menu.entity.menuitem.class'));
151
        $item = $repo->find($item);
152
153
        if ($item) {
154
            $repo->moveUp($item);
155
        }
156
157
        return new RedirectResponse(
158
            $this->generateUrl('kunstmaanmenubundle_admin_menuitem', array('menuid' => $menuid))
159
        );
160
    }
161
162
    /**
163
     * Move an item down in the list.
164
     *
165
     * @Route("{menuid}/items/{item}/move-down", name="kunstmaanmenubundle_admin_menuitem_move_down", methods={"GET"})
166
     *
167
     * @return RedirectResponse
168
     */
169 View Code Duplication
    public function moveDownAction(Request $request, $menuid, $item)
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...
170
    {
171
        $em = $this->getEntityManager();
172
        $repo = $em->getRepository($this->container->getParameter('kunstmaan_menu.entity.menuitem.class'));
173
        $item = $repo->find($item);
174
175
        if ($item) {
176
            $repo->moveDown($item);
177
        }
178
179
        return new RedirectResponse(
180
            $this->generateUrl('kunstmaanmenubundle_admin_menuitem', array('menuid' => $menuid))
181
        );
182
    }
183
}
184