Completed
Pull Request — 2.0 (#49)
by
unknown
02:59
created

File::getMediaTypeAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Modules\Media\Entities;
4
5
use Dimsav\Translatable\Translatable;
6
use Illuminate\Database\Eloquent\Model;
7
use Modules\Media\Helpers\FileHelper;
8
use Modules\Media\Image\Facade\Imagy;
9
use Modules\Media\ValueObjects\MediaPath;
10
11
/**
12
 * Class File
13
 * @package Modules\Media\Entities
14
 * @property \Modules\Media\ValueObjects\MediaPath path
15
 */
16
class File extends Model
17
{
18
    use Translatable;
19
    /**
20
     * All the different images types where thumbnails should be created
21
     * @var array
22
     */
23
    private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif'];
24
25
    protected $table = 'media__files';
26
    public $translatedAttributes = ['description', 'alt_attribute', 'keywords'];
27
    protected $fillable = [
28
        'description',
29
        'alt_attribute',
30
        'keywords',
31
        'filename',
32
        'path',
33
        'extension',
34
        'mimetype',
35
        'width',
36
        'height',
37
        'filesize',
38
        'folder_id',
39
    ];
40
    protected $appends = ['path_string', 'media_type'];
41
42
    public function getPathAttribute($value)
43
    {
44
        return new MediaPath($value);
45
    }
46
47
    public function getPathStringAttribute()
48
    {
49
        return (string) $this->path;
50
    }
51
52
    public function getMediaTypeAttribute()
53
    {
54
        return FileHelper::getTypeByMimetype($this->mimetype);
0 ignored issues
show
Documentation introduced by
The property mimetype does not exist on object<Modules\Media\Entities\File>. 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...
55
    }
56
57
    public function isImage()
58
    {
59
        return in_array(pathinfo($this->path, PATHINFO_EXTENSION), $this->imageExtensions);
60
    }
61
62
    public function getThumbnail($type)
63
    {
64
        if ($this->isImage() && $this->getKey()) {
65
            return Imagy::getThumbnail($this->path, $type);
66
        }
67
68
        return false;
69
    }
70
}
71