Completed
Push — master ( 53cf26...893eb2 )
by
unknown
08:28
created

FileProvider::buildProviderCreateForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Provider;
4
5
use MediaMonks\SonataMediaBundle\Entity\Media;
6
use Sonata\AdminBundle\Form\FormMapper;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
use Symfony\Component\Validator\Constraints as Constraint;
9
use Symfony\Component\Validator\Context\ExecutionContextInterface;
10
11
class FileProvider extends AbstractProvider
12
{
13
    /**
14
     * @var array
15
     */
16
    private $fileConstraintOptions = [];
17
18
    /**
19
     * @param array $fileConstraintOptions
20
     */
21
    public function __construct(array $fileConstraintOptions = [])
22
    {
23
        $this->fileConstraintOptions = $fileConstraintOptions;
24
    }
25
26
    /**
27
     * @param FormMapper $formMapper
28
     */
29
    public function buildProviderCreateForm(FormMapper $formMapper)
30
    {
31
        $this->addRequiredFileField($formMapper, 'binaryContent', 'File');
32
    }
33
34
    /**
35
     * @param FormMapper $formMapper
36
     */
37
    public function buildProviderEditForm(FormMapper $formMapper)
38
    {
39
        $this->addFileField($formMapper, 'binaryContent', 'File');
40
    }
41
42
    /**
43
     * @param Media $media
44
     * @param bool $providerReferenceUpdated
45
     */
46
    public function update(Media $media, $providerReferenceUpdated)
47
    {
48
        if (!is_null($media->getBinaryContent())) {
49
            if (empty($media->getImage())) {
50
                $this->setFileImage($media);
51
            }
52
            $filename = $this->handleFileUpload($media);
53
            if (!empty($filename)) {
54
                $media->setProviderReference($filename);
55
            }
56
        }
57
58
        parent::update($media, $providerReferenceUpdated);
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getIcon()
65
    {
66
        return 'fa fa-file';
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getTitle()
73
    {
74
        return 'File';
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getName()
81
    {
82
        return 'file';
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getType()
89
    {
90
        return AbstractProvider::TYPE_FILE;
91
    }
92
93
    /**
94
     * @param Media $media
95
     */
96
    protected function setFileImage(Media $media)
97
    {
98
        /**
99
         * @var UploadedFile $file
100
         */
101
        $file = $media->getBinaryContent();
102
        if (empty($file)) {
103
            return;
104
        }
105
106
        $imageFilename = $this->getImageByExtension($file->getClientOriginalExtension());
107
        $media->setImageContent(
108
            new UploadedFile(
109
                $this->getImageLocation().$imageFilename,
110
                $imageFilename
111
            )
112
        );
113
    }
114
115
    /**
116
     * @param FormMapper $formMapper
117
     * @param string $name
118
     * @param string $label
119
     * @param array $options
120
     */
121 View Code Duplication
    public function addFileField(FormMapper $formMapper, $name, $label, $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        $this->doAddFileField(
124
            $formMapper,
125
            $name,
126
            $label,
127
            false,
128
            [
129
                new Constraint\File($this->getFileConstraintOptions($options)),
130
                new Constraint\Callback([$this, 'validateExtension']),
131
            ]
132
        );
133
    }
134
135
    /**
136
     * @param FormMapper $formMapper
137
     * @param string $name
138
     * @param string $label
139
     * @param array $options
140
     */
141 View Code Duplication
    public function addRequiredFileField(FormMapper $formMapper, $name, $label, $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $this->doAddFileField(
144
            $formMapper,
145
            $name,
146
            $label,
147
            true,
148
            [
149
                new Constraint\File($this->getFileConstraintOptions($options)),
150
                new Constraint\Callback([$this, 'validateExtension']),
151
            ]
152
        );
153
    }
154
155
    /**
156
     * @param array $options
157
     * @return array
158
     */
159
    protected function getFileConstraintOptions(array $options = [])
160
    {
161
        $merged = array_merge($this->fileConstraintOptions, $options);
162
        unset($merged['extensions']);
163
164
        return $merged;
165
    }
166
167
    /**
168
     * @param $object
169
     * @param ExecutionContextInterface $context
170
     */
171
    public function validateExtension($object, ExecutionContextInterface $context)
172
    {
173
        if ($object instanceof UploadedFile && isset($this->fileConstraintOptions['extensions'])) {
174
            if (!in_array($object->getClientOriginalExtension(), $this->fileConstraintOptions['extensions'])) {
175
                $context->addViolation('It\'s not allowed to upload a file with extension "%s"');
176
            }
177
        }
178
    }
179
180
    /**
181
     * @param $extension
182
     * @return string
183
     */
184
    protected function getImageByExtension($extension)
185
    {
186
        if (in_array($extension, ['zip', 'rar', 'tar', 'gz'])) {
187
            return 'archive.png';
188
        }
189 View Code Duplication
        if (in_array($extension, ['wav', 'mp3', 'flac', 'aac', 'aiff', 'm4a', 'ogg', 'oga', 'wma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
            return 'audio.png';
191
        }
192
        if (in_array($extension, ['php', 'html', 'css', 'js', 'vb', 'phar', 'py', 'jar', 'json', 'yml'])) {
193
            return 'code.png';
194
        }
195 View Code Duplication
        if (in_array($extension, ['xls', 'xlt', 'xlm', 'xlsx', 'xlsm', 'xltx', 'xltm'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
            return 'excel.png';
197
        }
198
        if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'ai', 'psd'])) {
199
            return 'image.png';
200
        }
201
        if (in_array($extension, ['mp4', 'avi', 'mkv', 'mpg', 'mpeg'])) {
202
            return 'movie.png';
203
        }
204
        if (in_array($extension, ['pdf'])) {
205
            return 'pdf.png';
206
        }
207
        if (in_array(
208
            $extension,
209
            ['ppt', 'pot', 'pos', 'pps', 'pptx', 'pptm', 'potx', 'potm', 'ppam', 'ppsx', 'ppsm', 'sldx', 'sldm']
210
        )) {
211
            return 'powerpoint.png';
212
        }
213
        if (in_array($extension, ['txt'])) {
214
            return 'txt.png';
215
        }
216 View Code Duplication
        if (in_array($extension, ['doc', 'dot', 'wbk', 'docx', 'docm', 'dotx', 'dotm', 'docb'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
            return 'word.png';
218
        }
219
220
        return 'default.png';
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    protected function getImageLocation()
227
    {
228
        return __DIR__.'/../Resources/image/file/';
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    public function supportsDownload()
235
    {
236
        return true;
237
    }
238
239
    /**
240
     * @return bool
241
     */
242
    public function supportsEmbed()
243
    {
244
        return false;
245
    }
246
247
    /**
248
     * @return bool
249
     */
250
    public function supportsImage()
251
    {
252
        return true;
253
    }
254
}
255