Completed
Push — master ( 9dfdce...c53712 )
by Paweł
26s
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\PromotionBundle\Form\Type;
13
14
use Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistryInterface;
15
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
16
use Sylius\Component\Promotion\Model\ConfigurablePromotionElementInterface;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormEvent;
19
use Symfony\Component\Form\FormEvents;
20
use Symfony\Component\Form\FormInterface;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
23
/**
24
 * @author Kamil Kokot <[email protected]>
25
 */
26
abstract class AbstractConfigurablePromotionElementType extends AbstractResourceType
27
{
28
    /**
29
     * @var FormTypeRegistryInterface
30
     */
31
    private $formTypeRegistry;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function __construct($dataClass, array $validationGroups = [], FormTypeRegistryInterface $formTypeRegistry)
37
    {
38
        parent::__construct($dataClass, $validationGroups);
39
40
        $this->formTypeRegistry = $formTypeRegistry;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function buildForm(FormBuilderInterface $builder, array $options)
47
    {
48
        parent::buildForm($builder, $options);
49
50
        $builder
51
            ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
52
                $type = $this->getRegistryIdentifier($event->getForm(), $event->getData());
53
                if (null === $type) {
54
                    return;
55
                }
56
57
                $this->addConfigurationFields($event->getForm(), $this->formTypeRegistry->get($type, 'default'));
58
            })
59
            ->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
60
                $type = $this->getRegistryIdentifier($event->getForm(), $event->getData());
61
                if (null === $type) {
62
                    return;
63
                }
64
65
                $event->getForm()->get('type')->setData($type);
66
            })
67
            ->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
68
                $data = $event->getData();
69
70
                if (!isset($data['type'])) {
71
                    return;
72
                }
73
74
                $this->addConfigurationFields($event->getForm(), $this->formTypeRegistry->get($data['type'], 'default'));
75
            })
76
        ;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function configureOptions(OptionsResolver $resolver)
83
    {
84
        parent::configureOptions($resolver);
85
86
        $resolver
87
            ->setDefault('configuration_type', null)
88
            ->setAllowedTypes('configuration_type', ['string', 'null'])
89
        ;
90
    }
91
92
    /**
93
     * @param FormInterface $form
94
     * @param string $configurationType
95
     */
96
    protected function addConfigurationFields(FormInterface $form, $configurationType)
97
    {
98
        $form->add('configuration', $configurationType, [
99
            'label' => false,
100
        ]);
101
    }
102
103
    /**
104
     * @param FormInterface $form
105
     * @param mixed $data
106
     *
107
     * @return string|null
108
     */
109
    protected function getRegistryIdentifier(FormInterface $form, $data = null)
110
    {
111
        if ($data instanceof ConfigurablePromotionElementInterface && null !== $data->getType()) {
112
            return $data->getType();
113
        }
114
115
        if (null !== $form->getConfig()->hasOption('configuration_type')) {
116
            return $form->getConfig()->getOption('configuration_type');
117
        }
118
119
        return null;
120
    }
121
}
122