AttributeSubscriber   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 24
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 117
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOption() 0 10 2
A getSubscribedEvents() 0 6 1
A preSetData() 0 11 2
C createValueField() 0 69 18
1
<?php
2
namespace Padam87\AttributeBundle\Form\EventListener;
3
4
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
5
use Symfony\Component\Form\FormEvent;
6
use Symfony\Component\Form\FormEvents;
7
use Symfony\Component\Form\FormFactoryInterface;
8
9
class AttributeSubscriber implements EventSubscriberInterface
10
{
11
    private $factory;
12
    private $options;
13
14
    private $defaultOptions = [
15
        'allow_expanded' => true,
16
        'allow_textarea' => true,
17
        'all_multiple' => false,
18
    ];
19
20
    public function __construct(FormFactoryInterface $factory, $options = [])
21
    {
22
        $this->factory = $factory;
23
        $this->options = $options;
24
    }
25
26
    public function getOption($name)
27
    {
28
        if (!isset($this->options[$name])) {
29
            $value = $this->defaultOptions[$name];
30
        } else {
31
            $value = $this->options[$name];
32
        }
33
34
        return $value;
35
    }
36
37
    public static function getSubscribedEvents()
38
    {
39
        // Tells the dispatcher that we want to listen on the form.pre_set_data
40
        // event and that the preSetData method should be called.
41
        return [FormEvents::PRE_SET_DATA => 'preSetData'];
42
    }
43
44
    public function preSetData(FormEvent $event)
45
    {
46
        $data = $event->getData();
47
        $form = $event->getForm();
48
49
        if (null === $data) {
50
            return;
51
        }
52
53
        $this->createValueField($data, $form);
54
    }
55
56
    public function createValueField($data, $form, $fieldName = 'value')
57
    {
58
        if ($data->getDefinition() == null) {
59
            return false;
60
        }
61
62
        $attribute = $data;
63
        $definition = $attribute->getDefinition();
64
65
        $type = $definition->getType();
66
        $options = $definition->getOptions()->toArray();
67
        $value = $data->getValue();
68
69
        $params = [
70
            'attr' => $form->getConfig()->getOptions()['attr'],
71
        ];
72
73
        if ($type == 'textarea' && !$this->getOption('allow_expanded')) {
74
            $type = 'text';
75
        }
76
77
        if ($type == 'choice' || $type == 'checkbox' || $type == 'radio') {
78
            if (($type == 'checkbox' || $type == 'radio') && $this->getOption('allow_expanded')) {
79
                $params['expanded'] = true;
80
            }
81
82
            if ($this->getOption('all_multiple')) {
83
                $params['multiple'] = true;
84
            } else {
85
                if ($type == 'radio') {
86
                    $params['multiple'] = false;
87
                } elseif ($type == 'checkbox') {
88
                    if (!is_array($value)) {
89
                        $value = [
90
                            $value => $value,
91
                        ];
92
                    }
93
94
                    $params['multiple'] = true;
95
                }
96
            }
97
98
            $params['choices'] = [];
99
100
            foreach ($options as $option) {
101
                $params['choices'][$option->getValue()] = $option->getName();
102
            }
103
104
            $type = 'choice';
105
        } elseif (is_array($value)) {
106
            $value = null;
107
        }
108
109
        $params['required'] = $definition->getRequired();
110
111
        $params['label'] = $definition->getName();
112
113
        if ($definition->getUnit() != "") {
114
            $params['label_attr']['unit'] = $definition->getUnit();
115
        }
116
117
        if ($definition->getDescription() != "") {
118
            $params['label_attr']['help'] = $definition->getDescription();
119
        }
120
121
        $params['auto_initialize'] = false;
122
123
        $form->add($this->factory->createNamed($fieldName, $type, $value, $params));
124
    }
125
}
126