Completed
Push — 0.6 ( 3caceb...07d55c )
by Ben
21:59
created

AddAsset::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 5
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\AssetLibrary\Asset;
7
use Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException;
8
use Thinktomorrow\AssetLibrary\HasAsset;
9
10
class AddAsset
11
{
12
    private $order;
13
14
    /**
15
     * Add a file to this model, accepts a type and locale to be saved with the file.
16
     *
17
     * @param \Thinktomorrow\AssetLibrary\HasAsset $model
18
     * @param $file
19
     * @param string $type
20
     * @param string|null $locale
21
     * @return Asset
22
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
23
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
24
     */
25
    public function add(HasAsset $model, $file, string $type, string $locale, ?string $filename = null): Asset
26 42
    {
27
        $asset = $this->uploadAssetFromInput($file, $filename);
28 42
29
        $this->attachAssetToModel($asset, $model, $type, $locale);
30 42
31 6
        return $asset;
32
    }
33 36
34
    /**
35
     * Adds multiple files to this model, accepts a type and locale to be saved with the file.
36 42
     *
37 42
     * @param $files
38
     * @param string $type
39
     * @param string|null $locale
40 42
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
41
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
42
     */
43
    public function addMultiple(HasAsset $model, Collection $files, string $type, string $locale): Collection
44
    {
45
        $assets = collect();
46
47
        $files->each(function ($file, $filename) use ($assets, $model, $type, $locale) {
48
            $filename = is_string($filename) ? $filename : '';
49
            $assets->push($this->add($model, $file, $type, $locale, $filename));
50
        });
51
52 5
        return $assets;
53
    }
54 5
55 5
    public function setOrder(int $order = null): AddAsset
56
    {
57
        $this->order = $order;
58 5
59 5
        return $this;
60 5
    }
61
62 5
    /**
63
     * Attaches this asset instance to the given model and
64
     * sets the type and locale to the given values and
65 13
     * returns the model with the asset relationship.
66
     *
67 13
     * @param HasAsset $model
68
     * @param string $type
69 13
     * @param null|string $locale
70
     * @param null|int $order
71
     * @return void
72
     * @throws AssetUploadException
73
     */
74
    private function attachAssetToModel(Asset $asset, HasAsset $model, string $type, string $locale): void
75
    {
76
        if ($model->assetRelation()->get()->contains($asset)) {
77
            throw AssetUploadException::assetAlreadyLinked();
78
        }
79
80
        $model->assetRelation()->attach($asset, ['type' => $type, 'locale' => $locale, 'order' => $this->order]);
81
    }
82
83
    private function uploadAssetFromInput($file, ?string $filename = null): Asset
84 42
    {
85
        if($file instanceof Asset) return $file;
86 42
87 1
        if (is_string($file)) {
88
            return AssetUploader::uploadFromBase64($file, $filename);
89
        }
90 42
91
        return AssetUploader::upload($file, $filename);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Thinktomorrow\Ass...pload($file, $filename) could return the type Illuminate\Support\Collection|null which is incompatible with the type-hinted return Thinktomorrow\AssetLibrary\Asset. Consider adding an additional type-check to rule them out.
Loading history...
92 42
    }
93
}
94