Completed
Pull Request — master (#258)
by
unknown
02:51
created

Media::getTypeFromExtensionAttribute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 6
eloc 13
nc 6
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\MediaLibrary\Conversion\Conversion;
7
use Spatie\MediaLibrary\Conversion\ConversionCollection;
8
use Spatie\MediaLibrary\Helpers\File;
9
use Spatie\MediaLibrary\UrlGenerator\UrlGeneratorFactory;
10
11
class Media extends Model
12
{
13
    use SortableTrait;
14
15
    const TYPE_OTHER = 'other';
16
    const TYPE_IMAGE = 'image';
17
    const TYPE_PDF = 'pdf';
18
19
    protected $guarded = ['id', 'disk', 'file_name', 'size', 'model_type', 'model_id'];
20
21
    /**
22
     * The attributes that should be casted to native types.
23
     *
24
     * @var array
25
     */
26
    protected $casts = [
27
        'manipulations' => 'array',
28
        'custom_properties' => 'array',
29
    ];
30
31
    /**
32
     * Create the polymorphic relation.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
35
     */
36
    public function model()
37
    {
38
        return $this->morphTo();
39
    }
40
41
    /**
42
     * Get the original Url to a media-file.
43
     *
44
     * @param string $conversionName
45
     *
46
     * @return string
47
     *
48
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
49
     */
50
    public function getUrl(string $conversionName = '') : string
51
    {
52
        $urlGenerator = UrlGeneratorFactory::createForMedia($this);
53
54
        if ($conversionName !== '') {
55
            $urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName));
56
        }
57
58
        return $urlGenerator->getUrl();
59
    }
60
61
    /**
62
     * Get the original path to a media-file.
63
     *
64
     * @param string $conversionName
65
     *
66
     * @return string
67
     *
68
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
69
     */
70
    public function getPath(string $conversionName = '') : string
71
    {
72
        $urlGenerator = UrlGeneratorFactory::createForMedia($this);
73
74
        if ($conversionName != '') {
75
            $urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName));
76
        }
77
78
        return $urlGenerator->getPath();
79
    }
80
81
    /**
82
     * Determine the type of a file.
83
     *
84
     * @return string
85
     */
86
    public function getTypeAttribute()
87
    {
88
        $type = $this->type_from_extension;
89
        if ($type !== self::TYPE_OTHER) {
90
            return $type;
91
        }
92
93
        return $this->type_from_mime;
94
    }
95
96
    /**
97
     * Determine the type of a file from its file extension.
98
     *
99
     * @return string
100
     */
101
    public function getTypeFromExtensionAttribute()
102
    {
103
        switch (strtolower($this->extension)) {
104
            case 'png':
105
            case 'jpg':
106
            case 'jpeg':
107
            case 'gif':
108
                return static::TYPE_IMAGE;
109
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
110
            case 'pdf':
111
                return static::TYPE_PDF;
112
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
113
            default:
114
                return static::TYPE_OTHER;
115
        }
116
    }
117
118
    /*
119
     * Determine the type of a file from its mime type
120
     */
121
    public function getTypeFromMimeAttribute() : string
122
    {
123
        if ($this->getDiskDriverName() !== 'local') {
124
            return static::TYPE_OTHER;
125
        }
126
127
128
        switch (File::getMimetype($this->getPath())) {
129
            case 'image/jpeg':
130
            case 'image/gif':
131
            case 'image/png':
132
                return static::TYPE_IMAGE;
133
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
134
            case 'application/pdf':
135
                return static::TYPE_PDF;
136
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
137
            default:
138
                return static::TYPE_OTHER;
139
        }
140
    }
141
142
    public function getExtensionAttribute() : string
143
    {
144
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
145
    }
146
147
    public function getHumanReadableSizeAttribute() : string
148
    {
149
        return File::getHumanReadableSize($this->size);
150
    }
151
152
    public function getDiskDriverName() : string
153
    {
154
        return config("filesystems.disks.{$this->disk}.driver");
155
    }
156
157
    /*
158
     * Determine if the media item has a custom property with the given name.
159
     */
160
    public function hasCustomProperty(string $propertyName) : bool
161
    {
162
        return array_key_exists($propertyName, $this->custom_properties);
163
    }
164
165
    /**
166
     * Get if the value of custom property with the given name.
167
     *
168
     * @param string $propertyName
169
     * @param mixed  $default
170
     *
171
     * @return mixed
172
     */
173
    public function getCustomProperty(string $propertyName, $default = null)
174
    {
175
        return $this->custom_properties[$propertyName] ?? $default;
176
    }
177
178
    /**
179
     * @param string $name
180
     * @param mixed  $value
181
     */
182
    public function setCustomProperty(string $name, $value)
183
    {
184
        $this->custom_properties = array_merge($this->custom_properties, [$name => $value]);
185
    }
186
187
    /*
188
     * Get all the names of the registered media conversions.
189
     */
190
    public function getMediaConversionNames() : array
191
    {
192
        $conversions = ConversionCollection::createForMedia($this);
193
194
        return $conversions->map(function (Conversion $conversion) {
195
            return $conversion->getName();
196
        })->toArray();
197
    }
198
}
199