Completed
Pull Request — 2.0 (#49)
by
unknown
03:04
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
use Modules\Tag\Contracts\TaggableInterface;
11
use Modules\Tag\Traits\NamespacedEntity;
12
use Modules\Tag\Traits\TaggableTrait;
13
14
/**
15
 * Class File
16
 * @package Modules\Media\Entities
17
 * @property \Modules\Media\ValueObjects\MediaPath path
18
 */
19
class File extends Model implements TaggableInterface
20
{
21
    use Translatable, NamespacedEntity, TaggableTrait;
22
    /**
23
     * All the different images types where thumbnails should be created
24
     * @var array
25
     */
26
    private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif'];
27
28
    protected $table = 'media__files';
29
    public $translatedAttributes = ['description', 'alt_attribute', 'keywords'];
30
    protected $fillable = [
31
        'description',
32
        'alt_attribute',
33
        'keywords',
34
        'filename',
35
        'path',
36
        'extension',
37
        'mimetype',
38
        'width',
39
        'height',
40
        'filesize',
41
        'folder_id',
42
    ];
43
    protected $appends = ['path_string', 'media_type'];
44
    protected static $entityNamespace = 'asgardcms/media';
45
46
    public function getPathAttribute($value)
47
    {
48
        return new MediaPath($value);
49
    }
50
51
    public function getPathStringAttribute()
52
    {
53
        return (string) $this->path;
54
    }
55
56
    public function getMediaTypeAttribute()
57
    {
58
        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...
59
    }
60
61
    public function isImage()
62
    {
63
        return in_array(pathinfo($this->path, PATHINFO_EXTENSION), $this->imageExtensions);
64
    }
65
66
    public function getThumbnail($type)
67
    {
68
        if ($this->isImage() && $this->getKey()) {
69
            return Imagy::getThumbnail($this->path, $type);
70
        }
71
72
        return false;
73
    }
74
}
75