Completed
Push — master ( 879e21...0bf0ac )
by Nicolas
02:37
created

File   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 6
c 4
b 0
f 2
lcom 1
cbo 3
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPathAttribute() 0 4 1
A getPathStringAttribute() 0 4 1
A isImage() 0 4 1
A getThumbnail() 0 8 3
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