ChartAdmin   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 25
eloc 163
c 3
b 0
f 1
dl 0
loc 281
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getLibGame() 0 4 2
A getLibChart() 0 4 2
A getLibGroup() 0 4 2
A configureRoutes() 0 4 1
A configureShowFields() 0 16 1
A prePersist() 0 14 5
C configureFormFields() 0 112 10
A configureDatagridFilters() 0 43 1
A configureListFields() 0 28 1
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\ChoiceFilter;
16
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;
17
use Sonata\Form\Type\CollectionType;
18
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
19
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
20
use Symfony\Component\Form\Extension\Core\Type\TextType;
21
use Symfony\Component\Intl\Locale;
22
use VideoGamesRecords\CoreBundle\Entity\ChartLib;
23
use VideoGamesRecords\CoreBundle\ValueObject\ChartStatus;
24
25
class ChartAdmin extends AbstractAdmin
26
{
27
    protected $baseRouteName = 'vgrcorebundle_admin_chart';
28
29
    /**
30
     * @return string
31
     */
32
    private function getLibGroup(): string
33
    {
34
        $locale = Locale::getDefault();
35
        return ($locale == 'fr') ? 'libGroupFr' : 'libGroupEn';
36
    }
37
38
    private function getLibGame(): string
39
    {
40
        $locale = Locale::getDefault();
41
        return ($locale == 'fr') ? 'libGameFr' : 'libGameEn';
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    private function getLibChart(): string
48
    {
49
        $locale = Locale::getDefault();
50
        return ($locale == 'fr') ? 'libChartFr' : 'libChartEn';
51
    }
52
53
    /**
54
     * @param RouteCollectionInterface $collection
55
     */
56
    protected function configureRoutes(RouteCollectionInterface $collection): void
57
    {
58
        $collection
59
            ->remove('export');
60
    }
61
62
    /**
63
     * @param FormMapper $form
64
     */
65
    protected function configureFormFields(FormMapper $form): void
66
    {
67
        $groupOptions = array();
68
        if (($this->hasRequest()) && ($this->isCurrentRoute('create'))) {
69
            $idGroup = $this->getRequest()->get('idGroup', null);
70
71
            if ($idGroup !== null) {
72
                $this->getRequest()->getSession()->set('vgrcorebundle_admin_chart.idGroup', $idGroup);
73
            }
74
75
            if ($this->getRequest()->getSession()->has('vgrcorebundle_admin_chart.idGroup')) {
76
                $idGroup = $this->getRequest()->getSession()->get('vgrcorebundle_admin_chart.idGroup');
77
                $entityManager = $this->getModelManager()
78
                    ->getEntityManager('VideoGamesRecords\CoreBundle\Entity\Group');
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

78
                    ->/** @scrutinizer ignore-call */ getEntityManager('VideoGamesRecords\CoreBundle\Entity\Group');
Loading history...
79
                $group = $entityManager->getReference('VideoGamesRecords\CoreBundle\Entity\Group', $idGroup);
80
                $groupOptions = array('data' => $group);
81
            }
82
        }
83
84
        $form
85
            ->add('id', TextType::class, array(
86
                'label' => 'label.id',
87
                'attr' => array(
88
                    'readonly' => true,
89
                )
90
            ));
91
92
        if ($this->isCurrentRoute('create') || $this->isCurrentRoute('edit')) {
93
            $btnCalalogue = $this->isCurrentRoute('create');
94
            $form->
95
                add(
96
                    'group',
97
                    ModelListType::class,
98
                    array_merge(
99
                        $groupOptions,
100
                        [
101
                        'data_class' => null,
102
                        'btn_add' => false,
103
                        'btn_list' => $btnCalalogue,
104
                        'btn_edit' => false,
105
                        'btn_delete' => false,
106
                        'btn_catalogue' => $btnCalalogue,
107
                        'label' => 'label.group',
108
                        ]
109
                    )
110
                );
111
        }
112
113
        $form
114
            ->add('libChartEn', TextType::class, [
115
                'label' => 'label.name.en',
116
                'required' => true,
117
            ])
118
            ->add('libChartFr', TextType::class, [
119
                'label' => 'label.name.fr',
120
                'required' => false,
121
            ]);
122
123
        $form->add('isDlc', CheckboxType::class, [
124
            'label' => 'label.isDlc',
125
            'required' => false,
126
        ]);
127
128
        $form->add('isProofVideoOnly', CheckboxType::class, [
129
            'label' => 'label.isProofVideoOnly',
130
            'required' => false,
131
        ]);
132
133
134
        if ($this->isCurrentRoute('create') || $this->isCurrentRoute('edit')) {
135
            $form
136
                ->add(
137
                    'statusPlayer',
138
                    ChoiceType::class,
139
                    array(
140
                        'label' => 'label.chart.statusPlayer',
141
                        'choices' => ChartStatus::getStatusChoices()
142
                    )
143
                )
144
                ->add(
145
                    'statusTeam',
146
                    ChoiceType::class,
147
                    array(
148
                        'label' => 'label.chart.statusTeam',
149
                        'choices' => ChartStatus::getStatusChoices()
150
                    )
151
                );
152
        }
153
154
155
        $form
156
            ->add('libs', CollectionType::class, array(
157
                'label' => 'label.libs',
158
                'by_reference' => false,
159
                'help' => (($this->isCurrentRoute('create')) ?
160
                    'label.libs.help' : ''),
161
                'type_options' => array(
162
                    // Prevents the "Delete" option from being displayed
163
                    'delete' => false,
164
                    'delete_options' => array(
165
                        // You may otherwise choose to put the field but hide it
166
                        'type' => CheckboxType::class,
167
                        // In that case, you need to fill in the options as well
168
                        'type_options' => array(
169
                            'mapped' => false,
170
                            'required' => false,
171
                        )
172
                    )
173
                )
174
            ), array(
175
                'edit' => 'inline',
176
                'inline' => 'table',
177
            ));
178
    }
179
180
    /**
181
     * @param DatagridMapper $filter
182
     */
183
    protected function configureDatagridFilters(DatagridMapper $filter): void
184
    {
185
        $filter
186
            ->add('id', null, ['label' => 'label.id'])
187
            ->add($this->getLibChart(), null, ['label' => 'label.name'])
188
            ->add('group', ModelFilter::class, [
189
                'field_type' => ModelAutocompleteType::class,
190
                'field_options' => ['property' => $this->getLibGroup()],
191
                'label' => 'label.group',
192
            ])
193
            ->add('group.game', ModelFilter::class, [
194
                'field_type' => ModelAutocompleteType::class,
195
                'field_options' => ['property' => $this->getLibGame()],
196
                'label' => 'label.game',
197
            ])
198
            ->add('isDlc', null, ['label' => 'label.isDlc'])
199
            ->add('isProofVideoOnly', null, ['label' => 'label.isProofVideoOnly'])
200
            ->add(
201
                'statusPlayer',
202
                ChoiceFilter::class,
203
                [
204
                    'label' => 'label.chart.statusPlayer',
205
                    'field_type' => ChoiceType::class,
206
                    'field_options' => [
207
                        'choices' => ChartStatus::getStatusChoices(),
208
                        'multiple' => true,
209
                        'expanded' => false,
210
                        'choice_translation_domain' => true,
211
                    ]
212
213
                ]
214
            )
215
            ->add(
216
                'statusTeam',
217
                ChoiceFilter::class,
218
                [
219
                    'label' => 'label.chart.statusTeam',
220
                    'field_type' => ChoiceType::class,
221
                    'field_options' => [
222
                        'choices' => ChartStatus::getStatusChoices(),
223
                        'multiple' => true,
224
                        'expanded' => false,
225
                        'choice_translation_domain' => true,
226
                    ]
227
228
                ]
229
            );
230
    }
231
232
    /**
233
     * @param ListMapper $list
234
     */
235
    protected function configureListFields(ListMapper $list): void
236
    {
237
        $list
238
            ->addIdentifier('id', null, ['label' => 'label.id'])
239
            ->add('libChartEn', null, ['label' => 'label.chart.en', 'editable' => true])
240
            ->add('libChartFr', null, ['label' => 'label.chart.fr', 'editable' => true])
241
            //->add('slug', null, ['label' => 'label.slug'])
242
            ->add('group', null, array(
243
                'associated_property' => $this->getLibGroup(),
244
                'label' => 'label.group',
245
            ))
246
            ->add('group.game', null, array(
247
                'associated_property' => $this->getLibGame(),
248
                'label' => 'label.game',
249
            ))
250
            ->add('isProofVideoOnly', null, ['label' => 'label.isProofVideoOnly', 'editable' => true])
251
            ->add(
252
                'libs',
253
                null,
254
                [
255
                    'label' => 'label.libs',
256
                ]
257
            )
258
            ->add('createdAt', 'datetime', ['label' => 'label.createdAt'])
259
            ->add('_action', 'actions', array(
260
                'actions' => array(
261
                    'show' => array(),
262
                    'edit' => array(),
263
                )
264
            ));
265
    }
266
267
    /**
268
     * @param ShowMapper $show
269
     */
270
    protected function configureShowFields(ShowMapper $show): void
271
    {
272
        $show
273
            ->add('id', null, ['label' => 'label.id'])
274
            ->add('libChartEn', null, ['label' => 'label.name.en'])
275
            ->add('libChartFr', null, ['label' => 'label.name.fr'])
276
            ->add('statusPlayer', null, ['label' => 'label.chart.statusPlayer'])
277
            ->add('statusTeam', null, ['label' => 'label.chart.statusTeam'])
278
            ->add('isDlc', null, ['label' => 'label.isDlc'])
279
            ->add('isProofVideoOnly', null, ['label' => 'label.isProofVideoOnly'])
280
            ->add('libs', null, ['label' => 'label.libs'])
281
            ->add('createdAt', null, ['label' => 'label.createdAt'])
282
            ->add('updatedAt', null, ['label' => 'label.updatedAt'])
283
            ->add('group', null, array(
284
                'associated_property' => $this->getLibGroup(),
285
                'label' => 'label.group',
286
            ));
287
    }
288
289
    /**
290
     * @param $object
291
     */
292
    public function prePersist($object): void
293
    {
294
        $libs = $object->getLibs();
295
        if (count($libs) == 0) {
296
            $group = $object->getGroup();
297
            if ($group !== null) {
298
                $charts = $group->getCharts();
299
                if (count($charts) > 0) {
300
                    $chart = $charts[0];
301
                    foreach ($chart->getLibs() as $oldLib) {
302
                        $newLib = new ChartLib();
303
                        $newLib->setName($oldLib->getName());
304
                        $newLib->setType($oldLib->getType());
305
                        $object->addLib($newLib);
306
                    }
307
                }
308
            }
309
        }
310
    }
311
}
312