Completed
Push — master ( d89e8d...a4e309 )
by Vladimir
04:11
created

MapFormCreator::build()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 101
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 101
rs 8.2857
c 1
b 0
f 0
cc 3
eloc 70
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
                'data'      => $this->editing->getName(),
47
                'required' => true,
48
            ))
49
            ->add('alias', TextType::class, array(
50
                'constraints' => [
51
                    new Length([
52
                        'max' => 40,
53
                    ]),
54
                    new UniqueAlias('Map', $this->editing),
55
                    new Type([
56
                        'type' => 'alpha'
57
                    ]),
58
                ],
59
                'data'     => $this->editing->getAlias(),
60
                'label'    => 'Mapchange Configuration Name',
61
                'required' => true
62
            ))
63
            ->add('description', TextareaType::class, array(
64
                'constraints' => [
65
                    new NotBlank(),
66
                    new Length([
67
                        'min' => 10
68
                    ]),
69
                ],
70
                'data'     => $this->editing->getDescription(),
71
                'required' => true,
72
            ))
73
            ->add('world_size', IntegerType::class, [
74
                'constraints' => [
75
                    new GreaterThanOrEqual([
76
                        'value' => 200
77
                    ]),
78
                ],
79
                'data'     => $this->editing->getWorldSize(),
80
                'label'    => 'BZDB _worldSize',
81
                'required' => true,
82
            ])
83
            ->add('randomly_generated', CheckboxType::class, [
84
                'data'     => $this->editing->isRandomlyGenerated(),
85
                'label'    => 'Map is randomly generated',
86
                'required' => false,
87
            ])
88
            ->add('avatar', FileType::class, array(
89
                'constraints' => new Image(array(
90
                    'minWidth'  => 60,
91
                    'minHeight' => 60,
92
                    'maxSize'   => '8M'
93
                )),
94
                'required' => ($this->editing === null || $this->editing->hasAvatar())
95
            ))
96
            ->add('shot_count', IntegerType::class, [
97
                'constraints' => [
98
                    new GreaterThan([
99
                        'value' => 0
100
                    ]),
101
                ],
102
                'data'     => $this->editing->getShotCount(),
103
                'label'    => 'Max shot count',
104
                'required' => true,
105
            ])
106
            ->add('jumping', CheckboxType::class, [
107
                'data'     => $this->editing->isJumpingEnabled(),
108
                'label'    => 'Map allows jumping',
109
                'required' => false,
110
            ])
111
            ->add('ricochet', CheckboxType::class, [
112
                'data'     => $this->editing->isRicochetEnabled(),
113
                'label'    => 'Map allows ricochet',
114
                'required' => false,
115
            ])
116
            ->add('game_mode', ChoiceType::class, [
117
                'choices' => [
118
                    \Map::GAME_MODE_CTF  => 'CTF',
119
                    \Map::GAME_MODE_AHOD => 'AHOD',
120
                ],
121
                'data'     => $this->editing->getGameMode(),
122
                'multiple' => false,
123
                'label'    => 'Game Mode',
124
            ])
125
        ;
126
127
        if ($this->editing) {
128
            // We are editing the map, not creating it
129
            $builder->add('delete_avatar', SubmitType::class);
130
        }
131
132
        return $builder->add('submit', SubmitType::class, [
133
            'label' => 'Save'
134
        ]);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function enter($form)
141
    {
142
        return
143
            \Map::addMap(
144
                $form->get('name')->getData(),
145
                $form->get('alias')->getData(),
146
                $form->get('description')->getData()
147
            )
148
            ->setAvatarFile($form->get('avatar')->getData())
149
            ->setShotCount($form->get('shot_count')->getData())
150
            ->setJumpingEnabled($form->get('jumping')->getData())
151
            ->setRicochetEnabled($form->get('ricochet')->getData())
152
            ->setWorldSize($form->get('world_size')->getData())
153
            ->setRandomlyGenerated($form->get('randomly_generated')->getData())
154
            ->setGameMode($form->get('game_mode')->getData())
155
        ;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     *
161
     * @param \Map $map
162
     */
163
    public function update($form, $map)
164
    {
165
        $map->setName($form->get('name')->getData());
166
        $map->setAlias($form->get('alias')->getData());
167
        $map->setDescription($form->get('description')->getData());
168
        $map->setWorldSize($form->get('world_size')->getData());
169
        $map->setRandomlyGenerated($form->get('randomly_generated')->getData());
170
        $map->setShotCount($form->get('shot_count')->getData());
171
        $map->setJumpingEnabled($form->get('jumping')->getData());
172
        $map->setRicochetEnabled($form->get('ricochet')->getData());
173
        $map->setGameMode($form->get('game_mode')->getData());
174
175
        if ($form->has('delete_avatar') && $form->get('delete_avatar')->isClicked()) {
176
            $map->resetAvatar();
177
        } else {
178
            $map->setAvatarFile($form->get('avatar')->getData());
179
        }
180
181
        return $map;
182
    }
183
}
184