Passed
Push — develop ( 9bd1fd...1358ff )
by BENARD
05:41
created

PlayerChartAdmin::preValidate()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 27
rs 9.3554
cc 5
nc 7
nop 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Admin;
4
5
use Sonata\AdminBundle\Admin\AbstractAdmin;
6
use Sonata\AdminBundle\Datagrid\DatagridMapper;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
9
use Sonata\AdminBundle\Form\FormMapper;
10
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
11
use Sonata\AdminBundle\Form\Type\ModelListType;
12
use Sonata\AdminBundle\Route\RouteCollection;
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\DependencyInjection\ContainerInterface;
18
use Symfony\Component\Form\Extension\Core\Type\TextType;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
20
use Symfony\Component\Intl\Locale;
21
22
class PlayerChartAdmin extends AbstractAdmin
23
{
24
    protected $baseRouteName = 'vgrcorebundle_admin_player_chart';
25
26
    private ContainerInterface $container;
27
28
    public function setContainer(ContainerInterface $container): void
29
    {
30
        $this->container = $container;
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    private function getLibGame(): string
37
    {
38
        $locale = Locale::getDefault();
39
        return ($locale == 'fr') ? 'libGameFr' : 'libGameEn';
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    private function getLibGroup(): string
46
    {
47
        $locale = Locale::getDefault();
48
        return ($locale == 'fr') ? 'libGroupFr' : 'libGroupEn';
49
    }
50
51
    /**
52
     * @param RouteCollection $collection
53
     */
54
    protected function configureRoutes(RouteCollectionInterface $collection): void
55
    {
56
        $collection
57
            ->remove('create')
58
            ->remove('export');
59
    }
60
61
    /**
62
     * @param ProxyQueryInterface $query
63
     * @return ProxyQueryInterface
64
     */
65
    protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
66
    {
67
        $query = parent::configureQuery($query);
68
69
        $rootAlias = current($query->getRootAliases());
0 ignored issues
show
Bug introduced by
The method getRootAliases() does not exist on Sonata\AdminBundle\Datagrid\ProxyQueryInterface. It seems like you code against a sub-type of Sonata\AdminBundle\Datagrid\ProxyQueryInterface such as Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery. ( Ignorable by Annotation )

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

69
        $rootAlias = current($query->/** @scrutinizer ignore-call */ getRootAliases());
Loading history...
70
        $query
71
            ->innerJoin($rootAlias[0] . '.chart', 'chart')
0 ignored issues
show
Bug introduced by
The method innerJoin() does not exist on Sonata\AdminBundle\Datagrid\ProxyQueryInterface. It seems like you code against a sub-type of Sonata\AdminBundle\Datagrid\ProxyQueryInterface such as Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery. ( Ignorable by Annotation )

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

71
            ->/** @scrutinizer ignore-call */ 
72
              innerJoin($rootAlias[0] . '.chart', 'chart')
Loading history...
72
            ->addSelect('chart')
73
            ->innerJoin($rootAlias[0] . '.player', 'player')
74
            ->addSelect('player')
75
            ->innerJoin('chart.group', 'grp')
76
            ->addSelect('grp')
77
            ->innerJoin('grp.game', 'game')
78
            ->addSelect('game');
79
80
        return $query;
81
    }
82
83
84
    /**
85
     * @param FormMapper $form
86
     */
87
    protected function configureFormFields(FormMapper $form): void
88
    {
89
        $form
90
            ->add('id', TextType::class, [
91
                'label' => 'label.id',
92
                'attr' => [
93
                    'readonly' => true,
94
                ]
95
            ])
96
            ->add('player', ModelListType::class, [
97
                'btn_add' => false,
98
                'btn_list' => false,
99
                'btn_edit' => false,
100
                'btn_delete' => false,
101
                'btn_catalogue' => false,
102
                'label' => 'label.player',
103
            ])
104
            ->add('chart', ModelListType::class, [
105
                'btn_add' => false,
106
                'btn_list' => true,
107
                'btn_edit' => false,
108
                'btn_delete' => false,
109
                'btn_catalogue' => true,
110
                'label' => 'label.chart',
111
            ])
112
            ->add('platform', null, ['label' => 'label.platform'])
113
            ->add('status', null, ['label' => 'label.status'])
114
            ->add('proof', ModelListType::class, [
115
                'btn_add' => false,
116
                'btn_list' => true,
117
                'btn_edit' => false,
118
                'btn_delete' => false,
119
                'btn_catalogue' => true,
120
                'label' => 'label.proof',
121
            ])
122
            ->add('libs', CollectionType::class, array(
123
                'label' => 'label.libs',
124
                'btn_add' => null,
125
                'by_reference' => false,
126
                'type_options' => array(
127
                    'delete' => false,
128
                )
129
            ), array(
130
                'edit' => 'inline',
131
                'inline' => 'table',
132
            ));
133
    }
134
135
    /**
136
     * @param DatagridMapper $filter
137
     */
138
    protected function configureDatagridFilters(DatagridMapper $filter): void
139
    {
140
        $filter
141
            ->add('id', null, ['label' => 'label.id'])
142
            ->add('status', null, ['label' => 'label.status'])
143
            ->add(
144
                'player', ModelFilter::class, [
145
                    'label' => 'label.player',
146
                    'field_type' => ModelAutocompleteType::class,
147
                    'field_options' => ['property' => 'pseudo'],
148
                ]
149
            )
150
            ->add(
151
                'chart.group.game', ModelFilter::class, [
152
                    'label' => 'label.game',
153
                    'field_type' => ModelAutocompleteType::class,
154
                    'field_options' => ['property' => 'libGameEn'],
155
                ]
156
            )
157
            ->add(
158
                'chart.group', ModelFilter::class, [
159
                    'label' => 'label.group',
160
                    'field_type' => ModelAutocompleteType::class,
161
                    'field_options' => ['property' => 'libGroupEn'],
162
                ]
163
            )
164
            ->add('chart.id', null, ['label' => 'label.chart.id'])
165
            ->add('chart.libChartEn', null, ['label' => 'label.name.en'])
166
            ->add('chart.libChartFr', null, ['label' => 'label.name.fr']);
167
    }
168
169
    /**
170
     * @param ListMapper $list
171
     */
172
    protected function configureListFields(ListMapper $list): void
173
    {
174
        $list
175
            ->addIdentifier('id', null, ['label' => 'label.id'])
176
            ->add('player', null, [
177
                'associated_property' => 'pseudo',
178
                'label' => 'label.player',
179
            ])
180
            ->add('chart.group.game', null, [
181
                'associated_property' =>  $this->getLibGame(),
182
                'label' => 'label.game',
183
                'sortable' => true,
184
                'sort_field_mapping' => array(
185
                    'fieldName' => $this->getLibGame()
186
                ),
187
                'sort_parent_association_mappings' => array(
188
                    array('fieldName' => 'chart'),
189
                    array('fieldName' => 'group'),
190
                    array('fieldName' => 'game'),
191
                )
192
            ])
193
            ->add('chart.group', null, [
194
                'associated_property' =>  $this->getLibGroup(),
195
                'label' => 'label.group',
196
                'sortable' => true,
197
                'sort_field_mapping' => array(
198
                    'fieldName' => $this->getLibGroup()
199
                ),
200
                'sort_parent_association_mappings' => array(
201
                    array('fieldName' => 'chart'),
202
                    array('fieldName' => 'group')
203
                )
204
            ])
205
            ->add('chart', null, [
206
                'associated_property' => 'libChartEn',
207
                'label' => 'label.chart',
208
            ])
209
            ->add('status', null, ['label' => 'label.status'])
210
            ->add('libs', null, ['label' => 'label.libs'])
211
            ->add('_action', 'actions', [
212
                'actions' => [
213
                    'edit' => [],
214
                    'show' => [],
215
                ]
216
            ]);
217
    }
218
219
    /**
220
     * @param ShowMapper $show
221
     */
222
    protected function configureShowFields(ShowMapper $show): void
223
    {
224
        $show
225
            ->add('id', null, ['label' => 'label.id'])
226
            ->add('player', null, ['label' => 'label.player'])
227
            ->add('chart', null, ['label' => 'label.chart'])
228
            ->add('status', null, ['label' => 'label.status'])
229
            ->add('dateInvestigation', null, ['label' => 'label.dateInvestigation'])
230
            ->add('proof', null, ['label' => 'label.proof'])
231
            ->add('libs', null, ['label' => 'label.libs']);
232
    }
233
234
    /**
235
     * @param $object
236
     */
237
    public function preValidate($object): void
238
    {
239
        $platform = $object->getPlatform();
240
        $platforms = $object->getChart()->getGroup()->getGame()->getPlatforms();
241
242
        if ($platform !== null) {
243
            $isPlatFormValid = false;
244
            foreach ($platforms as $row) {
245
                if ($platform === $row) {
246
                    $isPlatFormValid = true;
247
                }
248
            }
249
            if (!$isPlatFormValid) {
250
                $this->container->get('session')->getFlashBag()->add(
251
                    'error',
252
                    "Platform is invalid"
253
                );
254
                $response = new RedirectResponse(
255
                    $this->generateUrl(
256
                        'edit',
257
                        array(
258
                            'id' => $object->getId()
259
                        )
260
                    )
261
                );
262
                header('Location: ' . $response->getTargetUrl());
263
                exit;
264
            }
265
        }
266
    }
267
268
    /**
269
     * @param $object
270
     */
271
    public function preUpdate($object): void
272
    {
273
        $chart = $object->getChart();
274
        $chart->setStatusPlayer('MAJ');
275
        $chart->setStatusTeam('MAJ');
276
    }
277
278
279
}
280