Passed
Push — dependabot/composer/orchestra/... ( 137c82...0b1a28 )
by
unknown
04:18
created

Asset::filename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    /**
36
     * @param string $size
37
     * @return string
38
     */
39 46
    public function url($size = ''): string
40
    {
41 46
        $media = $this->getMedia()->first();
42
43 46
        if ($media == null) {
44 1
            throw CorruptMediaException::missingMediaRelation($this->id);
45
        }
46
47 45
        return $media->getUrl($size);
48
    }
49
50
    /**
51
     * @return bool|string
52
     */
53 2
    public function getExtensionForFilter()
54
    {
55 2
        if ($extension = $this->getExtensionType()) {
56 1
            return $extension;
57
        }
58
59 1
        return '';
60
    }
61
62
    /**
63
     * @return null|string
64
     * @throws CorruptMediaException
65
     */
66 2
    public function getExtensionType(): ?string
67
    {
68 2
        $media = $this->getMedia()->first();
69
70 2
        if ($media == null) {
71 1
            throw CorruptMediaException::missingMediaRelation($this->id);
72
        }
73
74 1
        $extension = explode('.', $media->file_name);
75 1
        $extension = end($extension);
76
77 1
        if ($extension) {
78 1
            if (in_array(strtolower($extension), ['xls', 'xlsx', 'numbers', 'sheets'])) {
79 1
                return 'xls';
80
            }
81 1
            if (in_array(strtolower($extension), ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp'])) {
82 1
                return 'image';
83
            }
84 1
            if (strtolower($extension) === 'pdf') {
85 1
                return 'pdf';
86
            }
87
        }
88
89 1
        return null;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 2
    public function getMimeType(): string
96
    {
97 2
        return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->mime_type;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103 5
    public function isMediaEmpty(): bool
104
    {
105 5
        return $this->getMedia()->isEmpty();
106
    }
107
108
    /**
109
     * @return string
110
     */
111 2
    public function getSize(): string
112
    {
113 2
        return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->human_readable_size;
114
    }
115
116
    /**
117
     * @param string|null $size
118
     * @return string
119
     */
120 3
    public function getDimensions($size = null): string
121
    {
122 3
        if ($this->isMediaEmpty()) {
123 1
            return '';
124
        }
125
126
        //TODO Check the other sizes as well
127 2
        if ($size === 'cropped') {
128 1
            $dimensions = explode(',', $this->getMedia()[0]->manipulations['cropped']['manualCrop']);
129
130 1
            return $dimensions[0].' x'.$dimensions[1];
131
        }
132
133 1
        return $this->getMedia()[0]->getCustomProperty('dimensions');
134
    }
135
136
    /**
137
     * @param $width
138
     * @param $height
139
     * @param $x
140
     * @param $y
141
     * @return $this
142
     * @throws ConfigException
143
     */
144 2
    public function crop($width, $height, $x, $y)
145
    {
146 2
        if (! config('assetlibrary.allowCropping')) {
147 1
            throw ConfigException::croppingDisabled();
148
        }
149 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...
150
            'cropped'   => [
151 1
                'manualCrop' => $width.', '.$height.', '.$x.', '.$y,
152
            ],
153
        ];
154
155 1
        $this->media[0]->save();
156
157 1
        return $this;
158
    }
159
160
    /**
161
     * Register the conversions that should be performed.
162
     *
163
     * @param Media|null $media
164
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
165
     */
166 48
    public function registerMediaConversions(Media $media = null)
167
    {
168 48
        $conversions = config('assetlibrary.conversions');
169
170 48
        foreach ($conversions as $key => $value) {
171 48
            $this->addMediaConversion($key)
172 48
                ->width($value['width'])
173 48
                ->height($value['height'])
174 48
                ->keepOriginalImageFormat()
175 48
                ->optimize();
176
        }
177
178 48
        if (config('assetlibrary.allowCropping')) {
179 1
            $this->addMediaConversion('cropped')
180 1
                ->keepOriginalImageFormat()
181 1
                ->optimize();
182
        }
183 48
    }
184
}
185