Completed
Push — 0.6 ( 2a9301...913fb9 )
by Philippe
06:33
created

AddAsset::setOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException;
10
11
class AddAsset
12
{
13
14
    private $order;
15
    /**
16
     * Add a file to this model, accepts a type and locale to be saved with the file.
17
     *
18
     * @param \Thinktomorrow\AssetLibrary\Interfaces\HasAsset $model
19
     * @param $file
20
     * @param string $type
21
     * @param string|null $locale
22
     * @return Asset
23
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
24
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
25
     */
26 41
    public function add(HasAsset $model, $file, string $type = '', ?string $locale = null, ?string $filename = null): Asset
27
    {
28 41
        $locale = $this->normalizeLocaleString($locale);
29
30 41
        if (is_string($file)) {
31 5
            $asset = AssetUploader::uploadFromBase64($file, $filename);
32
        } else {
33 36
            $asset = AssetUploader::upload($file, $filename);
34
        }
35
36 41
        if ($asset instanceof Asset) {
37 41
            $this->attachAssetToModel($asset, $model, $type, $locale);
38
        }
39
40 41
        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...
41
    }
42
43
    /**
44
     * Adds multiple files to this model, accepts a type and locale to be saved with the file.
45
     *
46
     * @param $files
47
     * @param string $type
48
     * @param string|null $locale
49
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
50
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
51
     */
52 7
    public function addMultiple(HasAsset $model, Collection $files, $type = '', $locale = null): Collection
53
    {
54 7
        $locale = $this->normalizeLocaleString($locale);
55 7
        $assets = collect();
56
57
        $files->each(function ($file, $filename) use ($assets, $model, $type, $locale) {
58 7
            $filename = is_string($filename) ? $filename : '';
59 7
            $assets->push($this->add($model, $file, $type, $locale, $filename));
60 7
        });
61
62 7
        return $assets;
63
    }
64
65 13
    public function setOrder(int $order = null): AddAsset
66
    {
67 13
        $this->order = $order;
68
69 13
        return $this;
70
    }
71
72
    /**
73
     * Attaches this asset instance to the given model and
74
     * sets the type and locale to the given values and
75
     * returns the model with the asset relationship.
76
     *
77
     * @param HasAsset $model
78
     * @param string $type
79
     * @param null|string $locale
80
     * @param null|int $order
81
     * @return HasAsset
82
     * @throws AssetUploadException
83
     */
84 41
    private function attachAssetToModel(Asset $asset, HasAsset $model, $type = '', $locale = null): HasAsset
85
    {
86 41
        if ($model->assetRelation()->get()->contains($asset)) {
0 ignored issues
show
Bug introduced by
The method assetRelation() does not exist on Thinktomorrow\AssetLibrary\Interfaces\HasAsset. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\AssetLibrary\Interfaces\HasAsset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        if ($model->/** @scrutinizer ignore-call */ assetRelation()->get()->contains($asset)) {
Loading history...
87 1
            throw AssetUploadException::create();
88
        }
89
90 41
        $locale = $locale ?? config('app.fallback_locale');
91
92 41
        $model->assetRelation()->attach($asset, ['type' => $type, 'locale' => $locale, 'order' => $this->order]);
93
94 41
        return $model->load('assetRelation');
0 ignored issues
show
Bug introduced by
The method load() does not exist on Thinktomorrow\AssetLibrary\Interfaces\HasAsset. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\AssetLibrary\Interfaces\HasAsset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        return $model->/** @scrutinizer ignore-call */ load('assetRelation');
Loading history...
95
    }
96
97
98
    /**
99
     * @param string|null $locale
100
     * @return string
101
     */
102 41
    private function normalizeLocaleString($locale = null): string
103
    {
104 41
        $locale = $locale ?? config('app.fallback_locale');
105
106 41
        return $locale;
107
    }
108
}
109