Mediafile   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 26
eloc 32
c 2
b 1
f 0
dl 0
loc 220
rs 10

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getThumbUrl() 0 3 1
A getAlt() 0 3 1
A isPdf() 0 3 1
A getPath() 0 3 1
A isExcel() 0 3 1
A addOwner() 0 3 1
A isImage() 0 3 1
A getMimeType() 0 3 1
A getDisk() 0 3 1
A isPowerPoint() 0 3 1
A isApp() 0 3 1
A isVideo() 0 3 1
A getTitle() 0 3 1
A isWord() 0 3 1
A getThumbs() 0 3 1
A isAudio() 0 3 1
A getThumbPath() 0 7 3
A getDescription() 0 3 1
A getOriginalUrl() 0 3 1
A isText() 0 3 1
A getSize() 0 3 1
A findByMimeTypes() 0 3 1
A getOwners() 0 3 1
A isVisio() 0 3 1
1
<?php
2
3
namespace Itstructure\MFU\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Storage;
8
use Itstructure\MFU\Processors\SaveProcessor;
9
use Itstructure\MFU\Interfaces\HasOwnerInterface;
10
use Itstructure\MFU\Models\Owners\OwnerMediafile;
11
12
class Mediafile extends Model implements HasOwnerInterface
13
{
14
    protected $table = 'mediafiles';
15
16
    protected $fillable = ['file_name', 'mime_type', 'path', 'alt', 'size', 'title', 'description', 'thumbs', 'disk'];
17
18
    protected $casts = [
19
        'thumbs' => 'array',
20
    ];
21
22
    /**
23
     * @param array $mimeTypes
24
     * @return Collection
25
     */
26
    public static function findByMimeTypes(array $mimeTypes): Collection
27
    {
28
        return static::whereIn('mime_type', $mimeTypes)->get();
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getMimeType(): string
35
    {
36
        return $this->mime_type;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getPath(): string
43
    {
44
        return $this->path;
45
    }
46
47
    /**
48
     * @return null|string
49
     */
50
    public function getAlt(): ?string
51
    {
52
        return $this->alt;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getSize(): int
59
    {
60
        return $this->size;
61
    }
62
63
    /**
64
     * @return null|string
65
     */
66
    public function getTitle(): ?string
67
    {
68
        return $this->title;
69
    }
70
71
    /**
72
     * @return null|string
73
     */
74
    public function getDescription(): ?string
75
    {
76
        return $this->description;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getDisk(): string
83
    {
84
        return $this->disk;
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function getThumbs(): array
91
    {
92
        return $this->thumbs;
93
    }
94
95
    /**
96
     * @param string $alias
97
     * @return string
98
     */
99
    public function getThumbPath(string $alias): string
100
    {
101
        if ($alias == SaveProcessor::THUMB_ALIAS_ORIGINAL) {
102
            return $this->getPath();
103
        }
104
        $thumbs = $this->getThumbs();
105
        return !empty($thumbs[$alias]) ? $thumbs[$alias] : $this->getPath();
106
    }
107
108
    /**
109
     * @param string $alias
110
     * @return string
111
     */
112
    public function getThumbUrl(string $alias = SaveProcessor::THUMB_ALIAS_DEFAULT): string
113
    {
114
        return Storage::disk($this->getDisk())->url($this->getThumbPath($alias));
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getOriginalUrl(): string
121
    {
122
        return Storage::disk($this->getDisk())->url($this->getPath());
123
    }
124
125
    /**
126
     * @param int $ownerId
127
     * @param string $ownerName
128
     * @param string $ownerAttribute
129
     * @return bool
130
     */
131
    public function addOwner(int $ownerId, string $ownerName, string $ownerAttribute): bool
132
    {
133
        return OwnerMediafile::addOwner($this->id, $ownerId, $ownerName, $ownerAttribute);
134
    }
135
136
    /**
137
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
138
     */
139
    public function getOwners()
140
    {
141
        return $this->hasMany(OwnerMediafile::class, 'mediafile_id', 'id');
142
    }
143
144
    /**
145
     * Check if the file is image.
146
     * @return bool
147
     */
148
    public function isImage(): bool
149
    {
150
        return SaveProcessor::isImage($this->mime_type);
151
    }
152
153
    /**
154
     * Check if the file is audio.
155
     * @return bool
156
     */
157
    public function isAudio(): bool
158
    {
159
        return SaveProcessor::isAudio($this->mime_type);
160
    }
161
162
    /**
163
     * Check if the file is video.
164
     * @return bool
165
     */
166
    public function isVideo(): bool
167
    {
168
        return SaveProcessor::isVideo($this->mime_type);
169
    }
170
171
    /**
172
     * Check if the file is text.
173
     * @return bool
174
     */
175
    public function isText(): bool
176
    {
177
        return SaveProcessor::isText($this->mime_type);
178
    }
179
180
    /**
181
     * Check if the file is application.
182
     * @return bool
183
     */
184
    public function isApp(): bool
185
    {
186
        return SaveProcessor::isApp($this->mime_type);
187
    }
188
189
    /**
190
     * Check if the file is word.
191
     * @return bool
192
     */
193
    public function isWord(): bool
194
    {
195
        return SaveProcessor::isWord($this->mime_type);
196
    }
197
198
    /**
199
     * Check if the file is excel.
200
     * @return bool
201
     */
202
    public function isExcel(): bool
203
    {
204
        return SaveProcessor::isExcel($this->mime_type);
205
    }
206
207
    /**
208
     * Check if the file is visio.
209
     * @return bool
210
     */
211
    public function isVisio(): bool
212
    {
213
        return SaveProcessor::isVisio($this->mime_type);
214
    }
215
216
    /**
217
     * Check if the file is PowerPoint.
218
     * @return bool
219
     */
220
    public function isPowerPoint(): bool
221
    {
222
        return SaveProcessor::isPowerPoint($this->mime_type);
223
    }
224
225
    /**
226
     * Check if the file is pdf.
227
     * @return bool
228
     */
229
    public function isPdf(): bool
230
    {
231
        return SaveProcessor::isPdf($this->mime_type);
232
    }
233
}
234