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

AddAsset   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 59
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 0
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 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
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...
16
     * @param $file
17
     * @param string $type
18
     * @param string|null $locale
19
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
20
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
21
     */
22 24
    public function add(HasAsset $model, $file, string $type = '', ?string $locale = null, ?string $filename = null): Asset
23
    {
24 24
        $locale = $this->normalizeLocaleString($locale);
25
26 24
        if (is_string($file)) {
27 5
            $asset = AssetUploader::uploadFromBase64($file, $filename);
28
        } else {
29 19
            $asset = AssetUploader::upload($file, $filename);
30
        }
31 24
        if ($asset instanceof Asset) {
32 24
            $asset->attachToModel($model, $type, $locale);
33
        }
34
35 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...
36
    }
37
38
    /**
39
     * Adds multiple files to this model, accepts a type and locale to be saved with the file.
40
     *
41
     * @param $files
42
     * @param string $type
43
     * @param string|null $locale
44
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
45
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
46
     */
47 7
    public function addMultiple(HasAsset $model, Collection $files, $type = '', $locale = null): Collection
48
    {
49 7
        $locale = $this->normalizeLocaleString($locale);
50 7
        $assets = collect();
51
52
        $files->each(function ($file, $filename) use ($assets, $model, $type, $locale) {
53 7
            $filename = is_string($filename) ? $filename : '';
54 7
            $assets->push($this->add($model, $file, $type, $locale, $filename));
55 7
        });
56
57 7
        return $assets;
58
    }
59
60
    /**
61
     * @param string|null $locale
62
     * @return string
63
     */
64 24
    private function normalizeLocaleString($locale = null): string
65
    {
66 24
        $locale = $locale ?? config('app.fallback_locale');
67
68 24
        return $locale;
69
    }
70
}
71