Completed
Push — master ( a4e309...273848 )
by Vladimir
10:27
created

MapFormCreator::fill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
/**
3
 * This file contains a form creator for Teams
4
 *
5
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
6
 */
7
8
namespace BZIon\Form\Creator;
9
10
use BZIon\Form\Constraint\UniqueAlias;
11
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
12
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
13
use Symfony\Component\Form\Extension\Core\Type\FileType;
14
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
15
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
16
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
17
use Symfony\Component\Form\Extension\Core\Type\TextType;
18
use Symfony\Component\Validator\Constraints\GreaterThan;
19
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
20
use Symfony\Component\Validator\Constraints\Image;
21
use Symfony\Component\Validator\Constraints\Length;
22
use Symfony\Component\Validator\Constraints\NotBlank;
23
use Symfony\Component\Validator\Constraints\Type;
24
25
/**
26
 * Form creator for teams
27
 *
28
 * @property \Map $editing
29
 */
30
class MapFormCreator extends ModelFormCreator
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function build($builder)
36
    {
37
        $builder
38
            ->add('name', TextType::class, array(
39
                'constraints' => [
40
                    new NotBlank(),
41
                    new Length([
42
                        'min' => 2,
43
                        'max' => 40,
44
                    ]),
45
                ],
46
                'required' => true,
47
            ))
48
            ->add('alias', TextType::class, array(
49
                'constraints' => [
50
                    new Length([
51
                        'max' => 40,
52
                    ]),
53
                    new UniqueAlias('Map', $this->editing),
54
                    new Type([
55
                        'type' => 'alpha'
56
                    ]),
57
                ],
58
                'label'    => 'Mapchange Configuration Name',
59
                'required' => true
60
            ))
61
            ->add('description', TextareaType::class, array(
62
                'constraints' => [
63
                    new NotBlank(),
64
                    new Length([
65
                        'min' => 10
66
                    ]),
67
                ],
68
                'required' => true,
69
            ))
70
            ->add('world_size', IntegerType::class, [
71
                'constraints' => [
72
                    new GreaterThanOrEqual([
73
                        'value' => 200
74
                    ]),
75
                ],
76
                'label'    => 'BZDB _worldSize',
77
                'required' => true,
78
            ])
79
            ->add('randomly_generated', CheckboxType::class, [
80
                'label'    => 'Map is randomly generated',
81
                'required' => false,
82
            ])
83
            ->add('avatar', FileType::class, array(
84
                'constraints' => new Image(array(
85
                    'minWidth'  => 60,
86
                    'minHeight' => 60,
87
                    'maxSize'   => '8M'
88
                )),
89
                'required' => ($this->editing === null || $this->editing->hasAvatar())
90
            ))
91
            ->add('shot_count', IntegerType::class, [
92
                'constraints' => [
93
                    new GreaterThan([
94
                        'value' => 0
95
                    ]),
96
                ],
97
                'label'    => 'Max shot count',
98
                'required' => true,
99
            ])
100
            ->add('jumping', CheckboxType::class, [
101
                'label'    => 'Map allows jumping',
102
                'required' => false,
103
            ])
104
            ->add('ricochet', CheckboxType::class, [
105
                'label'    => 'Map allows ricochet',
106
                'required' => false,
107
            ])
108
            ->add('game_mode', ChoiceType::class, [
109
                'choices' => [
110
                    \Map::GAME_MODE_CTF  => 'CTF',
111
                    \Map::GAME_MODE_AHOD => 'AHOD',
112
                ],
113
                'multiple' => false,
114
                'label'    => 'Game Mode',
115
            ])
116
        ;
117
118
        if ($this->editing) {
119
            // We are editing the map, not creating it
120
            $builder->add('delete_avatar', SubmitType::class);
121
        }
122
123
        return $builder->add('submit', SubmitType::class, [
124
            'label' => 'Save'
125
        ]);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     *
131
     * @param \Map $map
132
     */
133
    public function fill($form, $map)
134
    {
135
        $form->get('name')->setData($map->getName());
136
        $form->get('alias')->setData($map->getAlias());
137
        $form->get('description')->setData($map->getDescription());
138
        $form->get('world_size')->setData($map->getWorldSize());
139
        $form->get('randomly_generated')->setData($map->isRandomlyGenerated());
140
        $form->get('shot_count')->setData($map->getShotCount());
141
        $form->get('jumping')->setData($map->isJumpingEnabled());
142
        $form->get('ricochet')->setData($map->isRicochetEnabled());
143
        $form->get('game_mode')->setData($map->getGameMode());
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function enter($form)
150
    {
151
        return
152
            \Map::addMap(
153
                $form->get('name')->getData(),
154
                $form->get('alias')->getData(),
155
                $form->get('description')->getData()
156
            )
157
            ->setAvatarFile($form->get('avatar')->getData())
158
            ->setShotCount($form->get('shot_count')->getData())
159
            ->setJumpingEnabled($form->get('jumping')->getData())
160
            ->setRicochetEnabled($form->get('ricochet')->getData())
161
            ->setWorldSize($form->get('world_size')->getData())
162
            ->setRandomlyGenerated($form->get('randomly_generated')->getData())
163
            ->setGameMode($form->get('game_mode')->getData())
164
        ;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     *
170
     * @param \Map $map
171
     */
172
    public function update($form, $map)
173
    {
174
        $map->setName($form->get('name')->getData());
175
        $map->setAlias($form->get('alias')->getData());
176
        $map->setDescription($form->get('description')->getData());
177
        $map->setWorldSize($form->get('world_size')->getData());
178
        $map->setRandomlyGenerated($form->get('randomly_generated')->getData());
179
        $map->setShotCount($form->get('shot_count')->getData());
180
        $map->setJumpingEnabled($form->get('jumping')->getData());
181
        $map->setRicochetEnabled($form->get('ricochet')->getData());
182
        $map->setGameMode($form->get('game_mode')->getData());
183
184
        if ($form->has('delete_avatar') && $form->get('delete_avatar')->isClicked()) {
185
            $map->resetAvatar();
186
        } else {
187
            $map->setAvatarFile($form->get('avatar')->getData());
188
        }
189
190
        return $map;
191
    }
192
}
193