Passed
Push — develop ( dd225a...42c58f )
by BENARD
06:31
created

PlayerChartAdmin::configureRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 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\Datagrid\ProxyQueryInterface;
11
use Sonata\AdminBundle\Form\FormMapper;
12
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
13
use Sonata\AdminBundle\Form\Type\ModelListType;
14
use Sonata\AdminBundle\Route\RouteCollection;
15
use Sonata\AdminBundle\Route\RouteCollectionInterface;
16
use Sonata\AdminBundle\Show\ShowMapper;
17
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;
18
use Sonata\Form\Type\CollectionType;
19
use Symfony\Component\Form\Extension\Core\Type\TextType;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Symfony\Component\Intl\Locale;
22
use VideoGamesRecords\CoreBundle\Event\PlayerChartEvent;
23
use VideoGamesRecords\CoreBundle\Traits\Accessor\SetEventDispacther;
24
use VideoGamesRecords\CoreBundle\Traits\Accessor\SetRequestStack;
25
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
26
27
class PlayerChartAdmin extends AbstractAdmin
28
{
29
    use SetRequestStack;
30
    use SetEventDispacther;
31
32
    protected $baseRouteName = 'vgrcorebundle_admin_player_chart';
33
34
35
    /**
36
     * @return string
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 getLibGroup(): string
48
    {
49
        $locale = Locale::getDefault();
50
        return ($locale == 'fr') ? 'libGroupFr' : 'libGroupEn';
51
    }
52
53
    /**
54
     * @param RouteCollection $collection
55
     */
56
    protected function configureRoutes(RouteCollectionInterface $collection): void
57
    {
58
        $collection
59
            ->remove('create')
60
            ->remove('export');
61
    }
62
63
    /**
64
     * @param ProxyQueryInterface $query
65
     * @return ProxyQueryInterface
66
     */
67
    protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
68
    {
69
        $query = parent::configureQuery($query);
70
71
        $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

71
        $rootAlias = current($query->/** @scrutinizer ignore-call */ getRootAliases());
Loading history...
72
        $query
73
            ->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

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