UploadableType   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 109
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A setDefaultOptions() 0 17 1
B buildForm() 0 32 2
A onPreSetData() 0 10 3
B onPreSubmit() 0 12 5
1
<?php
2
3
namespace Fenrizbes\UploadableBundle\Form\Type;
4
5
use Fenrizbes\UploadableBundle\Form\DataTransformer\UploadableDataTransformer;
6
use Symfony\Component\Form\AbstractType;
7
use Symfony\Component\Form\FormBuilderInterface;
8
use Symfony\Component\Form\FormEvent;
9
use Symfony\Component\Form\FormEvents;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
12
13
class UploadableType extends AbstractType
14
{
15
    protected $root_dir;
16
    protected $options;
17
18
    public function __construct($root_dir)
19
    {
20
        $this->root_dir = $root_dir;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getName()
27
    {
28
        return 'uploadable';
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function setDefaultOptions(OptionsResolverInterface $resolver)
35
    {
36
        $resolver
37
            ->setRequired(array(
38
                'removable',
39
                'remove_label'
40
            ))
41
            ->setOptional(array(
42
                'file_constraints'
43
            ))
44
            ->setDefaults(array(
45
                'removable'    => true,
46
                'remove_label' => 'remove',
47
                'required'     => false
48
            ))
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function buildForm(FormBuilderInterface $builder, array $options)
56
    {
57
        $this->options = $options;
58
59
        unset($options['removable']);
60
        unset($options['remove_label']);
61
62
        if (isset($options['file_constraints'])) {
63
            $options['constraints'] = $options['file_constraints'];
64
65
            unset($options['file_constraints']);
66
        }
67
68
        $builder
69
            ->add('file', 'file', array_merge($options, array(
70
                'compound'   => false,
71
                'label'      => false,
72
                'data_class' => '\Symfony\Component\HttpFoundation\File\File'
73
            )))
74
            ->addEventListener(
75
                FormEvents::PRE_SET_DATA,
76
                array($this, 'onPreSetData')
77
            )
78
            ->addEventListener(
79
                FormEvents::PRE_SUBMIT,
80
                array($this, 'onPreSubmit')
81
            )
82
            ->addViewTransformer(
83
                new UploadableDataTransformer($this->root_dir)
84
            )
85
        ;
86
    }
87
88
    /**
89
     * Adds a remove-field if need
90
     *
91
     * @param FormEvent $event
92
     */
93
    public function onPreSetData(FormEvent $event)
94
    {
95
        if (!$event->getData() || !$event->getForm()->getConfig()->getOption('removable')) {
96
            return;
97
        }
98
99
        $event->getForm()->add('remove', 'checkbox', array(
100
            'label' => $this->options['remove_label']
101
        ));
102
    }
103
104
    /**
105
     * Forbids to erase a value
106
     *
107
     * @param FormEvent $event
108
     */
109
    public function onPreSubmit(FormEvent $event)
110
    {
111
        $data = $event->getData();
112
113
        if ($data['file'] instanceof UploadedFile) {
114
            $event->getForm()->remove('remove');
115
        }
116
117
        if (empty($data['file']) && (!isset($data['remove']) || !$data['remove'])) {
118
            $event->setData($event->getForm()->getViewData());
119
        }
120
    }
121
}