Passed
Push — 0.6 ( fa70f4...7db85c )
by Philippe
06:36
created

AddAsset   A

Complexity

Total Complexity 6

Size/Duplication

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