Completed
Push — 0.6 ( 2a9301...913fb9 )
by Philippe
06:33
created

Asset::attachToModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 2
b 1
f 0
nc 2
nop 3
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A Asset::filename() 0 3 1
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Models;
4
5
use Spatie\MediaLibrary\Models\Media;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
8
use Thinktomorrow\AssetLibrary\Interfaces\HasAsset;
9
use Thinktomorrow\AssetLibrary\Exceptions\ConfigException;
10
use Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException;
11
use Thinktomorrow\AssetLibrary\Exceptions\CorruptMediaException;
12
13
class Asset extends Model implements HasAsset
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\Models\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
    /**
37
     * @param string $size
38
     * @return string
39
     */
40 51
    public function url($size = ''): string
41
    {
42 51
        $media = $this->getMedia()->first();
43
44 51
        if ($media == null) {
45 1
            throw CorruptMediaException::corrupt($this->id);
46
        }
47
48 50
        return $media->getUrl($size);
49
    }
50
51
    /**
52
     * @return bool|string
53
     */
54 2
    public function getExtensionForFilter()
55
    {
56 2
        if ($extension = $this->getExtensionType()) {
57 1
            return $extension;
58
        }
59
60 1
        return '';
61
    }
62
63
    /**
64
     * @return null|string
65
     * @throws CorruptMediaException
66
     */
67 2
    public function getExtensionType(): ?string
68
    {
69 2
        $media = $this->getMedia()->first();
70
71 2
        if ($media == null) {
72 1
            throw CorruptMediaException::corrupt($this->id);
73
        }
74
75 1
        $extension = explode('.', $media->file_name);
76 1
        $extension = end($extension);
77
78 1
        if ($extension) {
79 1
            if (in_array(strtolower($extension), ['xls', 'xlsx', 'numbers', 'sheets'])) {
80 1
                return 'xls';
81
            }
82 1
            if (in_array(strtolower($extension), ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp'])) {
83 1
                return 'image';
84
            }
85 1
            if (strtolower($extension) === 'pdf') {
86 1
                return 'pdf';
87
            }
88
        }
89
90 1
        return null;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 2
    public function getMimeType(): string
97
    {
98 2
        return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->mime_type;
99
    }
100
101
    /**
102
     * @return bool
103
     */
104 5
    public function isMediaEmpty(): bool
105
    {
106 5
        return $this->getMedia()->isEmpty();
107
    }
108
109
    /**
110
     * @return string
111
     */
112 2
    public function getSize(): string
113
    {
114 2
        return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->human_readable_size;
115
    }
116
117
    /**
118
     * @param string|null $size
119
     * @return string
120
     */
121 3
    public function getDimensions($size = null): string
122
    {
123 3
        if ($this->isMediaEmpty()) {
124 1
            return '';
125
        }
126
127
        //TODO Check the other sizes as well
128 2
        if ($size === 'cropped') {
129 1
            $dimensions = explode(',', $this->getMedia()[0]->manipulations['cropped']['manualCrop']);
130
131 1
            return $dimensions[0].' x'.$dimensions[1];
132
        }
133
134 1
        return $this->getMedia()[0]->getCustomProperty('dimensions');
135
    }
136
137
    /**
138
     * @param $width
139
     * @param $height
140
     * @param $x
141
     * @param $y
142
     * @return $this
143
     * @throws ConfigException
144
     */
145 2
    public function crop($width, $height, $x, $y)
146
    {
147 2
        if (! config('assetlibrary.allowCropping')) {
148 1
            throw ConfigException::create();
149
        }
150 1
        $this->media[0]->manipulations = [
0 ignored issues
show
Bug introduced by
The property media does not seem to exist on Thinktomorrow\AssetLibrary\Models\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...
151
            'cropped'   => [
152 1
                'manualCrop' => $width.', '.$height.', '.$x.', '.$y,
153
            ],
154
        ];
155
156 1
        $this->media[0]->save();
157
158 1
        return $this;
159
    }
160
161
    /**
162
     * Register the conversions that should be performed.
163
     *
164
     * @param Media|null $media
165
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
166
     */
167 48
    public function registerMediaConversions(Media $media = null)
168
    {
169 48
        $conversions = config('assetlibrary.conversions');
170
171 48
        foreach ($conversions as $key => $value) {
172 48
            $this->addMediaConversion($key)
173 48
                ->width($value['width'])
174 48
                ->height($value['height'])
175 48
                ->keepOriginalImageFormat()
176 48
                ->optimize();
177
        }
178
179 48
        if (config('assetlibrary.allowCropping')) {
180 1
            $this->addMediaConversion('cropped')
181 1
                ->keepOriginalImageFormat()
182 1
                ->optimize();
183
        }
184 48
    }
185
}
186