Completed
Pull Request — master (#198)
by
unknown
02:16
created

Media::getTypeFromExtensionAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\MediaLibrary\Conversion\ConversionCollectionFactory;
7
use Spatie\MediaLibrary\Helpers\File;
8
use Spatie\MediaLibrary\UrlGenerator\UrlGeneratorFactory;
9
10
class Media extends Model
11
{
12
    use SortableTrait;
13
14
    const TYPE_OTHER = 'other';
15
    const TYPE_IMAGE = 'image';
16
    const TYPE_PDF = 'pdf';
17
18
    protected $guarded = ['id', 'disk', 'file_name', 'size', 'model_type', 'model_id'];
19
20
    public $imageProfileUrls = [];
21
22
    public $hasModifiedManipulations = false;
23
24
    /**
25
     * The attributes that should be casted to native types.
26
     *
27
     * @var array
28
     */
29
    protected $casts = [
30
        'manipulations' => 'array',
31
        'custom_properties' => 'array',
32
    ];
33
34
    /**
35
     * Create the polymorphic relation.
36
     *
37
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
38
     */
39
    public function model()
40
    {
41
        return $this->morphTo();
42
    }
43
44
    /**
45
     * Get the original Url to a media-file.
46
     *
47
     * @param string $conversionName
48
     *
49
     * @return string
50
     *
51
     * @throws \Spatie\MediaLibrary\Exceptions\UnknownConversion
52
     */
53
    public function getUrl($conversionName = '')
54
    {
55
        $urlGenerator = $this->getUrlGenerator($conversionName);
56
57
        return $urlGenerator->getUrl();
58
    }
59
60
    /**
61
     * Get the original full Url to a media-file.
62
     *
63
     * @param string $conversionName
64
     *
65
     * @return string
66
     *
67
     * @throws \Spatie\MediaLibrary\Exceptions\UnknownConversion
68
     */
69
    public function getFullUrl($conversionName = '')
70
    {
71
        $urlGenerator = $this->getUrlGenerator($conversionName);
72
73
        return $urlGenerator->getFullUrl();
74
    }
75
76
    /**
77
     * Get the original path to a media-file.
78
     *
79
     * @param string $conversionName
80
     *
81
     * @return string
82
     *
83
     * @throws \Spatie\MediaLibrary\Exceptions\UnknownConversion
84
     */
85
    public function getPath($conversionName = '')
86
    {
87
        $urlGenerator = $this->getUrlGenerator($conversionName);
88
89
        return $urlGenerator->getPath();
90
    }
91
92
    /**
93
     * Determine the type of a file.
94
     *
95
     * @return string
96
     */
97
    public function getTypeAttribute()
98
    {
99
        $type = $this->type_from_extension;
0 ignored issues
show
Documentation introduced by
The property type_from_extension does not exist on object<Spatie\MediaLibrary\Media>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
        if ($type !== Media::TYPE_OTHER) {
101
            return $type;
102
        }
103
104
        return $this->type_from_mime;
0 ignored issues
show
Documentation introduced by
The property type_from_mime does not exist on object<Spatie\MediaLibrary\Media>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
    }
106
107
    /**
108
     * Determine the type of a file from its file extension
109
     *
110
     * @return string
111
     */
112
    public function getTypeFromExtensionAttribute()
113
    {
114
        $extension = strtolower($this->extension);
115
116
        if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
117
            return static::TYPE_IMAGE;
118
        }
119
120
        if ($extension == 'pdf') {
121
            return static::TYPE_PDF;
122
        }
123
124
        return static::TYPE_OTHER;
125
    }
126
127
    /**
128
     * Determine the type of a file from its mime type
129
     *
130
     * @return string
131
     */
132
    public function getTypeFromMimeAttribute()
133
    {
134
        $mime = File::getMimetype($this->getPath());
135
136
        if (in_array($mime, ['image/jpeg', 'image/gif', 'image/png'])) {
137
            return static::TYPE_IMAGE;
138
        }
139
140
        if ($mime == 'application/pdf') {
141
            return static::TYPE_PDF;
142
        }
143
144
        return static::TYPE_OTHER;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getExtensionAttribute()
151
    {
152
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getHumanReadableSizeAttribute()
159
    {
160
        return File::getHumanReadableSize($this->size);
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    public function getDiskDriverName()
167
    {
168
        return config('filesystems.disks.'.$this->disk.'.driver');
169
    }
170
171
    /**
172
     * Determine if the media item has a custom property with the given name.
173
     *
174
     * @param string $propertyName
175
     *
176
     * @return bool
177
     */
178
    public function hasCustomProperty($propertyName)
179
    {
180
        return array_key_exists($propertyName, $this->custom_properties);
181
    }
182
183
    /**
184
     * Get if the value of custom property with the given name.
185
     *
186
     * @param string $propertyName
187
     * @param mixed  $propertyName
188
     *
189
     * @return mixed
190
     */
191
    public function getCustomProperty($propertyName, $default = null)
192
    {
193
        if (!$this->hasCustomProperty($propertyName)) {
194
            return $default;
195
        }
196
197
        return $this->custom_properties[$propertyName];
198
    }
199
200
    public function setCustomProperty($name, $value)
201
    {
202
        $this->custom_properties = array_merge($this->custom_properties, [$name => $value]);
203
    }
204
205
    /**
206
     * Get the UrlGenerator instance.
207
     *
208
     * @param $conversionName
209
     *
210
     * @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator
211
     *
212
     * @throws \Spatie\MediaLibrary\Exceptions\UnknownConversion
213
     */
214
    protected function getUrlGenerator($conversionName)
215
    {
216
        $urlGenerator = UrlGeneratorFactory::createForMedia($this);
217
218
        if ($conversionName != '') {
219
            $urlGenerator->setConversion(ConversionCollectionFactory::createForMedia($this)->getByName($conversionName));
220
        }
221
222
        return $urlGenerator;
223
    }
224
}
225