Completed
Pull Request — master (#38)
by Philippe
08:13 queued 04:01
created

AddAsset   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 61
ccs 18
cts 18
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeLocaleString() 0 5 1
A add() 0 15 3
A addMultiple() 0 11 2
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Models\Application;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\AssetLibrary\Models\Asset;
7
use Thinktomorrow\AssetLibrary\Interfaces\HasAsset;
8
use Thinktomorrow\AssetLibrary\Models\AssetUploader;
9
10
class AddAsset
11
{
12
    /**
13
     * Add a file to this model, accepts a type and locale to be saved with the file.
14
     *
15
     * @param \Thinktomorrow\AssetLibrary\Interfaces\HasAsset $model
16
     * @param $file
17
     * @param string $type
18
     * @param string|null $locale
19
     * @return Asset
20
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
21
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
22
     */
23 24
    public function add(HasAsset $model, $file, string $type = '', ?string $locale = null, ?string $filename = null): Asset
24
    {
25 24
        $locale = $this->normalizeLocaleString($locale);
26
27 24
        if (is_string($file)) {
28 5
            $asset = AssetUploader::uploadFromBase64($file, $filename);
29
        } else {
30 19
            $asset = AssetUploader::upload($file, $filename);
31
        }
32
33 24
        if ($asset instanceof Asset) {
34 24
            $asset->attachToModel($model, $type, $locale);
35
        }
36
37 23
        return $asset;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $asset could return the type Illuminate\Support\Collection|null which is incompatible with the type-hinted return Thinktomorrow\AssetLibrary\Models\Asset. Consider adding an additional type-check to rule them out.
Loading history...
38
    }
39
40
    /**
41
     * Adds multiple files to this model, accepts a type and locale to be saved with the file.
42
     *
43
     * @param $files
44
     * @param string $type
45
     * @param string|null $locale
46
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
47
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
48
     */
49 7
    public function addMultiple(HasAsset $model, Collection $files, $type = '', $locale = null): Collection
50
    {
51 7
        $locale = $this->normalizeLocaleString($locale);
52 7
        $assets = collect();
53
54
        $files->each(function ($file, $filename) use ($assets, $model, $type, $locale) {
55 7
            $filename = is_string($filename) ? $filename : '';
56 7
            $assets->push($this->add($model, $file, $type, $locale, $filename));
57 7
        });
58
59 7
        return $assets;
60
    }
61
62
    /**
63
     * @param string|null $locale
64
     * @return string
65
     */
66 24
    private function normalizeLocaleString($locale = null): string
67
    {
68 24
        $locale = $locale ?? config('app.fallback_locale');
69
70 24
        return $locale;
71
    }
72
}
73