Completed
Branch v1.x-dev (5c2708)
by Benjamin
04:14
created

Skills::buildForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 15
cp 0
rs 9.6
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Obblm\Core\Form\Team\CreationOptions;
4
5
use Obblm\Core\Validator\Constraints\Team\AdditionalSkills;
6
use Symfony\Component\Form\AbstractType;
7
use Symfony\Component\Form\DataMapperInterface;
8
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
9
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
10
use Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\Form\FormInterface;
12
13
class Skills extends AbstractType implements DataMapperInterface
14
{
15
    public function buildForm(FormBuilderInterface $builder, array $options)
16
    {
17
        $builder->add('choice', ChoiceType::class, [
18
                'required' => true,
19
                'choices' => [
20
                    'obblm.forms.team.fields.skills_allowed.choices.'.AdditionalSkills::NONE => AdditionalSkills::NONE,
21
                    'obblm.forms.team.fields.skills_allowed.choices.'.AdditionalSkills::FREE => AdditionalSkills::FREE,
22
                    'obblm.forms.team.fields.skills_allowed.choices.'.AdditionalSkills::NOT_FREE => AdditionalSkills::NOT_FREE
23
                ],
24
                'expanded' => true,
25
            ])
26
            ->add('total', IntegerType::class, [
27
                'required' => false,
28
                'attr' => ['min' => 0]
29
            ])
30
            ->add('double', IntegerType::class, [
31
                'required' => false,
32
                'attr' => ['min' => 0]
33
            ])
34
            ->add('characteristics', IntegerType::class, [
35
                'required' => false,
36
                'attr' => ['min' => 0]
37
            ])
38
            ->add('max_skills_per_player', IntegerType::class, [
39
                'required' => false,
40
                'attr' => ['min' => 0]
41
            ]);
42
        $builder->setDataMapper($this);
43
    }
44
45
    public function mapFormsToData(iterable $forms, &$viewData)
46
    {
47
        if ($viewData === null) {
48
            return;
49
        }
50
51
        /** @var FormInterface[] $forms */
52
        $forms = iterator_to_array($forms);
0 ignored issues
show
Bug introduced by
$forms of type Symfony\Component\Form\FormInterface[] is incompatible with the type Traversable expected by parameter $iterator of iterator_to_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $forms = iterator_to_array(/** @scrutinizer ignore-type */ $forms);
Loading history...
53
54
        $choice = $forms['choice'];
55
56
        if ($choice->getData() == AdditionalSkills::NONE) {
57
            $viewData = false;
58
            return;
59
        }
60
        $total = $forms['total'];
61
        $double = $forms['double'];
62
        $characteristics = $forms['characteristics'];
63
        $viewData['choice'] = $choice->getData();
64
        $viewData['total'] = $total->getData();
65
        $viewData['double'] = $double->getData();
66
        $viewData['characteristics'] = $characteristics->getData();
67
        $viewData['max_skills_per_player'] = $double->getData();
68
    }
69
70
    public function mapDataToForms($viewData, iterable $forms)
71
    {
72
        /** @var FormInterface[] $forms */
73
        $forms = iterator_to_array($forms);
0 ignored issues
show
Bug introduced by
$forms of type Symfony\Component\Form\FormInterface[] is incompatible with the type Traversable expected by parameter $iterator of iterator_to_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $forms = iterator_to_array(/** @scrutinizer ignore-type */ $forms);
Loading history...
74
75
        if ($viewData === null || !isset($viewData['choice'])) {
76
            $forms['choice']->setData(AdditionalSkills::NONE);
77
        }
78
79
        if (isset($viewData['choice']) && $viewData['choice']) {
80
            $forms['choice']->setData($viewData['choice'] ?? AdditionalSkills::NONE);
81
            $forms['total']->setData($viewData['total'] ?? 0);
82
            $forms['double']->setData($viewData['double'] ?? 0);
83
            $forms['characteristics']->setData($viewData['characteristics'] ?? 0);
84
            $forms['max_skills_per_player']->setData($viewData['max_skills_per_player'] ?? 1);
85
            return;
86
        }
87
88
        $forms['choice']->setData(AdditionalSkills::NONE);
89
        $forms['total']->setData(0);
90
        $forms['double']->setData(0);
91
        $forms['characteristics']->setData(0);
92
        $forms['max_skills_per_player']->setData(1);
93
    }
94
95
    public function getBlockPrefix()
96
    {
97
        return 'obblm_creation_allowed_skills';
98
    }
99
}
100