Test Failed
Push — 0.6 ( 1e942f...a96f06 )
by Ben
05:42 queued 01:43
created

ReplaceAsset   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 18
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 1
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Thinktomorrow\AssetLibrary\HasAsset;
6
use Thinktomorrow\AssetLibrary\Models\Asset;
7
8
class ReplaceAsset
9
{
10
    /**
11
     * Remove the asset and attaches a new one.
12
     *
13
     * @param $replace
14
     * @param $with
15
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
16
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
17
     */
18
    public function handle(HasAsset $model, $replace, $with)
19
    {
20
        $old = $model->assetRelation()->findOrFail($replace);
21
22
        $model->assetRelation()->detach($old->id);
23
        $old->delete();
24
25
        app(AddAsset::class)->add($model, Asset::findOrFail($with), $old->pivot->type, $old->pivot->locale);
26
    }
27
}
28