GroupAdmin::configureFormFields()   C
last analyzed

Complexity

Conditions 11
Paths 20

Size

Total Lines 130
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 91
nc 20
nop 1
dl 0
loc 130
rs 6.0496
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
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Admin;
6
7
use Sonata\AdminBundle\Admin\AbstractAdmin;
8
use Sonata\AdminBundle\Datagrid\DatagridMapper;
9
use Sonata\AdminBundle\Datagrid\ListMapper;
10
use Sonata\AdminBundle\Form\FormMapper;
11
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
12
use Sonata\AdminBundle\Form\Type\ModelListType;
13
use Sonata\AdminBundle\Route\RouteCollectionInterface;
14
use Sonata\AdminBundle\Show\ShowMapper;
15
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;
16
use Sonata\Form\Type\CollectionType;
17
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
18
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
19
use Symfony\Component\Form\Extension\Core\Type\TextType;
20
use Symfony\Component\Intl\Locale;
21
use VideoGamesRecords\CoreBundle\ValueObject\GroupOrderBy;
22
23
final class GroupAdmin extends AbstractAdmin
24
{
25
    protected $baseRouteName = 'vgrcorebundle_admin_group';
26
27
    /**
28
     * @return string
29
     */
30
    private function getLibGame(): string
31
    {
32
        $locale = Locale::getDefault();
33
        return ($locale == 'fr') ? 'libGameFr' : 'libGameEn';
34
    }
35
36
    /**
37
     * @param RouteCollectionInterface $collection
38
     */
39
    protected function configureRoutes(RouteCollectionInterface $collection): void
40
    {
41
        $collection->remove('export')
42
            ->add('copy', $this->getRouterIdParameter() . '/copy')
43
            ->add('add-lib-chart', $this->getRouterIdParameter() . '/add-lib-chart')
44
            ->add('set-video-proof-only', $this->getRouterIdParameter() . '/set-video-proof-only');
45
    }
46
47
    public function configureActionButtons(array $buttonList, string $action, ?object $object = null): array
48
    {
49
        if (in_array($action, ['show', 'edit', 'acl']) && $object) {
50
            $buttonList['copy'] = [
51
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.copy.html.twig',
52
            ];
53
            $buttonList['add-lib-chart'] = [
54
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.add_lib_chart.html.twig',
55
            ];
56
            $buttonList['set-video-proof-only'] = [
57
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.set_video_proof_only.html.twig',
58
            ];
59
            $buttonList['add-chart'] = [
60
                'template' => '@VideoGamesRecordsCore/Admin/Object/Group/btn.add_chart.html.twig',
61
            ];
62
        }
63
        return $buttonList;
64
    }
65
66
    /**
67
     * @param FormMapper $form
68
     */
69
    protected function configureFormFields(FormMapper $form): void
70
    {
71
        $gameOptions = [];
72
        if (($this->hasRequest()) && ($this->isCurrentRoute('create'))) {
73
            $idGame = $this->getRequest()->get('idGame', null);
74
            if ($idGame !== null) {
75
                $this->getRequest()->getSession()->set('vgrcorebundle_admin_group.idGame', $idGame);
76
            }
77
78
            if ($this->getRequest()->getSession()->has('vgrcorebundle_admin_group.idGame')) {
79
                $idGame = $this->getRequest()->getSession()->get('vgrcorebundle_admin_group.idGame');
80
                $entityManager = $this->getModelManager()
81
                    ->getEntityManager('VideoGamesRecords\CoreBundle\Entity\Game');
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on Sonata\AdminBundle\Model\ModelManagerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sonata\AdminBundle\Model\LockInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
                    ->/** @scrutinizer ignore-call */ getEntityManager('VideoGamesRecords\CoreBundle\Entity\Game');
Loading history...
82
                $game = $entityManager->getReference('VideoGamesRecords\CoreBundle\Entity\Game', $idGame);
83
                $gameOptions = ['data' => $game];
84
            }
85
        }
86
87
        $form
88
            // Informations principales - Colonne 1
89
            ->with('section.general.information', [
90
                'class' => 'col-md-6',
91
                'label' => 'section.general.information',
92
                'box_class' => 'box box-primary'
93
            ])
94
            ->add('id', TextType::class, [
95
                'label' => 'group.form.id',
96
                'attr' => [
97
                    'readonly' => true,
98
                ]
99
            ])
100
            ->add('libGroupEn', TextType::class, [
101
                'label' => 'group.form.name.en',
102
                'required' => true,
103
            ])
104
            ->add('libGroupFr', TextType::class, [
105
                'label' => 'group.form.name.fr',
106
                'required' => false,
107
            ]);
108
109
        // Ajout du jeu selon le contexte
110
        if ($this->isCurrentRoute('create') || $this->isCurrentRoute('edit')) {
111
            $btnCatalogue = $this->isCurrentRoute('create');
112
            $form->add(
113
                'game',
114
                ModelListType::class,
115
                array_merge(
116
                    $gameOptions,
117
                    [
118
                        'data_class' => null,
119
                        'btn_add' => false,
120
                        'btn_list' => $btnCatalogue,
121
                        'btn_edit' => false,
122
                        'btn_delete' => false,
123
                        'btn_catalogue' => $btnCatalogue,
124
                        'label' => 'group.form.game',
125
                    ]
126
                )
127
            );
128
        }
129
130
        $form->end()
131
132
            // Configuration - Colonne 2
133
            ->with('section.configuration', [
134
                'class' => 'col-md-6',
135
                'label' => 'section.configuration',
136
                'box_class' => 'box box-success'
137
            ])
138
            ->add('isRank', CheckboxType::class, [
139
                'label' => 'group.form.boolRanking',
140
                'required' => false,
141
            ])
142
            ->add(
143
                'orderBy',
144
                ChoiceType::class,
145
                [
146
                    'label' => 'group.form.orderBy',
147
                    'choices' => GroupOrderBy::getStatusChoices(),
148
                ]
149
            )
150
            ->add('isDlc', CheckboxType::class, [
151
                'label' => 'group.form.isDlc',
152
                'required' => false,
153
            ])
154
            ->end();
155
156
        // Charts - Section complète si présents
157
        $subject = $this->getSubject();
158
        if (
159
            (strpos(
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (strpos($this->getReques...ject->getCharts()) < 50, Probably Intended Meaning: strpos($this->getRequest...ect->getCharts()) < 50)
Loading history...
160
                $this->getRequest()
161
                        ->getPathInfo(),
162
                'videogamesrecords/core/group'
163
            ) || (($this->getRequest()
164
                            ->getPathInfo() == '/admin/core/append-form-field-element') && ($this->getRequest(
165
                            )->query->get('_sonata_admin') == 'sonata.admin.vgr.group'))) && (count(
166
                                $subject->getCharts()
167
                            ) < 50)
168
        ) {
169
            $form
170
                ->with('group.form.charts', [
171
                    'class' => 'col-md-12',
172
                    'label' => 'group.form.charts',
173
                    'box_class' => 'box box-info'
174
                ])
175
                ->add(
176
                    'charts',
177
                    CollectionType::class,
178
                    array(
179
                        'label' => 'group.form.charts',
180
                        'by_reference' => false,
181
                        'help' => 'group.form.libs',
182
                        'type_options' => array(
183
                            'delete' => true,
184
                            'delete_options' => array(
185
                                'type' => CheckboxType::class,
186
                                'type_options' => array(
187
                                    'mapped' => false,
188
                                    'required' => false,
189
                                )
190
                            )
191
                        ),
192
                    ),
193
                    array(
194
                        'edit' => 'inline',
195
                        'inline' => 'table',
196
                    )
197
                )
198
                ->end();
199
        }
200
    }
201
202
    /**
203
     * @param DatagridMapper $filter
204
     */
205
    protected function configureDatagridFilters(DatagridMapper $filter): void
206
    {
207
        $filter
208
            ->add('id', null, ['label' => 'group.filter.id'])
209
            ->add('libGroupEn', null, ['label' => 'group.filter.name'])
210
            ->add('libGroupFr', null, ['label' => 'group.filter.name'])
211
            ->add('isDlc', null, ['label' => 'group.filter.isActive'])
212
            ->add(
213
                'game',
214
                ModelFilter::class,
215
                [
216
                    'field_type' => ModelAutocompleteType::class,
217
                    'field_options' => ['property' => $this->getLibGame()],
218
                    'label' => 'group.filter.game'
219
                ]
220
            )
221
        ;
222
    }
223
224
    /**
225
     * @param ListMapper $list
226
     */
227
    protected function configureListFields(ListMapper $list): void
228
    {
229
        $btns = [];
230
        if ($this->hasAccess('create')) {
231
            $btns = [
232
                'copy' => [
233
                    'template' => '@VideoGamesRecordsCore/Admin/Object/Group/link.copy.html.twig'
234
                ],
235
                'add_chart' => [
236
                    'template' => '@VideoGamesRecordsCore/Admin/Object/Group/link.add_chart.html.twig'
237
                ],
238
            ];
239
        }
240
241
        $list
242
            ->addIdentifier('id', null, ['label' => 'group.list.id'])
243
            ->add('libGroupEn', null, ['label' => 'group.list.group.en', 'editable' => true])
244
            ->add('libGroupFr', null, ['label' => 'group.list.group.fr', 'editable' => true])
245
            ->add('nbChart', null, ['label' => 'group.list.nbChart'])
246
            ->add('game', null, [
247
                'associated_property' => $this->getLibGame(),
248
                'label' => 'group.list.game',
249
            ])
250
            ->add(
251
                'orderBy',
252
                'choice',
253
                [
254
                    'label' => 'group.list.orderBy',
255
                    'editable' => true,
256
                    'choices' => GroupOrderBy::getStatusChoices(),
257
                ]
258
            )
259
            ->add('isDlc', 'boolean', ['label' => 'group.list.isDlc'])
260
            ->add('_action', 'actions', [
261
                'actions' =>
262
                    array_merge(
263
                        [
264
                            'show' => [],
265
                            'edit' => [],
266
                            'groups' => [
267
                                'template' => '@VideoGamesRecordsCore/Admin/Object/Group/link.charts.html.twig'
268
                            ]
269
                        ],
270
                        $btns
271
                    )
272
            ]);
273
    }
274
275
    /**
276
     * @param ShowMapper $show
277
     */
278
    protected function configureShowFields(ShowMapper $show): void
279
    {
280
        $show
281
            // Informations principales - Colonne 1
282
            ->with('section.general.information', [
283
                'class' => 'col-md-6',
284
                'label' => 'section.general.information',
285
                'box_class' => 'box box-primary'
286
            ])
287
            ->add('id', null, ['label' => 'group.show.id'])
288
            ->add('libGroupEn', null, ['label' => 'group.show.name.en'])
289
            ->add('libGroupFr', null, ['label' => 'group.show.name.fr'])
290
            ->add('game', null, [
291
                'associated_property' => $this->getLibGame(),
292
                'label' => 'group.show.game',
293
            ])
294
            ->end()
295
296
            // Configuration et statistiques - Colonne 2
297
            ->with('section.configuration', [
298
                'class' => 'col-md-6',
299
                'label' => 'section.configuration',
300
                'box_class' => 'box box-success'
301
            ])
302
            ->add('nbChart', null, ['label' => 'group.show.nbChart'])
303
            ->add('isRank', null, ['label' => 'group.show.boolRanking'])
304
            ->add('orderBy', null, ['label' => 'group.show.orderBy'])
305
            ->add('isDlc', null, ['label' => 'group.show.isDlc'])
306
            ->end()
307
308
            // Charts - Section complète
309
            ->with('group.show.charts', [
310
                'class' => 'col-md-12',
311
                'label' => 'group.show.charts',
312
                'box_class' => 'box box-info'
313
            ])
314
            ->add('charts', null, ['label' => 'group.show.charts'])
315
            ->end();
316
    }
317
}
318