Completed
Pull Request — master (#41)
by
unknown
02:46
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\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