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

ProofRequestAdmin::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\ORMException;
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\Form\FormMapper;
14
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
15
use Sonata\AdminBundle\Form\Type\ModelListType;
16
use Sonata\AdminBundle\Route\RouteCollectionInterface;
17
use Sonata\AdminBundle\Show\ShowMapper;
18
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter;
19
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;
20
use Symfony\Bundle\SecurityBundle\Security;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
23
use Symfony\Component\HttpFoundation\RedirectResponse;
24
use Symfony\Component\Intl\Locale;
25
use VideoGamesRecords\CoreBundle\ValueObject\ProofRequestStatus;
26
27
class ProofRequestAdmin extends AbstractAdmin
28
{
29
    protected $baseRouteName = 'vgrcorebundle_admin_proofrequest';
30
31
    /** @var ContainerInterface */
32
    private ContainerInterface $container;
33
34
    private Security $security;
35
36
    public function setContainer(ContainerInterface $container): void
37
    {
38
        $this->container = $container;
39
    }
40
41
    public function setSecurity(Security $security): void
42
    {
43
        $this->security = $security;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    private function getLibGame(): string
50
    {
51
        $locale = Locale::getDefault();
52
        return ($locale == 'fr') ? 'libGameFr' : 'libGameEn';
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    private function getLibChart(): string
59
    {
60
        $locale = Locale::getDefault();
61
        return ($locale == 'fr') ? 'libChartFr' : 'libChartEn';
62
    }
63
64
    /**
65
     * @param RouteCollectionInterface $collection
66
     */
67
    protected function configureRoutes(RouteCollectionInterface $collection): void
68
    {
69
        $collection
70
            ->remove('create')
71
            ->remove('delete')
72
            ->remove('export');
73
    }
74
75
76
    /**
77
     * @param FormMapper $form
78
     */
79
    protected function configureFormFields(FormMapper $form): void
80
    {
81
        $form
82
            ->add(
83
                'playerRequesting',
84
                ModelListType::class,
85
                [
86
                    'data_class' => null,
87
                    'btn_add' => false,
88
                    'btn_list' => false,
89
                    'btn_edit' => false,
90
                    'btn_delete' => false,
91
                    'btn_catalogue' => false,
92
                    'label' => 'label.player.requesting',
93
                ]
94
            )
95
            ->add(
96
                'playerResponding',
97
                ModelListType::class,
98
                [
99
                    'data_class' => null,
100
                    'btn_add' => false,
101
                    'btn_list' => false,
102
                    'btn_edit' => false,
103
                    'btn_delete' => false,
104
                    'btn_catalogue' => false,
105
                    'label' => 'label.player.responding',
106
                ]
107
            )
108
            ->add(
109
                'status',
110
                ChoiceType::class,
111
                [
112
                    'label' => 'label.status',
113
                    'choices' => ProofRequestStatus::getStatusChoices(),
114
                ]
115
            )
116
            ->add('message', CKEditorType::class, [
117
                'label' => 'label.message',
118
                'required' => true,
119
                'attr' => array(
120
                    'readonly' => true,
121
                ),
122
                'config' => array(
123
                    'height' => '100',
124
                    'toolbar' => 'standard'
125
                ),
126
            ])
127
            ->add('response', CKEditorType::class, [
128
                'label' => 'label.proof.response',
129
                'required' => false,
130
                'config' => array(
131
                    'height' => '100',
132
                    'toolbar' => 'standard'
133
                ),
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', ChoiceFilter::class, [
145
                'label' => 'label.status',
146
                'field_type' => ChoiceType::class,
147
                'field_options' => [
148
                    'choices' => ProofRequestStatus::getStatusChoices(),
149
                    'multiple' => false,
150
                ]
151
            ])
152
            ->add('playerChart.player', ModelFilter::class, [
153
                'field_type' => ModelAutocompleteType::class,
154
                'field_options' => ['property' => 'pseudo'],
155
                'label' => 'label.player',
156
            ])
157
            ->add('playerRequesting', ModelFilter::class, [
158
                'field_type' => ModelAutocompleteType::class,
159
                'field_options' => ['property' => 'pseudo'],
160
                'label' => 'label.player.requesting',
161
            ])
162
            ->add('playerResponding', ModelFilter::class, [
163
                'field_type' => ModelAutocompleteType::class,
164
                'field_options' => ['property' => 'pseudo'],
165
                'label' => 'label.player.responding',
166
            ]);
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('createdAt', null, ['label' => 'label.createdAt'])
177
            ->add('playerRequesting', null, [
178
                'associated_property' => 'pseudo',
179
                'label' => 'label.player.requesting',
180
            ])
181
            ->add('playerResponding', null, [
182
                'associated_property' => 'pseudo',
183
                'label' => 'label.player.responding',
184
            ])
185
            ->add('playerChart.player', null, [
186
                'associated_property' =>  'pseudo',
187
                'label' => 'label.player',
188
                'sortable' => true,
189
                'sort_field_mapping' => array(
190
                    'fieldName' => 'pseudo'
191
                ),
192
                'sort_parent_association_mappings' => array(
193
                    array('fieldName' => 'playerChart'),
194
                    array('fieldName' => 'player'),
195
                )
196
            ])
197
            ->add('playerChart.chart.group.game', null, [
198
                'associated_property' =>  $this->getLibGame(),
199
                'label' => 'label.name',
200
                'sortable' => true,
201
                'sort_field_mapping' => array(
202
                    'fieldName' => $this->getLibGame()
203
                ),
204
                'sort_parent_association_mappings' => array(
205
                    array('fieldName' => 'playerChart'),
206
                    array('fieldName' => 'chart'),
207
                    array('fieldName' => 'group'),
208
                    array('fieldName' => 'game'),
209
                )
210
            ])
211
            ->add('playerChart.chart', null, [
212
                'associated_property' =>  $this->getLibChart(),
213
                'label' => 'label.chart',
214
                'sortable' => true,
215
                'sort_field_mapping' => array(
216
                    'fieldName' => $this->getLibChart()
217
                ),
218
                'sort_parent_association_mappings' => array(
219
                    array('fieldName' => 'playerChart'),
220
                    array('fieldName' => 'chart'),
221
                )
222
            ])
223
            ->add('message', 'text', [
224
                'label' => 'label.message',
225
                'header_style' => 'width: 30%'
226
            ])
227
            ->add(
228
                'status',
229
                'choice',
230
                [
231
                    'label' => 'label.status',
232
                    'editable' => true,
233
                    'choices' => ProofRequestStatus::getStatusChoices(),
234
                    'choice_translation_domain' => false,
235
                ]
236
            )
237
            ->add('_action', 'actions', [
238
                'actions' => [
239
                    'show' => [],
240
                    'edit' => [],
241
                    'view_chart' => [
242
                        'template' => '@VideoGamesRecordsCore/Admin/view_chart_link.html.twig'
243
                    ],
244
                ],
245
                'header_style' => 'width: 220px'
246
            ]);
247
    }
248
249
    /**
250
     * @param ShowMapper $show
251
     */
252
    protected function configureShowFields(ShowMapper $show): void
253
    {
254
        $show
255
            ->add('id', null, ['label' => 'label.id'])
256
            ->add('createdAt', null, ['label' => 'label.createdAt'])
257
            ->add('playerRequesting', null, ['label' => 'label.player.requesting'])
258
            ->add('playerResponding', null, ['label' => 'label.player.responding'])
259
            ->add('playerChart', null, ['label' => 'label.playerChart'])
260
            ->add('message', null, ['label' => 'label.message'])
261
            ->add('status', null, ['label' => 'label.status']);
262
    }
263
264
    /**
265
     * @param $object
266
     */
267
    public function preValidate($object): void
268
    {
269
        $player = $this->getPlayer();
270
271
        if ($player) {
272
            if (
273
                ($object->getPlayerRequesting()->getId() === $player->getId())
274
                || ($object->getPlayerChart()->getPlayer()->getId() === $player->getId())
275
            ) {
276
                $this->container->get('session')->getFlashBag()->add(
277
                    'error',
278
                    "You can't update this request"
279
                );
280
281
                $response = new RedirectResponse(
282
                    $this->generateUrl(
283
                        'edit',
284
                        array(
285
                            'id' => $object->getId()
286
                        )
287
                    )
288
                );
289
                header('Location: ' . $response->getTargetUrl());
290
                exit;
291
            }
292
        }
293
    }
294
295
296
    /**
297
     * @param $object
298
     * @throws ORMException
299
     */
300
    public function preUpdate($object): void
301
    {
302
        /** @var EntityManager $em */
303
        $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

303
        $em = $this->getModelManager()->/** @scrutinizer ignore-call */ getEntityManager($this->getClass());
Loading history...
304
        $originalObject = $em->getUnitOfWork()->getOriginalEntityData($object);
305
306
        // Cant change status final
307
        if (in_array($originalObject['status'], array(ProofRequestStatus::ACCEPTED, ProofRequestStatus::REFUSED), true)) {
308
            $object->setStatus($originalObject['status']);
309
        }
310
    }
311
312
    /**
313
     * @return mixed
314
     */
315
    private function getPlayer()
316
    {
317
        /** @var EntityManager $em */
318
        $em = $this->getModelManager()->getEntityManager($this->getClass());
319
        $user = $this->security->getUser();
320
        return $em->getRepository('VideoGamesRecords\CoreBundle\Entity\Player')->getPlayerFromUser($user);
321
    }
322
}
323