Passed
Push — develop ( 0a11b3...ab491c )
by BENARD
05:49
created

PlayerChartAdmin::postUpdate()   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
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
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\EventDispatcher\EventDispatcherInterface;
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\Security\UserProvider;
24
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
25
26
class PlayerChartAdmin extends AbstractAdmin
27
{
28
    protected $baseRouteName = 'vgrcorebundle_admin_player_chart';
29
30
    private ContainerInterface $container;
31
    private readonly EventDispatcherInterface $eventDispatcher;
32
33
    public function setContainer(ContainerInterface $container): void
34
    {
35
        $this->container = $container;
36
    }
37
38
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
39
    {
40
        $this->eventDispatcher = $eventDispatcher;
0 ignored issues
show
Bug introduced by
The property eventDispatcher is declared read-only in VideoGamesRecords\CoreBu...\Admin\PlayerChartAdmin.
Loading history...
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    private function getLibGame(): string
47
    {
48
        $locale = Locale::getDefault();
49
        return ($locale == 'fr') ? 'libGameFr' : 'libGameEn';
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    private function getLibGroup(): string
56
    {
57
        $locale = Locale::getDefault();
58
        return ($locale == 'fr') ? 'libGroupFr' : 'libGroupEn';
59
    }
60
61
    /**
62
     * @param RouteCollection $collection
63
     */
64
    protected function configureRoutes(RouteCollectionInterface $collection): void
65
    {
66
        $collection
67
            ->remove('create')
68
            ->remove('export');
69
    }
70
71
    /**
72
     * @param ProxyQueryInterface $query
73
     * @return ProxyQueryInterface
74
     */
75
    protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
76
    {
77
        $query = parent::configureQuery($query);
78
79
        $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

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

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