Previewer::getAudioPreview()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Itstructure\MFU\Services;
4
5
use Itstructure\MFU\Processors\SaveProcessor;
6
use Itstructure\MFU\Models\Mediafile;
7
use Itstructure\MFU\Helpers\HtmlHelper;
8
9
class Previewer
10
{
11
    const LOCATION_FILE_ITEM = 'fileitem';
12
    const LOCATION_FILE_INFO = 'fileinfo';
13
    const LOCATION_EXISTING = 'existing';
14
15
    /**
16
     * @var array
17
     */
18
    private $config;
19
20
    /**
21
     * @param array $config
22
     * @return Previewer
23
     */
24
    public static function getInstance(array $config = []): self
25
    {
26
        return new static($config);
27
    }
28
29
    /**
30
     * Previewer constructor.
31
     * @param array $config
32
     */
33
    private function __construct(array $config = [])
34
    {
35
        $this->config = $config;
36
    }
37
38
    /**
39
     * @param Mediafile $mediafile
40
     * @param string $location
41
     * @param array $htmlAttributes
42
     * @return string
43
     */
44
    public function getPreviewHtml(Mediafile $mediafile, string $location, array $htmlAttributes = []): string
45
    {
46
        if ($mediafile->isImage()) {
47
            $thumbAlias = isset($this->config['thumbAlias']) && isset($this->config['thumbAlias'][$location])
48
                ? $this->config['thumbAlias'][$location]
49
                : SaveProcessor::THUMB_ALIAS_SMALL;
50
            return $this->getImagePreview($mediafile, $location, $htmlAttributes, $thumbAlias);
51
52
        } else if ($mediafile->isAudio()) {
53
            return $this->getAudioPreview($mediafile, $location, $htmlAttributes);
54
55
        } else if ($mediafile->isVideo()) {
56
            return $this->getVideoPreview($mediafile, $location, $htmlAttributes);
57
58
        } else if ($mediafile->isApp()) {
59
            return $this->getStubAppPreview($mediafile, $location, $htmlAttributes);
60
61
        } else if ($mediafile->isText()) {
62
            return $this->getStubTextPreview($location, $htmlAttributes);
63
64
        } else {
65
            return $this->getStubOtherPreview($location, $htmlAttributes);
66
        }
67
    }
68
69
    /**
70
     * @param Mediafile $mediafile
71
     * @param string $location
72
     * @param array $htmlAttributes
73
     * @param string $alias
74
     * @return string
75
     */
76
    public function getImagePreview(Mediafile $mediafile, string $location, array $htmlAttributes = [], string $alias = SaveProcessor::THUMB_ALIAS_SMALL): string
77
    {
78
        return view('uploader::preview.image', [
79
            'src' => $mediafile->getThumbUrl($alias),
80
            'alt' => $mediafile->getAlt(),
81
            'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_IMAGE, $location, $htmlAttributes)
82
        ]);
83
    }
84
85
    /**
86
     * @param Mediafile $mediafile
87
     * @param string $location
88
     * @param array $htmlAttributes
89
     * @return string
90
     */
91
    public function getAudioPreview(Mediafile $mediafile, string $location, array $htmlAttributes = []): string
92
    {
93
        return view('uploader::preview.audio', [
94
            'src' => $mediafile->getOriginalUrl(),
95
            'type' => $mediafile->getMimeType(),
96
            'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_AUDIO, $location, $htmlAttributes)
97
        ]);
98
    }
99
100
    /**
101
     * @param Mediafile $mediafile
102
     * @param string $location
103
     * @param array $htmlAttributes
104
     * @return string
105
     */
106
    public function getVideoPreview(Mediafile $mediafile, string $location, array $htmlAttributes = []): string
107
    {
108
        return view('uploader::preview.video', [
109
            'src' => $mediafile->getOriginalUrl(),
110
            'type' => $mediafile->getMimeType(),
111
            'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_VIDEO, $location, $htmlAttributes)
112
        ]);
113
    }
114
115
    /**
116
     * @param Mediafile $mediafile
117
     * @param string $location
118
     * @param array $htmlAttributes
119
     * @return string
120
     */
121
    public function getStubAppPreview(Mediafile $mediafile, string $location, array $htmlAttributes = []): string
122
    {
123
        if ($mediafile->isWord()) {
124
            return view('uploader::preview.stub.word', [
125
                'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_APP_WORD, $location, $htmlAttributes)
126
            ]);
127
        } else if ($mediafile->isExcel()) {
128
            return view('uploader::preview.stub.excel', [
129
                'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_APP_EXCEL, $location, $htmlAttributes)
130
            ]);
131
        } else if ($mediafile->isVisio()) {
132
            return view('uploader::preview.stub.visio', [
133
                'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_APP_VISIO, $location, $htmlAttributes)
134
            ]);
135
        } else if ($mediafile->isPowerPoint()) {
136
            return view('uploader::preview.stub.ppt', [
137
                'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_APP_PPT, $location, $htmlAttributes)
138
            ]);
139
        } else if ($mediafile->isPdf()) {
140
            return view('uploader::preview.stub.pdf', [
141
                'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_APP_PDF, $location, $htmlAttributes)
142
            ]);
143
        } else {
144
            return view('uploader::preview.stub.app', [
145
                'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_APP, $location, $htmlAttributes)
146
            ]);
147
        }
148
    }
149
150
    /**
151
     * @param string $location
152
     * @param array $htmlAttributes
153
     * @return string
154
     */
155
    public function getStubTextPreview(string $location, array $htmlAttributes = []): string
156
    {
157
        return view('uploader::preview.stub.text', [
158
            'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_TEXT, $location, $htmlAttributes)
159
        ]);
160
    }
161
162
    /**
163
     * @param string $location
164
     * @param array $htmlAttributes
165
     * @return string
166
     */
167
    public function getStubOtherPreview(string $location, array $htmlAttributes = []): string
168
    {
169
        return view('uploader::preview.stub.other', [
170
            'htmlAttributes' => $this->getHtmlAttributes(SaveProcessor::FILE_TYPE_OTHER, $location, $htmlAttributes)
171
        ]);
172
    }
173
174
    /**
175
     * @param string $fileType
176
     * @param string $location
177
     * @param array $additional
178
     * @return string
179
     */
180
    private function getHtmlAttributes(string $fileType,  string $location, array $additional = []): string
181
    {
182
        $htmlAttributes = isset($this->config['htmlAttributes'])
183
            && isset($this->config['htmlAttributes'][$fileType])
184
            && isset($this->config['htmlAttributes'][$fileType][$location])
185
                ? $this->config['htmlAttributes'][$fileType][$location]
186
                : [];
187
        $htmlAttributes = array_merge($htmlAttributes, $additional);
188
        return HtmlHelper::buildHtmlAttributes($htmlAttributes);
189
    }
190
}
191