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

FileProvider   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 162
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 3
dl 9
loc 162
ccs 0
cts 104
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A buildProviderCreateForm() 0 4 1
A buildProviderEditForm() 0 4 1
A update() 0 14 4
A getIcon() 0 4 1
A getTitle() 0 4 1
A getType() 0 4 1
A getEmbedTemplate() 0 4 1
A setFileImage() 0 18 2
C getImageByExtension() 9 38 11
A getImageLocation() 0 4 1
A supportsDownload() 0 4 1
A supportsEmbed() 0 4 1
A supportsImage() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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