MimeTypeRule   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateMediaItem() 0 7 2
A __construct() 0 3 1
A message() 0 4 1
1
<?php
2
3
namespace Spatie\MediaLibraryPro\Rules\ItemRules;
4
5
use Illuminate\Support\Arr;
6
7
class MimeTypeRule extends MediaItemRule
8
{
9
    protected array $allowedMimeTypes;
10
11
    /** @var string|array */
12
    public function __construct($allowedMimeTypes)
13
    {
14
        $this->allowedMimeTypes = Arr::wrap($allowedMimeTypes);
15
    }
16
17
    public function validateMediaItem(): bool
18
    {
19
        if (! $media = $this->getTemporaryUploadMedia()) {
20
            return true;
21
        }
22
23
        return in_array($media->mime_type, $this->allowedMimeTypes);
24
    }
25
26
    public function message(): string
27
    {
28
        return __('media-library::validation.mime', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return __('media-library...is->allowedMimeTypes))) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
29
            'mimes' => implode(', ', $this->allowedMimeTypes),
30
        ]);
31
    }
32
}
33