Passed
Push — master ( 238907...2271b3 )
by Philippe
07:19
created

Asset   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Test Coverage

Coverage 95.52%

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 187
ccs 64
cts 67
cp 0.9552
rs 10
c 0
b 0
f 0
wmc 28

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtensionType() 0 24 6
A getSize() 0 3 2
A getExtensionForFilter() 0 7 2
A isMediaEmpty() 0 3 1
A hasFile() 0 3 1
A crop() 0 14 2
A url() 0 9 2
A getDimensions() 0 14 3
A exists() 0 3 1
A filename() 0 3 1
A getMimeType() 0 3 2
A isUsed() 0 4 1
A registerMediaConversions() 0 16 3
A isUnused() 0 4 1
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary;
4
5
use Illuminate\Support\Facades\DB;
6
use Spatie\MediaLibrary\Models\Media;
7
use Illuminate\Database\Eloquent\Model;
8
use Spatie\MediaLibrary\HasMedia\HasMedia;
9
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
10
use Thinktomorrow\AssetLibrary\Exceptions\ConfigException;
11
use Thinktomorrow\AssetLibrary\Exceptions\CorruptMediaException;
12
13
class Asset extends Model implements HasMedia
14
{
15
    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...
16
17
    private $order;
0 ignored issues
show
introduced by
The private property $order is not used, and could be removed.
Loading history...
18
19
    /**
20
     * @return bool
21
     */
22 1
    public function hasFile(): bool
23
    {
24 1
        return (bool) $this->url();
25
    }
26
27
    /**
28
     * @param string $size
29
     * @return string
30
     */
31 17
    public function filename($size = ''): string
32
    {
33 17
        return basename($this->url($size));
34
    }
35
36 1
    public function exists(): bool
37
    {
38 1
        return true;
39
    }
40
41
    /**
42
     * @param string $size
43
     * @return string
44
     */
45 47
    public function url($size = ''): string
46
    {
47 47
        $media = $this->getFirstMedia();
48
49 47
        if ($media == null) {
50 1
            return '';
51
        }
52
53 46
        return $media->getUrl($size);
54
    }
55
56
    /**
57
     * @return bool|string
58
     */
59 2
    public function getExtensionForFilter()
60
    {
61 2
        if ($extension = $this->getExtensionType()) {
62 1
            return $extension;
63
        }
64
65 1
        return '';
66
    }
67
68
    /**
69
     * @return null|string
70
     * @throws CorruptMediaException
71
     */
72 2
    public function getExtensionType(): ?string
73
    {
74 2
        $media = $this->getMedia()->first();
75
76 2
        if ($media == null) {
77 1
            throw CorruptMediaException::missingMediaRelation($this->id);
78
        }
79
80 1
        $extension = explode('.', $media->file_name);
81 1
        $extension = end($extension);
82
83 1
        if ($extension) {
84 1
            if (in_array(strtolower($extension), ['xls', 'xlsx', 'numbers', 'sheets'])) {
85 1
                return 'xls';
86
            }
87 1
            if (in_array(strtolower($extension), ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp'])) {
88 1
                return 'image';
89
            }
90 1
            if (strtolower($extension) === 'pdf') {
91 1
                return 'pdf';
92
            }
93
        }
94
95 1
        return null;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 2
    public function getMimeType(): string
102
    {
103 2
        return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->mime_type;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109 5
    public function isMediaEmpty(): bool
110
    {
111 5
        return $this->getMedia()->isEmpty();
112
    }
113
114
    /**
115
     * @return string
116
     */
117 2
    public function getSize(): string
118
    {
119 2
        return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->human_readable_size;
120
    }
121
122
    /**
123
     * @param string|null $size
124
     * @return string
125
     */
126 3
    public function getDimensions($size = null): string
127
    {
128 3
        if ($this->isMediaEmpty()) {
129 1
            return '';
130
        }
131
132
        //TODO Check the other sizes as well
133 2
        if ($size === 'cropped') {
134 1
            $dimensions = explode(',', $this->getMedia()[0]->manipulations['cropped']['manualCrop']);
135
136 1
            return $dimensions[0].' x'.$dimensions[1];
137
        }
138
139 1
        return $this->getMedia()[0]->getCustomProperty('dimensions') ?? '';
140
    }
141
142 1
    public function isUsed()
143
    {
144 1
        $pivots = DB::table('asset_pivots')->where('asset_id', $this->id)->where('unused', false)->get();
145 1
        return !$pivots->isEmpty();
146
    }
147
148
    public function isUnused()
149
    {
150
        $pivots = DB::table('asset_pivots')->where('asset_id', $this->id)->where('unused', false)->get();
151
        return $pivots->isEmpty();
152
    }
153
154
    /**
155
     * @param $width
156
     * @param $height
157
     * @param $x
158
     * @param $y
159
     * @return $this
160
     * @throws ConfigException
161
     */
162 2
    public function crop($width, $height, $x, $y)
163
    {
164 2
        if (! config('thinktomorrow.assetlibrary.allowCropping')) {
165 1
            throw ConfigException::croppingDisabled();
166
        }
167 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...
168
            'cropped'   => [
169 1
                'manualCrop' => $width.', '.$height.', '.$x.', '.$y,
170
            ],
171
        ];
172
173 1
        $this->media[0]->save();
174
175 1
        return $this;
176
    }
177
178
    /**
179
     * Register the conversions that should be performed.
180
     *
181
     * @param Media|null $media
182
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
183
     */
184 49
    public function registerMediaConversions(Media $media = null)
185
    {
186 49
        $conversions = config('thinktomorrow.assetlibrary.conversions');
187
188 49
        foreach ($conversions as $key => $value) {
189 49
            $this->addMediaConversion($key)
190 49
                ->width($value['width'])
191 49
                ->height($value['height'])
192 49
                ->keepOriginalImageFormat()
193 49
                ->optimize();
194
        }
195
196 49
        if (config('thinktomorrow.assetlibrary.allowCropping')) {
197 1
            $this->addMediaConversion('cropped')
198 1
                ->keepOriginalImageFormat()
199 1
                ->optimize();
200
        }
201 49
    }
202
}
203