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

ProofRequestAdmin::configureRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

290
        $em = $this->getModelManager()->/** @scrutinizer ignore-call */ getEntityManager($this->getClass());
Loading history...
291
        $originalObject = $em->getUnitOfWork()->getOriginalEntityData($object);
292
293
        // Cant change status final
294
        if (in_array($originalObject['status'], array(ProofRequestStatus::ACCEPTED, ProofRequestStatus::REFUSED), true)) {
295
            $object->setStatus($originalObject['status']);
296
        }
297
    }
298
299
    /**
300
     * @return mixed
301
     */
302
    private function getPlayer()
303
    {
304
        /** @var EntityManager $em */
305
        $em = $this->getModelManager()->getEntityManager($this->getClass());
306
        $user = $this->security->getUser();
307
        return $em->getRepository('VideoGamesRecords\CoreBundle\Entity\Player')->getPlayerFromUser($user);
308
    }
309
}
310