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

CompositionSkillsForm   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 35
c 1
b 0
f 0
dl 0
loc 70
ccs 0
cts 34
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mapFormsToData() 0 7 3
A mapDataToForms() 0 20 5
A buildForm() 0 19 3
A configureOptions() 0 11 1
1
<?php
2
3
namespace Obblm\Core\Form\Team;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Obblm\Core\Contracts\RuleHelperInterface;
7
use Obblm\Core\Entity\TeamVersion;
8
use Obblm\Core\Validator\Constraints\Team\AdditionalSkills;
9
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\DataMapperInterface;
11
use Symfony\Component\Form\Exception\UnexpectedTypeException;
12
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
13
use Symfony\Component\Form\FormBuilderInterface;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
class CompositionSkillsForm extends AbstractType implements DataMapperInterface
18
{
19
    public function buildForm(FormBuilderInterface $builder, array $options)
20
    {
21
        /** @var RuleHelperInterface $helper */
22
        $helper = $options['helper'];
23
24
        if ($builder->getData()) {
25
            /** @var TeamVersion $version */
26
            $version = $builder->getData();
27
            if ($version->getTeam()->getCreationOption('skills_allowed')) {
28
                $builder->add('playerVersions', CollectionType::class, [
29
                    'entry_type' => AddSkills::class,
30
                    'allow_add' => false,
31
                    'allow_delete' => false,
32
                    'mapped' => true,
33
                    'entry_options' => [
34
                        'helper' => $helper
35
                    ]
36
                ]);
37
                $builder->setDataMapper($this);
38
            }
39
        }
40
    }
41
42
43
    public function mapDataToForms($viewData, iterable $forms)
44
    {
45
        if ($viewData === null) {
46
            return;
47
        }
48
        if (!$viewData instanceof TeamVersion) {
49
            throw new UnexpectedTypeException($viewData, TeamVersion::class);
50
        }
51
52
        /** @var FormInterface[] $forms */
53
        $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

53
        $forms = iterator_to_array(/** @scrutinizer ignore-type */ $forms);
Loading history...
54
55
        $players = $viewData->getNotDeadPlayerVersions();
56
        $onlyPositionals = new ArrayCollection();
57
        foreach ($players as $player) {
58
            if (!$player->getPlayer()->isStarPlayer()) {
59
                $onlyPositionals->add($player);
60
            }
61
        }
62
        $forms['playerVersions']->setData($onlyPositionals);
63
    }
64
65
66
    public function mapFormsToData(iterable $forms, &$viewData)
67
    {
68
        if ($viewData === null) {
69
            return;
70
        }
71
        if (!$viewData instanceof TeamVersion) {
72
            throw new UnexpectedTypeException($viewData, TeamVersion::class);
73
        }
74
    }
75
76
    public function configureOptions(OptionsResolver $resolver)
77
    {
78
        $resolver->setDefaults([
79
            'translation_domain' => 'obblm',
80
            'data_class' => TeamVersion::class,
81
            'helper' => null,
82
            'constraints' => [
83
                new AdditionalSkills()
84
            ]
85
        ]);
86
        $resolver->setAllowedTypes('helper', [RuleHelperInterface::class]);
87
    }
88
}
89