Passed
Push — master ( c230fb...1c12b0 )
by Philippe
04:55
created

Asset   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 53
c 0
b 0
f 0
dl 0
loc 175
ccs 61
cts 61
cp 1
rs 10
wmc 26

12 Methods

Rating   Name   Duplication   Size   Complexity  
A hasFile() 0 3 1
A filename() 0 3 1
A getExtensionType() 0 24 6
A getSize() 0 3 2
A getExtensionForFilter() 0 7 2
A isMediaEmpty() 0 3 1
A crop() 0 14 2
A url() 0 9 2
A getDimensions() 0 14 3
A exists() 0 3 1
A getMimeType() 0 3 2
A registerMediaConversions() 0 16 3
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\MediaLibrary\HasMedia\HasMedia;
7
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
8
use Spatie\MediaLibrary\Models\Media;
9
use Thinktomorrow\AssetLibrary\Exceptions\ConfigException;
10
use Thinktomorrow\AssetLibrary\Exceptions\CorruptMediaException;
11
12
class Asset extends Model implements HasMedia
13
{
14
    use HasMediaTrait;
0 ignored issues
show
introduced by
The trait Spatie\MediaLibrary\HasMedia\HasMediaTrait requires some properties which are not provided by Thinktomorrow\AssetLibrary\Asset: $fallbackPath, $each, $mediaConversionRegistrations, $forceDeleting, $fallbackUrl, $media, $collection_name
Loading history...
15
16
    private $order;
0 ignored issues
show
introduced by
The private property $order is not used, and could be removed.
Loading history...
17
18
    /**
19
     * @return bool
20
     */
21 1
    public function hasFile(): bool
22
    {
23 1
        return (bool) $this->url();
24
    }
25
26
    /**
27
     * @param string $size
28
     * @return string
29
     */
30 17
    public function filename($size = ''): string
31
    {
32 17
        return basename($this->url($size));
33
    }
34
35 1
    public function exists(): bool
36
    {
37 1
        return true;
38
    }
39
40
    /**
41
     * @param string $size
42
     * @return string
43
     */
44 46
    public function url($size = ''): string
45
    {
46 46
        $media = $this->getMedia()->first();
47
48 46
        if ($media == null) {
49 1
            throw CorruptMediaException::missingMediaRelation($this->id);
50
        }
51
52 45
        return $media->getUrl($size);
53
    }
54
55
    /**
56
     * @return bool|string
57
     */
58 2
    public function getExtensionForFilter()
59
    {
60 2
        if ($extension = $this->getExtensionType()) {
61 1
            return $extension;
62
        }
63
64 1
        return '';
65
    }
66
67
    /**
68
     * @return null|string
69
     * @throws CorruptMediaException
70
     */
71 2
    public function getExtensionType(): ?string
72
    {
73 2
        $media = $this->getMedia()->first();
74
75 2
        if ($media == null) {
76 1
            throw CorruptMediaException::missingMediaRelation($this->id);
77
        }
78
79 1
        $extension = explode('.', $media->file_name);
80 1
        $extension = end($extension);
81
82 1
        if ($extension) {
83 1
            if (in_array(strtolower($extension), ['xls', 'xlsx', 'numbers', 'sheets'])) {
84 1
                return 'xls';
85
            }
86 1
            if (in_array(strtolower($extension), ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp'])) {
87 1
                return 'image';
88
            }
89 1
            if (strtolower($extension) === 'pdf') {
90 1
                return 'pdf';
91
            }
92
        }
93
94 1
        return null;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 2
    public function getMimeType(): string
101
    {
102 2
        return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->mime_type;
103
    }
104
105
    /**
106
     * @return bool
107
     */
108 5
    public function isMediaEmpty(): bool
109
    {
110 5
        return $this->getMedia()->isEmpty();
111
    }
112
113
    /**
114
     * @return string
115
     */
116 2
    public function getSize(): string
117
    {
118 2
        return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->human_readable_size;
119
    }
120
121
    /**
122
     * @param string|null $size
123
     * @return string
124
     */
125 3
    public function getDimensions($size = null): string
126
    {
127 3
        if ($this->isMediaEmpty()) {
128 1
            return '';
129
        }
130
131
        //TODO Check the other sizes as well
132 2
        if ($size === 'cropped') {
133 1
            $dimensions = explode(',', $this->getMedia()[0]->manipulations['cropped']['manualCrop']);
134
135 1
            return $dimensions[0].' x'.$dimensions[1];
136
        }
137
138 1
        return $this->getMedia()[0]->getCustomProperty('dimensions');
139
    }
140
141
    /**
142
     * @param $width
143
     * @param $height
144
     * @param $x
145
     * @param $y
146
     * @return $this
147
     * @throws ConfigException
148
     */
149 2
    public function crop($width, $height, $x, $y)
150
    {
151 2
        if (! config('thinktomorrow.assetlibrary.allowCropping')) {
152 1
            throw ConfigException::croppingDisabled();
153
        }
154 1
        $this->media[0]->manipulations = [
0 ignored issues
show
Bug introduced by
The property media does not seem to exist on Thinktomorrow\AssetLibrary\Asset. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
155
            'cropped'   => [
156 1
                'manualCrop' => $width.', '.$height.', '.$x.', '.$y,
157
            ],
158
        ];
159
160 1
        $this->media[0]->save();
161
162 1
        return $this;
163
    }
164
165
    /**
166
     * Register the conversions that should be performed.
167
     *
168
     * @param Media|null $media
169
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
170
     */
171 48
    public function registerMediaConversions(Media $media = null)
172
    {
173 48
        $conversions = config('thinktomorrow.assetlibrary.conversions');
174
175 48
        foreach ($conversions as $key => $value) {
176 48
            $this->addMediaConversion($key)
177 48
                ->width($value['width'])
178 48
                ->height($value['height'])
179 48
                ->keepOriginalImageFormat()
180 48
                ->optimize();
181
        }
182
183 48
        if (config('thinktomorrow.assetlibrary.allowCropping')) {
184 1
            $this->addMediaConversion('cropped')
185 1
                ->keepOriginalImageFormat()
186 1
                ->optimize();
187
        }
188 48
    }
189
}
190