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

CompositionInducementsForm::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Obblm\Core\Form\Team;
4
5
use Obblm\Core\Contracts\InducementInterface;
6
use Obblm\Core\Contracts\RuleHelperInterface;
7
use Obblm\Core\Entity\Team;
8
use Obblm\Core\Helper\TeamHelper;
9
use Obblm\Core\Validator\Constraints\Team\InducementsAmount;
10
use Obblm\Core\Validator\Constraints\Team\InducementsQuantity;
11
use Symfony\Component\Form\AbstractType;
12
use Symfony\Component\Form\DataMapperInterface;
13
use Symfony\Component\Form\Exception\UnexpectedTypeException;
14
use Symfony\Component\Form\FormBuilderInterface;
15
use Symfony\Component\Form\FormInterface;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
class CompositionInducementsForm extends AbstractType implements DataMapperInterface
19
{
20
    public function buildForm(FormBuilderInterface $builder, array $options)
21
    {
22
        /** @var RuleHelperInterface $helper */
23
        $helper = $options['helper'];
24
25
        if ($builder->getData()) {
26
            /** @var Team $team */
27
            $team = $builder->getData();
28
            $budget = TeamHelper::getLastVersion($team)->getTreasure();
29
            if ($team->getCreationOption('inducement_allowed')) {
30
                if ($team->getCreationOption('inducements')) {
31
                    foreach ($team->getCreationOption('inducements') as $key) {
32
                        $inducement = $helper->getInducement($key);
33
                        $budget += $inducement->getValue();
34
                    }
35
                }
36
                $builder->add('inducements', InducementCollection::class, [
37
                    'allow_add' => true,
38
                    'allow_delete' => true,
39
                    'prototype' => true,
40
                    'error_bubbling' => false,
41
                    'entry_type' => InducementType::class,
42
                    'help' => 'obblm.forms.team.fields.inducement_help',
43
                    'help_translation_parameters' => ['%limit%' => $budget],
44
                    'constraints' => [
45
                        new InducementsAmount($budget, $budget),
46
                        new InducementsQuantity($helper)
47
                    ],
48
                    'entry_options' => [
49
                        'label' => 'inducement',
50
                        'choice_value' => 'key',
51
                        'group_by' => 'type',
52
                        'choices' => $helper->getInducementsFor($team, $budget, ['inducements']),
53
                        'choice_translation_domain' => $helper->getAttachedRule()->getRuleKey()
54
                    ]
55
                ]);
56
            }
57
58
            $builder->setDataMapper($this);
59
        }
60
    }
61
62
    public function mapDataToForms($viewData, iterable $forms)
63
    {
64
        if ($viewData === null) {
65
            return;
66
        }
67
        if (!$viewData instanceof Team) {
68
            throw new UnexpectedTypeException($viewData, Team::class);
69
        }
70
71
        /** @var FormInterface[] $forms */
72
        $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

72
        $forms = iterator_to_array(/** @scrutinizer ignore-type */ $forms);
Loading history...
73
        if (isset($forms['inducements']) && $viewData->getCreationOption('inducements')) {
74
            $forms['inducements']->setData($viewData->getCreationOption('inducements'));
75
        }
76
    }
77
78
    public function mapFormsToData(iterable $forms, &$viewData)
79
    {
80
        if ($viewData === null) {
81
            return;
82
        }
83
        if (!$viewData instanceof Team) {
84
            throw new UnexpectedTypeException($viewData, Team::class);
85
        }
86
87
        /** @var FormInterface[] $forms */
88
        $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

88
        $forms = iterator_to_array(/** @scrutinizer ignore-type */ $forms);
Loading history...
89
90
        $creationOptions = $viewData->getCreationOptions();
91
        if (isset($forms['inducements'])) {
92
            $inducements = array_map(function (InducementInterface $inducement) {
93
                return $inducement->getKey();
94
            }, $forms['inducements']->getData());
95
            $creationOptions['inducements'] = $inducements;
96
        }
97
        $viewData->setCreationOptions($creationOptions);
0 ignored issues
show
Bug introduced by
It seems like $creationOptions can also be of type null; however, parameter $creationOptions of Obblm\Core\Entity\Team::setCreationOptions() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

97
        $viewData->setCreationOptions(/** @scrutinizer ignore-type */ $creationOptions);
Loading history...
98
    }
99
100
    public function configureOptions(OptionsResolver $resolver)
101
    {
102
        $resolver->setDefaults([
103
            'translation_domain' => 'obblm',
104
            'data_class' => Team::class,
105
            'helper' => null,
106
            'constraints' => [
107
            ]
108
        ]);
109
110
        $resolver->setAllowedTypes('helper', [RuleHelperInterface::class]);
111
    }
112
}
113