Completed
Push — 0.6 ( 3caceb...07d55c )
by Ben
21:59
created

Asset::crop()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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