Passed
Push — dependabot/composer/orchestra/... ( 137c82...0b1a28 )
by
unknown
04:18
created

ReplaceAsset::handle()   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
nc 1
nop 3
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Thinktomorrow\AssetLibrary\Asset;
6
use Thinktomorrow\AssetLibrary\HasAsset;
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 2
    public function handle(HasAsset $model, $replace, $with)
19
    {
20 2
        $old = $model->assetRelation()->findOrFail($replace);
21
22 2
        $old->delete();
23
24 2
        app(AddAsset::class)->add($model, Asset::findOrFail($with), $old->pivot->type, $old->pivot->locale);
25 2
    }
26
}
27