Completed
Push — issue-20 ( f0936c...8a28ac )
by Christian
05:20
created

VideoUploaderExtension::buildView()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
ccs 26
cts 26
cp 1
rs 8.8571
cc 3
eloc 25
nc 2
nop 3
crap 3
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaBundle package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
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 Xabbuh\PandaBundle\Form\Extension;
13
14
use Symfony\Component\Form\AbstractTypeExtension;
15
use Symfony\Component\Form\FormInterface;
16
use Symfony\Component\Form\FormView;
17
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
18
use Symfony\Component\OptionsResolver\Options;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
23
/**
24
 * Extends the {@link http://api.symfony.com/master/Symfony/Component/Form/Extension/Core/Type/FileType.html FileType}
25
 * by adding an option to mark such a field as a widget for using the
26
 * {@link http://www.pandastream.com/docs/video_uploader Panda uploader}.
27
 *
28
 * @author Christian Flothmann <[email protected]>
29
 */
30
class VideoUploaderExtension extends AbstractTypeExtension
31
{
32
    /**
33
     * The url generator
34
     * @var UrlGeneratorInterface
35
     */
36
    private $urlGenerator;
37
38
    /**
39
     * General options to configure the display of widget components
40
     * @var array
41
     */
42
    private $defaultOptions;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param UrlGeneratorInterface $urlGenerator   The url generator
48
     * @param array                 $defaultOptions Default display options for widget objects
49
     */
50 17
    public function __construct(UrlGeneratorInterface $urlGenerator, array $defaultOptions)
51
    {
52 17
        $this->urlGenerator = $urlGenerator;
53 17
        $this->defaultOptions = array_replace(array(
54 17
            'multiple_files' => false,
55
            'cancel_button' => true,
56
            'progress_bar' => true,
57 17
        ), $defaultOptions);
58 17
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 17
    public function getExtendedType()
64
    {
65
        // BC with Symfony < 2.8
66 17
        if (!method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
67
            return 'file';
68
        }
69
70 17
        return 'Symfony\Component\Form\Extension\Core\Type\FileType';
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function setDefaultOptions(OptionsResolverInterface $resolver)
77
    {
78
        // BC for symfony 2.6 and older which don't have the forward-compatibility layer
79
        if (!$resolver instanceof OptionsResolver) {
80
            throw new \InvalidArgumentException(sprintf('Custom resolver "%s" must extend "Symfony\Component\OptionsResolver\OptionsResolver".', get_class($resolver)));
81
        }
82
83
        $this->configureOptions($resolver);
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 17
    public function configureOptions(OptionsResolver $resolver)
90
    {
91 17
        $resolver->setDefaults(array(
92 17
            'panda_widget' => false,
93
            'panda_widget_version' => 2,
94
            'cloud' => null,
95
        ));
96 17
        $resolver->setDefaults($this->defaultOptions);
97
98 16
        $cloudNormalizer = function (Options $options, $cloud) {
99 16
            if (!$options['panda_widget']) {
100 4
                return null;
101
            }
102
103 12
            if (null === $cloud) {
104 1
                throw new InvalidOptionsException('The "cloud" option is required when enabling the panda widget.');
105
            }
106
107 11
            return $cloud;
108
        };
109
110 17
        $resolver->setNormalizer('cloud', $cloudNormalizer);
111 17
        $resolver->setAllowedValues('panda_widget', array(true, false, 'yes', 'no'));
112 17
        $resolver->setAllowedValues('panda_widget_version', array(1, 2));
113 17
        $resolver->setAllowedTypes('cancel_button', 'bool');
114 17
        $resolver->setAllowedTypes('cloud', array('null', 'string'));
115 17
        $resolver->setAllowedTypes('multiple_files', 'bool');
116 17
        $resolver->setAllowedTypes('panda_widget', array('bool', 'string'));
117 17
        $resolver->setAllowedTypes('progress_bar', 'bool');
118 17
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123 12
    public function buildView(FormView $view, FormInterface $form, array $options)
124
    {
125 12
        if (!$options['panda_widget'] || !isset($view->vars['id'])) {
126 1
            $view->vars["panda_uploader"] = false;
127
128 1
            return;
129
        }
130
131 11
        $view->vars["panda_uploader"] = true;
132
133 11
        $widgetVersion = $options["panda_widget_version"];
134
135
        // generate widgets' ids
136 11
        $formId = $view->vars["id"];
137 11
        $browseButtonId = "browse_button_$formId";
138 11
        $cancelButtonId = "cancel_button_$formId";
139 11
        $progressBarId = "progress_bar_$formId";
140
141
        // set input field attributes accordingly (widget ids will be
142
        // read from there by the javascript uploader implementation)
143 11
        $view->vars["attr"]["panda-uploader"] = "v$widgetVersion";
144 11
        $view->vars["attr"]["authorise-url"] = $this->urlGenerator->generate(
145 11
            "xabbuh_panda_authorise_upload",
146 11
            array("cloud" => $options["cloud"])
147
        );
148 11
        $view->vars["attr"]["multiple_files"] = var_export($options["multiple_files"], true);
149 11
        $view->vars["attr"]["browse-button-id"] = $browseButtonId;
150 11
        $view->vars["attr"]["cancel-button-id"] = $cancelButtonId;
151 11
        $view->vars["attr"]["progress-bar-id"] = $progressBarId;
152
153 11
        $view->vars["browse_button_id"] = $browseButtonId;
154 11
        $view->vars["browse_button_label"] = "Browse";
155 11
        $view->vars["cancel_button"] = $options["cancel_button"];
156 11
        $view->vars["cancel_button_id"] = $cancelButtonId;
157 11
        $view->vars["cancel_button_label"] = "Cancel";
158 11
        $view->vars["progress_bar"] = $options["progress_bar"];
159 11
        $view->vars["progress_bar_id"] = $progressBarId;
160 11
    }
161
}
162