Completed
Push — master ( 05d2b7...63b013 )
by Freek
7s
created

Media::getTypeAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
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 = UrlGeneratorFactory::createForMedia($this);
56
57
        if ($conversionName != '') {
58
            $urlGenerator->setConversion(ConversionCollectionFactory::createForMedia($this)->getByName($conversionName));
59
        }
60
61
        return $urlGenerator->getUrl();
62
    }
63
64
    /**
65
     * Get the original path to a media-file.
66
     *
67
     * @param string $conversionName
68
     *
69
     * @return string
70
     *
71
     * @throws \Spatie\MediaLibrary\Exceptions\UnknownConversion
72
     */
73
    public function getPath($conversionName = '')
74
    {
75
        $urlGenerator = UrlGeneratorFactory::createForMedia($this);
76
77
        if ($conversionName != '') {
78
            $urlGenerator->setConversion(ConversionCollectionFactory::createForMedia($this)->getByName($conversionName));
79
        }
80
81
        return $urlGenerator->getPath();
82
    }
83
84
    /**
85
     * Determine the type of a file.
86
     *
87
     * @return string
88
     */
89
    public function getTypeAttribute()
90
    {
91
        $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...
92
        if ($type !== Media::TYPE_OTHER) {
93
            return $type;
94
        }
95
96
        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...
97
    }
98
99
    /**
100
     * Determine the type of a file from its file extension
101
     *
102
     * @return string
103
     */
104
    public function getTypeFromExtensionAttribute()
105
    {
106
        $extension = strtolower($this->extension);
107
108
        if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
109
            return static::TYPE_IMAGE;
110
        }
111
112
        if ($extension == 'pdf') {
113
            return static::TYPE_PDF;
114
        }
115
116
        return static::TYPE_OTHER;
117
    }
118
119
    /**
120
     * Determine the type of a file from its mime type
121
     *
122
     * @return string
123
     */
124
    public function getTypeFromMimeAttribute()
125
    {
126
        $mime = File::getMimetype($this->getPath());
127
128
        if (in_array($mime, ['image/jpeg', 'image/gif', 'image/png'])) {
129
            return static::TYPE_IMAGE;
130
        }
131
132
        if ($mime == 'application/pdf') {
133
            return static::TYPE_PDF;
134
        }
135
136
        return static::TYPE_OTHER;
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getExtensionAttribute()
143
    {
144
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getHumanReadableSizeAttribute()
151
    {
152
        return File::getHumanReadableSize($this->size);
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getDiskDriverName()
159
    {
160
        return config('filesystems.disks.'.$this->disk.'.driver');
161
    }
162
163
    /**
164
     * Determine if the media item has a custom property with the given name.
165
     *
166
     * @param string $propertyName
167
     *
168
     * @return bool
169
     */
170
    public function hasCustomProperty($propertyName)
171
    {
172
        return array_key_exists($propertyName, $this->custom_properties);
173
    }
174
175
    /**
176
     * Get if the value of custom property with the given name.
177
     *
178
     * @param string $propertyName
179
     * @param mixed  $propertyName
180
     *
181
     * @return mixed
182
     */
183
    public function getCustomProperty($propertyName, $default = null)
184
    {
185
        if (!$this->hasCustomProperty($propertyName)) {
186
            return $default;
187
        }
188
189
        return $this->custom_properties[$propertyName];
190
    }
191
192
    public function setCustomProperty($name, $value)
193
    {
194
        $this->custom_properties = array_merge($this->custom_properties, [$name => $value]);
195
    }
196
}
197