Completed
Push — master ( 879e21...0bf0ac )
by Nicolas
02:37
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\ValueObjects\MediaPath;
6
7
/**
8
 * Class File
9
 * @package Modules\Media\Entities
10
 * @property \Modules\Media\ValueObjects\MediaPath path
11
 */
12
class File extends Model
13
{
14
    use Translatable;
15
    /**
16
     * All the different images types where thumbnails should be created
17
     * @var array
18
     */
19
    private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif'];
20
21
    protected $table = 'media__files';
22
    public $translatedAttributes = ['description', 'alt_attribute', 'keywords'];
23
    protected $fillable = [
24
        'description',
25
        'alt_attribute',
26
        'keywords',
27
        'filename',
28
        'path',
29
        'extension',
30
        'mimetype',
31
        'width',
32
        'height',
33
        'filesize',
34
        'folder_id',
35
    ];
36
    protected $appends = ['path_string'];
37
38
    public function getPathAttribute($value)
39
    {
40
        return new MediaPath($value);
41
    }
42
43
    public function getPathStringAttribute()
44
    {
45
        return (string) $this->path;
46
    }
47
48
    public function isImage()
49
    {
50
        return in_array(pathinfo($this->path, PATHINFO_EXTENSION), $this->imageExtensions);
51
    }
52
    
53
    public function getThumbnail($type)
54
    {
55
        if ($this->isImage() && $this->getKey()) {
56
            return Imagy::getThumbnail($this->path, $type);
57
        }
58
59
        return false;
60
    }
61
}
62