AssetUploader   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 49
dl 0
loc 193
ccs 53
cts 53
cp 1
rs 10
c 3
b 1
f 0
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A upload() 0 19 6
A uploadMultiple() 0 14 2
A uploadFromBase64() 0 3 1
A uploadFromUrl() 0 3 1
A prepareOptions() 0 19 2
A uploadFromUrlToAsset() 0 12 1
A uploadBase64ToAsset() 0 9 1
A uploadToAsset() 0 17 3
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Illuminate\Http\UploadedFile;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use InvalidArgumentException;
9
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileCannotBeAdded;
10
use Spatie\MediaLibrary\MediaCollections\FileAdder;
11
use Thinktomorrow\AssetLibrary\Asset;
12
use Traversable;
13
14
class AssetUploader
15
{
16
    /**
17
     * Uploads the file/files or asset by creating the
18
     * asset that is needed to upload the files too.
19
     *
20
     * @param Asset|Traversable|array|Collection|UploadedFile $files
21
     * @param string|null $filename
22
     * @return Collection|null|Asset
23
     * @throws FileCannotBeAdded
24
     */
25 34
    public static function upload($files, ?string $filename = null, string $collection = 'default', string $disk = '')
26
    {
27 34
        if (! $files) {
28 2
            throw new InvalidArgumentException();
29
        }
30
31 32
        if ($files instanceof Asset) {
32 2
            return $files;
33
        }
34
35 31
        if (is_array($files) || $files instanceof Traversable) {
36 2
            return self::uploadMultiple($files);
37
        }
38
39 30
        if (! ($files instanceof UploadedFile)) {
0 ignored issues
show
introduced by
$files is always a sub-type of Illuminate\Http\UploadedFile.
Loading history...
40 1
            throw new InvalidArgumentException();
41
        }
42
43 29
        return self::uploadToAsset($files, Asset::create(), $filename, false, $collection, $disk);
44
    }
45
46
    /**
47
     * Uploads the multiple files or assets by creating the
48
     * asset that is needed to upload the files too.
49
     *
50
     * @param Asset|Traversable|array $files
51
     * @param string $collection
52
     * @param string $disk
53 2
     * @return Collection
54
     * @throws FileCannotBeAdded
55 2
     * @throws FileCannotBeAdded\DiskDoesNotExist
56
     * @throws FileCannotBeAdded\FileDoesNotExist
57 2
     * @throws FileCannotBeAdded\FileIsTooBig
58 1
     */
59
    private static function uploadMultiple($files, string $collection = 'default', string $disk = '')
60 2
    {
61 2
        $list = collect([]);
62 2
        collect($files)->each(function ($file) use ($list, $collection, $disk) {
63
            if ($file instanceof Asset) {
64 2
                $list->push($file);
65
            } else {
66 2
                $asset = new Asset();
67
                $asset->save();
68
                $list->push(self::uploadToAsset($file, $asset, null, false, $collection, $disk));
69
            }
70
        });
71
72
        return $list;
73
    }
74
75
    /**
76
     * Uploads the file/files or asset by creating the
77
     * asset that is needed to upload the files too.
78 6
     *
79
     * @param string $file
80 6
     * @param string|null $filename
81
     * @return Collection|null|Asset
82
     * @throws FileCannotBeAdded
83
     */
84
    public static function uploadFromBase64(string $file, string $filename, string $collection = 'default', string $disk = '')
85
    {
86
        return self::uploadBase64ToAsset($file, Asset::create(), $filename, $collection, $disk);
87
    }
88
89
    /**
90
     * Uploads the url by creating the
91 11
     * asset that is needed to upload the files too.
92
     *
93 11
     * @param string $url
94
     * @return Asset
95
     * @throws FileCannotBeAdded
96
     */
97
    public static function uploadFromUrl(string $url, string $collection = 'default', string $disk = '')
98
    {
99
        return self::uploadFromUrlToAsset($url, Asset::create(), $collection, $disk);
100
    }
101
102
    /**
103
     * Uploads the given file to this instance of asset
104
     * and sets the dimensions as a custom property.
105
     *
106 31
     * @param UploadedFile $file
107
     * @param Asset $asset
108 31
     * @param string|null $filename
109 1
     * @param bool $responsive
110
     * @param string $collection
111
     * @param string $disk
112 30
     * @return Asset
113
     * @throws FileCannotBeAdded
114 30
     */
115
    public static function uploadToAsset($file, $asset, $filename = null, bool $responsive = false, string $collection = 'default', string $disk = ''): Asset
116 30
    {
117
        if (! $file) {
0 ignored issues
show
introduced by
$file is of type Illuminate\Http\UploadedFile, thus it always evaluated to true.
Loading history...
118 30
            throw new InvalidArgumentException();
119
        }
120
121
        $fileAdd = $asset->addMedia($file);
122
123
        $fileAdd = self::prepareOptions($fileAdd, $filename);
124
125
        if($responsive) {
126
            $fileAdd = $fileAdd->withResponsiveImages();
127
        }
128
129
        $fileAdd->toMediaCollection($collection, $disk);
130
131
        return $asset->load('media');
132 6
    }
133
134 6
    /**
135
     * Uploads the given file to this instance of asset
136 6
     * and sets the dimensions as a custom property.
137
     *
138 6
     * @param string $file
139
     * @param Asset $asset
140 6
     * @param string|null $filename
141
     * @return Asset
142
     * @throws FileCannotBeAdded
143
     * @internal param $files
144
     */
145
    public static function uploadBase64ToAsset(string $file, $asset, string $filename, string $collection = 'default', string $disk = ''): Asset
146
    {
147
        $fileAdd = $asset->addMediaFromBase64($file);
148
149
        $fileAdd = self::prepareOptions($fileAdd, $filename);
150
151
        $fileAdd->toMediaCollection($collection, $disk);
152 11
153
        return $asset->load('media');
154 11
    }
155
156 11
    /**
157 11
     * Uploads the given file to this instance of asset
158
     * and sets the dimensions as a custom property.
159 11
     *
160
     * @param string $url
161 11
     * @param Asset $asset
162
     * @return Asset
163 11
     * @throws FileCannotBeAdded
164
     */
165
    public static function uploadFromUrlToAsset(string $url, $asset, string $collection = 'default', string $disk = ''): Asset
166
    {
167
        $fileAdd = $asset->addMediaFromUrl($url);
168
169
        $filename = substr($url, strrpos($url, '/') + 1);
170
        $fileAdd->setName($filename);
171
172
        $fileAdd = self::prepareOptions($fileAdd, $filename);
173
174
        $fileAdd->toMediaCollection($collection, $disk);
175 46
176
        return $asset->load('media');
177 46
    }
178 19
179 19
    /**
180
     * Set the possible options on the fileAdder. This includes preserveOriginal
181
     * and filename.
182 46
     *
183
     * @param FileAdder $fileAdd
184
     * @param string|null $filename
185
     * @return FileAdder
186 46
     * @throws FileCannotBeAdded
187 46
     */
188 46
    private static function prepareOptions($fileAdd, $filename): FileAdder
189
    {
190 46
        if ($filename) {
191 46
            $fileAdd->usingName(substr($filename, 0, strpos($filename, '.')));
192
            $fileAdd->usingFileName($filename);
193 46
        }
194
195
        $fileAdd->preservingOriginal();
196
197
        // Sanitize filename by sluggifying the filename without the extension
198
        $fileAdd->sanitizingFileName(function ($filename) {
199
            $extension = substr($filename, strrpos($filename, '.') + 1);
200
            $filename  = substr($filename, 0, strrpos($filename, '.'));
201
            $filename  = Str::slug($filename).'.'.$extension;
202
203
            return strtolower($filename);
204
        });
205
206
        return $fileAdd;
207
    }
208
}
209