Completed
Push — master ( 1cc057...1c0904 )
by
unknown
03:48
created

FileProvider::buildProviderEditFormBefore()   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 buildProviderEditFormBefore(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 getName()
73
    {
74
        return 'file';
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getType()
81
    {
82
        return AbstractProvider::TYPE_FILE;
83
    }
84
85
    /**
86
     * @param Media $media
87
     */
88
    protected function setFileImage(Media $media)
89
    {
90
        /**
91
         * @var UploadedFile $file
92
         */
93
        $file = $media->getBinaryContent();
94
        if (empty($file)) {
95
            return;
96
        }
97
98
        $imageFilename = $this->getImageByExtension($file->getClientOriginalExtension());
99
        $media->setImageContent(
100
            new UploadedFile(
101
                $this->getImageLocation().$imageFilename,
102
                $imageFilename
103
            )
104
        );
105
    }
106
107
    /**
108
     * @param FormMapper $formMapper
109
     * @param string $name
110
     * @param string $label
111
     * @param array $options
112
     */
113 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...
114
    {
115
        $this->doAddFileField(
116
            $formMapper,
117
            $name,
118
            $label,
119
            false,
120
            [
121
                new Constraint\File($this->getFileConstraintOptions($options)),
122
                new Constraint\Callback([$this, 'validateExtension']),
123
            ]
124
        );
125
    }
126
127
    /**
128
     * @param FormMapper $formMapper
129
     * @param string $name
130
     * @param string $label
131
     * @param array $options
132
     */
133 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...
134
    {
135
        $this->doAddFileField(
136
            $formMapper,
137
            $name,
138
            $label,
139
            true,
140
            [
141
                new Constraint\File($this->getFileConstraintOptions($options)),
142
                new Constraint\Callback([$this, 'validateExtension']),
143
            ]
144
        );
145
    }
146
147
    /**
148
     * @param array $options
149
     * @return array
150
     */
151
    protected function getFileConstraintOptions(array $options = [])
152
    {
153
        $merged = array_merge($this->fileConstraintOptions, $options);
154
        unset($merged['extensions']);
155
156
        return $merged;
157
    }
158
159
    /**
160
     * @param $object
161
     * @param ExecutionContextInterface $context
162
     */
163
    public function validateExtension($object, ExecutionContextInterface $context)
164
    {
165
        if ($object instanceof UploadedFile && isset($this->fileConstraintOptions['extensions'])) {
166
            if (!in_array($object->getClientOriginalExtension(), $this->fileConstraintOptions['extensions'])) {
167
                $context->addViolation('It\'s not allowed to upload a file with extension "%s"');
168
            }
169
        }
170
    }
171
172
    /**
173
     * @param $extension
174
     * @return string
175
     */
176
    protected function getImageByExtension($extension)
177
    {
178
        if (in_array($extension, ['zip', 'rar', 'tar', 'gz'])) {
179
            return 'archive.png';
180
        }
181 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...
182
            return 'audio.png';
183
        }
184
        if (in_array($extension, ['php', 'html', 'css', 'js', 'vb', 'phar', 'py', 'jar', 'json', 'yml'])) {
185
            return 'code.png';
186
        }
187 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...
188
            return 'excel.png';
189
        }
190
        if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'ai', 'psd'])) {
191
            return 'image.png';
192
        }
193
        if (in_array($extension, ['mp4', 'avi', 'mkv', 'mpg', 'mpeg'])) {
194
            return 'movie.png';
195
        }
196
        if (in_array($extension, ['pdf'])) {
197
            return 'pdf.png';
198
        }
199
        if (in_array(
200
            $extension,
201
            ['ppt', 'pot', 'pos', 'pps', 'pptx', 'pptm', 'potx', 'potm', 'ppam', 'ppsx', 'ppsm', 'sldx', 'sldm']
202
        )) {
203
            return 'powerpoint.png';
204
        }
205
        if (in_array($extension, ['txt'])) {
206
            return 'txt.png';
207
        }
208 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...
209
            return 'word.png';
210
        }
211
212
        return 'default.png';
213
    }
214
215
    /**
216
     * @return string
217
     */
218
    protected function getImageLocation()
219
    {
220
        return __DIR__.'/../Resources/image/file/';
221
    }
222
223
    /**
224
     * @return bool
225
     */
226
    public function supportsDownload()
227
    {
228
        return true;
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    public function supportsEmbed()
235
    {
236
        return false;
237
    }
238
239
    /**
240
     * @return bool
241
     */
242
    public function supportsImage()
243
    {
244
        return true;
245
    }
246
}
247