Passed
Push — develop ( 7ecf79...2fdeaf )
by BENARD
06:23
created

ProofAdmin::setSecurity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
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 Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Exception\NotSupported;
9
use FOS\CKEditorBundle\Form\Type\CKEditorType;
10
use Sonata\AdminBundle\Admin\AbstractAdmin;
11
use Sonata\AdminBundle\Datagrid\DatagridMapper;
12
use Sonata\AdminBundle\Datagrid\ListMapper;
13
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
14
use Sonata\AdminBundle\Form\FormMapper;
15
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
16
use Sonata\AdminBundle\Form\Type\ModelListType;
17
use Sonata\AdminBundle\Route\RouteCollectionInterface;
18
use Sonata\AdminBundle\Show\ShowMapper;
19
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter;
20
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;
21
use Sonata\DoctrineORMAdminBundle\Filter\NullFilter;
22
use Symfony\Bundle\SecurityBundle\Security;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
25
use Symfony\Component\HttpFoundation\RedirectResponse;
26
use Symfony\Component\Intl\Locale;
27
use VideoGamesRecords\CoreBundle\ValueObject\ProofStatus;
28
29
class ProofAdmin extends AbstractAdmin
30
{
31
    protected $baseRouteName = 'vgrcorebundle_admin_proof';
32
33
    /** @var ContainerInterface */
34
    private ContainerInterface $container;
35
36
    private Security $security;
37
38
    public function setContainer(ContainerInterface $container): void
39
    {
40
        $this->container = $container;
41
    }
42
43
    public function setSecurity(Security $security): void
44
    {
45
        $this->security = $security;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    private function getLibGame(): string
52
    {
53
        $locale = Locale::getDefault();
54
        return ($locale == 'fr') ? 'libGameFr' : 'libGameEn';
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    private function getLibGroup(): string
61
    {
62
        $locale = Locale::getDefault();
63
        return ($locale == 'fr') ? 'libGroupFr' : 'libGroupEn';
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    private function getLibChart(): string
70
    {
71
        $locale = Locale::getDefault();
72
        return ($locale == 'fr') ? 'libChartFr' : 'libChartEn';
73
    }
74
75
    /**
76
     * @param ProxyQueryInterface $query
77
     * @return ProxyQueryInterface
78
     */
79
    protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
80
    {
81
        $query = parent::configureQuery($query);
82
        $query->innerJoin($query->getRootAliases()[0] . '.chart', 'chr')
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

82
        $query->innerJoin($query->/** @scrutinizer ignore-call */ getRootAliases()[0] . '.chart', 'chr')
Loading history...
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

82
        $query->/** @scrutinizer ignore-call */ 
83
                innerJoin($query->getRootAliases()[0] . '.chart', 'chr')
Loading history...
83
            ->addSelect('chr')
84
            ->innerJoin('chr.group', 'grp')
85
            ->addSelect('grp')
86
            ->innerJoin('grp.game', 'gam')
87
            ->addSelect('gam');
88
        return $query;
89
    }
90
91
    /**
92
     * @param RouteCollectionInterface $collection
93
     */
94
    protected function configureRoutes(RouteCollectionInterface $collection): void
95
    {
96
        $collection
97
            ->remove('create')
98
            ->remove('delete')
99
            ->remove('export')
100
            ->add('stats', 'stats');
101
    }
102
103
104
    /**
105
     * @param FormMapper $form
106
     */
107
    protected function configureFormFields(FormMapper $form): void
108
    {
109
        $form
110
            ->add(
111
                'playerResponding',
112
                ModelListType::class,
113
                [
114
                    'data_class' => null,
115
                    'btn_add' => false,
116
                    'btn_list' => false,
117
                    'btn_edit' => false,
118
                    'btn_delete' => false,
119
                    'btn_catalogue' => false,
120
                    'label' => 'label.player.responding',
121
                ]
122
            )
123
            ->add(
124
                'playerChart',
125
                ModelListType::class,
126
                [
127
                    'data_class' => null,
128
                    'btn_add' => false,
129
                    'btn_list' => false,
130
                    'btn_edit' => false,
131
                    'btn_delete' => false,
132
                    'btn_catalogue' => false,
133
                    'label' => 'label.playerChart.edit',
134
                ]
135
            )
136
            ->add(
137
                'status',
138
                ChoiceType::class,
139
                [
140
                    'label' => 'label.status',
141
                    'choices' => ProofStatus::getStatusChoices(),
142
                    'choice_translation_domain' => false,
143
                ]
144
            )
145
            ->add('response', CKEditorType::class, [
146
                'label' => 'label.proof.response',
147
                'required' => false,
148
                'config' => array(
149
                    'height' => '100',
150
                    'toolbar' => 'standard'
151
                ),
152
            ]);
153
    }
154
155
    /**
156
     * @param DatagridMapper $filter
157
     */
158
    protected function configureDatagridFilters(DatagridMapper $filter): void
159
    {
160
        $filter
161
            ->add('id', null, ['label' => 'label.id'])
162
            ->add('player', ModelFilter::class, [
163
                'field_type' => ModelAutocompleteType::class,
164
                'field_options' => ['property' => 'pseudo'],
165
                'label' => 'label.player'
166
            ])
167
            ->add('player.pseudo', null, ['label' => 'label.pseudo'])
168
            ->add('chart.group.game', ModelFilter::class, [
169
                'field_type' => ModelAutocompleteType::class,
170
                'field_options' => ['property' => $this->getLibGame()],
171
                'label' => 'label.game'
172
            ])
173
            ->add('chart.group.game.libGameEn', null, ['label' => 'label.game.en'])
174
            ->add('chart.group.game.libGameFr', null, ['label' => 'label.game.fr'])
175
            ->add('status', ChoiceFilter::class, [
176
                'label' => 'label.proof.status',
177
                'field_type' => ChoiceType::class,
178
                'field_options' => [
179
                    'choices' => ProofStatus::getStatusChoices(),
180
                    'multiple' => false,
181
                ]
182
            ])
183
            ->add('playerChart.status', null, ['label' => 'label.playerChart.status'])
184
            ->add('playerResponding', ModelFilter::class, [
185
                'field_type' => ModelAutocompleteType::class,
186
                'field_options' => ['property' => 'pseudo'],
187
                'label' => 'label.player.responding'
188
            ])
189
            ->add('video', NullFilter::class, ['label' => 'label.video_is_null'])
190
            ->add('picture', NullFilter::class, ['label' => 'label.picture_is_null'])
191
        ;
192
    }
193
194
195
    /**
196
     * @param ListMapper $list
197
     */
198
    protected function configureListFields(ListMapper $list): void
199
    {
200
        $list
201
            ->addIdentifier('id', null, ['label' => 'label.id'])
202
            ->add('player', null, [
203
                'associated_property' => 'pseudo',
204
                'label' => 'label.player',
205
            ])
206
            ->add('chart.group.game', null, [
207
                'associated_property' =>  $this->getLibGame(),
208
                'label' => 'label.game',
209
                'sortable' => true,
210
                'sort_field_mapping' => array(
211
                    'fieldName' => $this->getLibGame()
212
                ),
213
                'sort_parent_association_mappings' => array(
214
                    array('fieldName' => 'chart'),
215
                    array('fieldName' => 'group'),
216
                    array('fieldName' => 'game'),
217
                )
218
            ])
219
            ->add('chart.group', null, [
220
                'associated_property' =>  $this->getLibGroup(),
221
                'label' => 'label.group',
222
                'sortable' => true,
223
                'sort_field_mapping' => array(
224
                    'fieldName' => $this->getLibGroup()
225
                ),
226
                'sort_parent_association_mappings' => array(
227
                    array('fieldName' => 'chart'),
228
                    array('fieldName' => 'group')
229
                )
230
            ])
231
            ->add('chart', null, [
232
                'associated_property' => $this->getLibChart(),
233
                'label' => 'label.chart',
234
            ])
235
            ->addIdentifier('type', null, ['label' => 'label.type'])
236
            ->add('playerResponding', null, [
237
                'associated_property' => 'pseudo',
238
                'label' => 'label.player.responding',
239
            ])
240
            ->add(
241
                'status',
242
                'choice',
243
                [
244
                    'label' => 'label.status',
245
                    'editable' => true,
246
                    'choices' => ProofStatus::getStatusChoices(),
247
                    'choice_translation_domain' => false,
248
                ]
249
            )
250
            ->add('playerChart.status', null, ['label' => 'label.playerChart.status'])
251
            ->add('created_at', 'datetime', ['label' => 'label.createdAt'])
252
            ->add('_action', 'actions', [
253
                'actions' => [
254
                    'show' => [],
255
                    'edit' => [],
256
                ]
257
            ]);
258
    }
259
260
    /**
261
     * @param ShowMapper $show
262
     */
263
    protected function configureShowFields(ShowMapper $show): void
264
    {
265
        $show
266
            ->add('id', null, ['label' => 'label.id'])
267
            ->add('Player', null, ['label' => 'label.player'])
268
            ->add('chart.group.game', null, array(
269
                'associated_property' => $this->getLibGame(),
270
                'label' => 'label.game',
271
            ))
272
            ->add('chart.group', null, array(
273
                'associated_property' => $this->getLibGroup(),
274
                'label' => 'label.group',
275
            ))
276
            ->add('chart', null, array(
277
                'associated_property' => $this->getLibChart(),
278
                'label' => 'label.chart',
279
            ))
280
            ->add('created_at', 'datetime', ['label' => 'label.createdAt'])
281
            ->add('updated_at', 'datetime', ['label' => 'label.updatedAt'])
282
            ->add('checkedAt', 'datetime', ['label' => 'label.checkedAt'])
283
            ->add('playerChart', null, ['label' => 'label.score'])
284
            ->add('picture', null, ['label' => 'label.picture'])
285
            ->add('video', null, ['label' => 'label.video'])
286
            ->add('playerResponding', null, ['label' => 'label.player.responding'])
287
            ->add('status', null, ['label' => 'label.status'])
288
            ->add('proofRequest.message', null, ['label' => 'label.message']);
289
    }
290
291
    /**
292
     * @param $object
293
     */
294
    public function preValidate($object): void
295
    {
296
        $player = $this->getPlayer();
297
298
        if ($object->getPlayerChart() != null) {
299
            if ($object->getPlayerChart()->getPlayer()->getId() === $player->getId()) {
300
                $this->container->get('session')->getFlashBag()->add(
301
                    'error',
302
                    "You can't update this proof"
303
                );
304
305
                $response = new RedirectResponse(
306
                    $this->generateUrl(
307
                        'edit',
308
                        array(
309
                            'id' => $object->getId()
310
                        )
311
                    )
312
                );
313
                header('Location: ' . $response->getTargetUrl());
314
                exit;
315
            }
316
        }
317
    }
318
319
    /**
320
     * @param $object
321
     * @return void
322
     */
323
    public function preUpdate($object): void
324
    {
325
        /** @var EntityManager $em */
326
        $em = $this->getModelManager()->getEntityManager($this->getClass());
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

326
        $em = $this->getModelManager()->/** @scrutinizer ignore-call */ getEntityManager($this->getClass());
Loading history...
327
        $originalObject = $em->getUnitOfWork()->getOriginalEntityData($object);
328
329
        // Cant change status final (CLOSED & REFUSED)
330
        if (in_array($originalObject['status'], array(ProofStatus::CLOSED, ProofStatus::REFUSED), true)) {
331
            $object->setStatus($originalObject['status']);
332
        }
333
334
335
        if ($object->getPlayerChart() == null) {
336
            $object->setStatus(ProofStatus::CLOSED);
337
        }
338
    }
339
340
    /**
341
     * @return mixed
342
     * @throws NotSupported
343
     */
344
    private function getPlayer()
345
    {
346
        /** @var EntityManager $em */
347
        $em = $this->getModelManager()->getEntityManager($this->getClass());
348
        $user = $this->security->getUser();
349
        return $em->getRepository('VideoGamesRecords\CoreBundle\Entity\Player')->getPlayerFromUser($user);
350
    }
351
}
352