Completed
Pull Request — master (#41)
by
unknown
02:46
created

File::getThumbnail()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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