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 $locale |
||
21 | * @return Asset |
||
22 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
23 | * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException |
||
24 | */ |
||
25 | 59 | public function add(HasAsset $model, $file, string $type, string $locale, ?string $filename = null): Asset |
|
26 | { |
||
27 | 59 | $asset = $this->uploadAssetFromInput($file, $filename); |
|
28 | |||
29 | 59 | $this->attachAssetToModel($asset, $model, $type, $locale); |
|
30 | |||
31 | 59 | return $asset; |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * Adds multiple files to this model, accepts a type and locale to be saved with the file. |
||
36 | * |
||
37 | * @param $files |
||
38 | * @param string $type |
||
39 | * @param string $locale |
||
40 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
41 | * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException |
||
42 | */ |
||
43 | 5 | public function addMultiple(HasAsset $model, Collection $files, string $type, string $locale): Collection |
|
44 | { |
||
45 | 5 | $assets = collect(); |
|
46 | |||
47 | $files->each(function ($file, $filename) use ($assets, $model, $type, $locale) { |
||
48 | 5 | $filename = is_string($filename) ? $filename : ''; |
|
49 | 5 | $assets->push($this->add($model, $file, $type, $locale, $filename)); |
|
50 | 5 | }); |
|
51 | |||
52 | 5 | return $assets; |
|
53 | } |
||
54 | |||
55 | 13 | public function setOrder(int $order = null): self |
|
56 | { |
||
57 | 13 | $this->order = $order; |
|
58 | |||
59 | 13 | return $this; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Attaches this asset instance to the given model and |
||
64 | * sets the type and locale to the given values and |
||
65 | * returns the model with the asset relationship. |
||
66 | * |
||
67 | * @param HasAsset $model |
||
68 | * @param string $type |
||
69 | * @param null|string $locale |
||
70 | * @param null|int $order |
||
71 | * @return void |
||
72 | * @throws AssetUploadException |
||
73 | */ |
||
74 | 59 | private function attachAssetToModel(Asset $asset, HasAsset $model, string $type, string $locale): void |
|
75 | { |
||
76 | 59 | $model->assetRelation()->attach($asset, ['type' => $type, 'locale' => $locale, 'order' => $this->order]); |
|
77 | 59 | } |
|
78 | |||
79 | 59 | private function uploadAssetFromInput($file, ?string $filename = null): Asset |
|
80 | { |
||
81 | 59 | if ($file instanceof Asset) { |
|
82 | 51 | return $file; |
|
83 | } |
||
84 | |||
85 | 20 | if (is_string($file)) { |
|
86 | 6 | return AssetUploader::uploadFromBase64($file, $filename); |
|
87 | } |
||
88 | |||
89 | 14 | return AssetUploader::upload($file, $filename); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
90 | } |
||
91 | } |
||
92 |