Completed
Push — master ( 80edcc...1fe58d )
by
unknown
05:10 queued 11s
created

FileProvider::getImageByExtension()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 38
Code Lines 24

Duplication

Lines 9
Ratio 23.68 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 9
loc 38
ccs 0
cts 37
cp 0
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 24
nc 11
nop 1
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
9
class FileProvider extends AbstractProvider
10
{
11
    /**
12
     * @param FormMapper $formMapper
13
     */
14
    public function buildProviderCreateForm(FormMapper $formMapper)
15
    {
16
        $this->addRequiredFileUploadField($formMapper, 'binaryContent', 'File');
17
    }
18
19
    /**
20
     * @param FormMapper $formMapper
21
     */
22
    public function buildProviderEditForm(FormMapper $formMapper)
23
    {
24
        $this->addFileUploadField($formMapper, 'binaryContent', 'File');
25
    }
26
27
    /**
28
     * @param Media $media
29
     */
30
    public function update(Media $media)
31
    {
32
        if (!is_null($media->getBinaryContent())) {
33
            if (empty($media->getImage())) {
34
                $this->setFileImage($media);
35
            }
36
            $filename = $this->handleFileUpload($media);
37
            if (!empty($filename)) {
38
                $media->setProviderReference($filename);
39
            }
40
        }
41
42
        parent::update($media);
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getIcon()
49
    {
50
        return 'file';
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getTitle()
57
    {
58
        return 'File';
59
    }
60
61
    public function getType()
62
    {
63
        return 'file';
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getEmbedTemplate()
70
    {
71
        return 'MediaMonksSonataMediaBundle:Provider:file_embed.html.twig';
72
    }
73
74
    /**
75
     * @param Media $media
76
     */
77
    protected function setFileImage(Media $media)
78
    {
79
        /**
80
         * @var UploadedFile $file
81
         */
82
        $file = $media->getBinaryContent();
83
        if (empty($file)) {
84
            return;
85
        }
86
87
        $imageFilename = $this->getImageByExtension($file->getClientOriginalExtension());
88
        $media->setImageContent(
89
            new UploadedFile(
90
                $this->getImageLocation().$imageFilename,
91
                $imageFilename
92
            )
93
        );
94
    }
95
96
    /**
97
     * @param $extension
98
     * @return string
99
     */
100
    protected function getImageByExtension($extension)
101
    {
102
        if (in_array($extension, ['zip', 'rar', 'tar', 'gz'])) {
103
            return 'archive.png';
104
        }
105 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...
106
            return 'audio.png';
107
        }
108
        if (in_array($extension, ['php', 'html', 'css', 'js', 'vb', 'phar', 'py', 'jar', 'json', 'yml'])) {
109
            return 'code.png';
110
        }
111 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...
112
            return 'excel.png';
113
        }
114
        if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'ai', 'psd'])) {
115
            return 'image.png';
116
        }
117
        if (in_array($extension, ['mp4', 'avi', 'mkv', 'mpg', 'mpeg'])) {
118
            return 'movie.png';
119
        }
120
        if (in_array($extension, ['pdf'])) {
121
            return 'pdf.png';
122
        }
123
        if (in_array(
124
            $extension,
125
            ['ppt', 'pot', 'pos', 'pps', 'pptx', 'pptm', 'potx', 'potm', 'ppam', 'ppsx', 'ppsm', 'sldx', 'sldm']
126
        )) {
127
            return 'powerpoint.png';
128
        }
129
        if (in_array($extension, ['txt'])) {
130
            return 'txt.png';
131
        }
132 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...
133
            return 'word.png';
134
        }
135
136
        return 'default.png';
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    protected function getImageLocation()
143
    {
144
        return __DIR__.'/../Resources/image/file/';
145
    }
146
147
    /**
148
     * @return bool
149
     */
150
    public function supportsDownload()
151
    {
152
        return true;
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function supportsEmbed()
159
    {
160
        return false;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166
    public function supportsImage()
167
    {
168
        return true;
169
    }
170
}
171