AlpixelDropzoneType::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Alpixel\Bundle\MediaBundle\Form\Type;
4
5
use Alpixel\Bundle\MediaBundle\DataTransformer\EntityToIdTransformer;
6
use Alpixel\Bundle\MediaBundle\EventListener\MediaEvent;
7
use Doctrine\ORM\EntityManager;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\Extension\Core\Type\TextType;
11
use Symfony\Component\Form\FormBuilderInterface;
12
use Symfony\Component\Form\FormEvent;
13
use Symfony\Component\Form\FormEvents;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\Form\FormView;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
18
19
/**
20
 * @author Benjamin HUBERT <[email protected]>
21
 */
22
class AlpixelDropzoneType extends AbstractType
23
{
24
    /**
25
     * @var \Doctrine\ORM\EntityManager
26
     */
27
    protected $entityManager;
28
    /**
29
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
30
     */
31
    protected $dispatcher;
32
33
    protected $uploadConfigurations;
34
35
    /**
36
     * AlpixelDropzoneType constructor.
37
     * @param \Doctrine\ORM\EntityManager $entityManager
38
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
39
     */
40
    public function __construct(
41
        EntityManager $entityManager,
42
        EventDispatcherInterface $dispatcher,
43
        $uploadConfigurations
44
    ) {
45
        $this->entityManager = $entityManager;
46
        $this->dispatcher = $dispatcher;
47
        $this->uploadConfigurations = $uploadConfigurations;
48
    }
49
50
    /**
51
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
52
     * @param array $options
53
     */
54
    public function buildForm(FormBuilderInterface $builder, array $options)
55
    {
56
        $builder->addModelTransformer(
57
            new EntityToIdTransformer(
58
                $this->entityManager,
59
                'Alpixel\Bundle\MediaBundle\Entity\Media',
60
                'secretKey',
61
                null,
62
                $options['multiple']
63
            )
64
        );
65
66
        $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
67
        $builder->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit']);
68
    }
69
70
    /**
71
     * @param \Symfony\Component\Form\FormEvent $event
72
     */
73
    public function onPreSetData(FormEvent $event)
74
    {
75
        $form = $event->getForm();
76
        $formConfig = $form->getConfig();
77
        $uploadConfiguration = $formConfig->getOption("upload_configuration");
78
79
        if (!empty($uploadConfiguration)) {
80
            if (empty($this->uploadConfigurations) ||
81
                !array_key_exists($uploadConfiguration, $this->uploadConfigurations)
82
            ) {
83
                throw new \InvalidArgumentException(
84
                    sprintf("The %s configuration doesn't exist in MediaBundle", $uploadConfiguration)
85
                );
86
            }
87
        }
88
    }
89
90
    /**
91
     * @param \Symfony\Component\Form\FormEvent $event
92
     */
93
    public function onPostSubmit(FormEvent $event)
94
    {
95
        $results = $event->getForm()->getData();
96
97
        if ($results === null) {
98
            return;
99
        }
100
101
        if (!is_array($results)) {
102
            $results = [$results];
103
        }
104
105
        foreach ($results as $media) {
106
            $mediaEvent = new MediaEvent($media);
107
            $this->dispatcher->dispatch(MediaEvent::POST_SUBMIT, $mediaEvent);
108
        }
109
    }
110
111
    /**
112
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
113
     */
114
    public function configureOptions(OptionsResolver $resolver)
115
    {
116
        $resolver->setDefaults(
117
            [
118
                'type'                 => TextType::class,
119
                'hidden'               => true,
120
                'upload_configuration' => "",
121
                'multiple'             => false,
122
                'label'                => false,
123
                'helper'               => 'Ajouter une photo / un fichier',
124
                'max_nb_file'          => 10,
125
            ]
126
        );
127
    }
128
129
    /**
130
     * @param \Symfony\Component\Form\FormView $view
131
     * @param \Symfony\Component\Form\FormInterface $form
132
     * @param array $options
133
     */
134
    public function buildView(FormView $view, FormInterface $form, array $options)
135
    {
136
        if (true === $options['hidden']) {
137
            $view->vars['type'] = 'hidden';
138
        }
139
140
        $view->vars['helper'] = $options['helper'];
141
        $view->vars['multiple'] = $options['multiple'];
142
        if (!empty($options['upload_configuration'])) {
143
            $view->vars['mimetypes'] = $this->uploadConfigurations[$options['upload_configuration']]['allowed_mimetypes'];
144
            $view->vars['upload_configuration'] = $options['upload_configuration'];
145
        }
146
147
        $view->vars['max_nb_file'] = $options['max_nb_file'];
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function finishView(FormView $view, FormInterface $form, array $options)
154
    {
155
        $view->vars['multipart'] = true;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getParent()
162
    {
163
        return TextType::class;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getName()
170
    {
171
        return 'alpixel_dropzone';
172
    }
173
}
174