Completed
Push — master ( 6eaf7d...374325 )
by Vladimir
02:54
created

MapFormCreator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 118
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 71 3
A enter() 0 14 1
A update() 0 17 3
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\FileType;
13
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
14
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
15
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
use Symfony\Component\Validator\Constraints\Image;
18
use Symfony\Component\Validator\Constraints\Length;
19
use Symfony\Component\Validator\Constraints\NotBlank;
20
use Symfony\Component\Validator\Constraints\Type;
21
22
/**
23
 * Form creator for teams
24
 *
25
 * @property \Map $editing
26
 */
27
class MapFormCreator extends ModelFormCreator
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function build($builder)
33
    {
34
        $builder
35
            ->add('name', TextType::class, array(
36
                'constraints' => [
37
                    new NotBlank(),
38
                    new Length([
39
                        'min' => 2,
40
                        'max' => 40,
41
                    ]),
42
                ],
43
                'data'      => $this->editing->getName(),
44
                'required' => true,
45
            ))
46
            ->add('alias', TextType::class, array(
47
                'constraints' => [
48
                    new Length([
49
                        'max' => 40,
50
                    ]),
51
                    new UniqueAlias('Map', $this->editing),
52
                    new Type([
53
                        'type' => 'alpha'
54
                    ]),
55
                ],
56
                'data'     => $this->editing->getAlias(),
57
                'required' => true
58
            ))
59
            ->add('description', TextareaType::class, array(
60
                'constraints' => [
61
                    new NotBlank(),
62
                    new Length([
63
                        'min' => 10
64
                    ]),
65
                ],
66
                'data'     => $this->editing->getDescription(),
67
                'required' => true,
68
            ))
69
            ->add('avatar', FileType::class, array(
70
                'constraints' => new Image(array(
71
                    'minWidth'  => 60,
72
                    'minHeight' => 60,
73
                    'maxSize'   => '8M'
74
                )),
75
                'required' => ($this->editing === null || $this->editing->hasAvatar())
76
            ))
77
            ->add('shot_count', IntegerType::class, [
78
                'data'     => $this->editing->getShotCount(),
79
                'label'    => 'Max shot count',
80
                'required' => true,
81
            ])
82
            ->add('jumping', CheckboxType::class, [
83
                'data'     => $this->editing->isJumpingEnabled(),
84
                'label'    => 'Map allows jumping',
85
                'required' => false,
86
            ])
87
            ->add('ricochet', CheckboxType::class, [
88
                'data'     => $this->editing->isRicochetEnabled(),
89
                'label'    => 'Map allows ricochet',
90
                'required' => false,
91
            ])
92
        ;
93
94
        if ($this->editing) {
95
            // We are editing the map, not creating it
96
            $builder->add('delete_avatar', SubmitType::class);
97
        }
98
99
        return $builder->add('submit', SubmitType::class, [
100
            'label' => 'Save'
101
        ]);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function enter($form)
108
    {
109
        return
110
            \Map::addMap(
111
                $form->get('name')->getData(),
112
                $form->get('alias')->getData(),
113
                $form->get('description')->getData()
114
            )
115
            ->setAvatarFile($form->get('avatar')->getData())
116
            ->setShotCount($form->get('shot_count')->getData())
117
            ->setJumpingEnabled($form->get('jumping')->getData())
118
            ->setRicochetEnabled($form->get('ricochet')->getData())
119
        ;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     *
125
     * @param \Map $map
126
     */
127
    public function update($form, $map)
128
    {
129
        $map->setName($form->get('name')->getData());
130
        $map->setAlias($form->get('alias')->getData());
131
        $map->setDescription($form->get('description')->getData());
132
        $map->setShotCount($form->get('shot_count')->getData());
133
        $map->setJumpingEnabled($form->get('jumping')->getData());
134
        $map->setRicochetEnabled($form->get('ricochet')->getData());
135
136
        if ($form->has('delete_avatar') && $form->get('delete_avatar')->isClicked()) {
137
            $map->resetAvatar();
138
        } else {
139
            $map->setAvatarFile($form->get('avatar')->getData());
140
        }
141
142
        return $map;
143
    }
144
}
145