Completed
Push — master ( c1ca11...4ddf1e )
by Benjamin
02:36
created

ItemAdmin::configureFormFields()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 58
Code Lines 40

Duplication

Lines 20
Ratio 34.48 %

Importance

Changes 10
Bugs 4 Features 4
Metric Value
c 10
b 4
f 4
dl 20
loc 58
rs 6.5331
cc 12
eloc 40
nc 12
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\Admin\CRUD;
4
5
use Alpixel\Bundle\MenuBundle\Utils\URLChecker;
6
use Doctrine\ORM\EntityRepository;
7
use Sonata\AdminBundle\Admin\Admin;
8
use Sonata\AdminBundle\Datagrid\ListMapper;
9
use Sonata\AdminBundle\Form\FormMapper;
10
use Sonata\AdminBundle\Route\RouteCollection;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class ItemAdmin extends Admin
14
{
15
    public $last_position = 0;
16
17
    protected $datagridValues = [
18
        '_page'       => 1,
19
        '_sort_order' => 'ASC',
20
        '_sort_by'    => 'position',
21
    ];
22
23
    public function setPositionService(\Pix\SortableBehaviorBundle\Services\PositionHandler $positionHandler)
24
    {
25
        $this->positionService = $positionHandler;
26
    }
27
28
    protected function configureRoutes(RouteCollection $collection)
29
    {
30
        $collection->add('move', $this->getRouterIdParameter().'/move/{position}');
31
        $collection->add('item', $this->getRouterIdParameter().'/item');
32
    }
33
34
    public function getPersistentParameters()
35
    {
36
        $parameters = [];
37
        if ($this->getRequest() instanceof Request) {
38
            $query = $this->getRequest()->query;
39
            if ($query->has('menu')) {
40
                $parameters['menu'] = $query->getInt('menu');
41
            }
42
43
            if ($query->has('item')) {
44
                $parameters['item'] = $query->getInt('item');
45
            }
46
        }
47
48
        return $parameters;
49
    }
50
51
    public function createQuery($context = 'list')
52
    {
53
        $query = parent::createQuery($context);
54
        if ($this->getRequest() instanceof Request) {
55
            $requestQuery = $this->getRequest()->query;
56
            if ($requestQuery->has('menu')) {
57
                $menuId = $requestQuery->getInt('menu');
58
                $query
59
                    ->join($query->getRootAlias().'.menu', 'm')
60
                    ->leftJoin($query->getRootAlias().'.parent', 'p')
61
                    ->where('m.id = :menuId')
62
                    ->andwhere('p.id IS NULL')
63
                    ->setParameters([
64
                        'menuId' => $menuId,
65
                    ]);
66
            }
67
68
            if ($requestQuery->has('item')) {
69
                $parentId = $requestQuery->getInt('item');
70
                $query->join($query->getRootAlias().'.parent', 'p')
71
                    ->andWhere('p.id = :parentId')
72
                    ->setParameters([
73
                        'parentId' => $parentId,
74
                    ]);
75
            }
76
        }
77
78
        return $query;
79
    }
80
81
    protected function configureFormFields(FormMapper $formMapper)
82
    {
83
        $subject = $this->getSubject();
84
        $isNew   = ($this->id($subject) === null) ? true : false;
85
86
        if ($isNew === true) {
87
            $idMenu = $this->getRequest()->query->getInt('create-menu');
88
            $idItem = $this->getRequest()->query->getInt('create-item');
89
        } else {
90
            $idMenu  = $subject->getMenu()->getId();
91
            $idItem  = ($subject->getParent() !== null) ? $subject->getParent()->getId() : 0;
92
        }
93
94
        $formMapper
95
            ->add('menu', null, [
96
                'label'         => 'Menu',
97
                'required'      => true,
98
                'property'      => 'name',
99 View Code Duplication
                'query_builder' => function (EntityRepository $entityRepository) use ($idMenu) {
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...
100
                    $query = $entityRepository->createQuerybuilder('m');
101
                    if ($idMenu === 0) {
102
                        return $query;
103
                    }
104
105
                    return $query
106
                        ->where('m.id = :id')
107
                        ->setParameter('id', $idMenu);
108
                },
109
            ]);
110
111
        if ($idItem === 0 && $idMenu === 0 || $isNew === false && $subject->getParent() !== null || $isNew === true && $idItem > 0) {
112
            $formMapper->add('parent', null, [
113
                'label'    => 'Item parent',
114
                'required' => true,
115
                'property' => 'name',
116 View Code Duplication
                'query_builder' => function (EntityRepository $entityRepository) use ($idItem) {
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...
117
                    $query = $entityRepository->createQuerybuilder('i');
118
                    if ($idItem === 0) {
119
                        return $query;
120
                    }
121
122
                    return $query
123
                        ->where('i.id = :id')
124
                        ->setParameter('id', $idItem);
125
                }
126
            ]);
127
        }
128
129
        $formMapper
130
            ->add('name', null, [
131
                'label'    => 'Nom du menu à afficher',
132
                'required' => true,
133
            ])
134
            ->add('uri', 'text', [
135
                'label'    => 'URI',
136
                'required' => true,
137
            ]);
138
    }
139
140
    protected function configureListFields(ListMapper $listMapper)
141
    {
142
        $this->last_position = $this->positionService->getLastPosition($this->getRoot()->getClass());
143
144
        $listMapper
145
            ->add('name', null, [
146
                'label'    => 'Label',
147
                'required' => true,
148
            ])
149
            ->add('uri', null, [
150
                'label'    => 'URI',
151
                'required' => true,
152
            ])
153
            ->add('position', null, [
154
                'label'    => 'Position',
155
                'required' => true,
156
            ])
157
            ->add('_action', 'actions', [
158
                'actions' => [
159
                    'edit' => [],
160
                    'add'  => [
161
                        'template' => 'AlpixelMenuBundle:CRUD:add__action_item.html.twig',
162
                    ],
163
                    'move' => [
164
                        'template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig',
165
                    ],
166
                    'item' => [
167
                        'template' => 'AlpixelMenuBundle:CRUD:list__action_item.html.twig',
168
                    ],
169
                ],
170
            ]);
171
    }
172
173
    public function postUpdate($object = null)
174
    {
175
        if ($object !== null && $object->getUri() !== null) {
176
            $container = $this->getConfigurationPool()->getContainer();
177
            $checker = $container->get('alpixel_menu.utils.url_checker');
178
            $url = $object->getUri();
179
            if ($checker->check($url) === URLChecker::URL_PROBLEM) {
180
                $session = $container->get('session');
181
                $session->getFlashBag()->add('warning', 'Cependant une erreur semble être apparue quand nous avons tenté d\'analyser la page "'.$url.'". Vous devriez vérifier que le lien spécifié n\'affiche aucune erreur.');
182
            }
183
        }
184
    }
185
}
186