Passed
Push — 0.6 ( fa70f4...7db85c )
by Philippe
06:36
created

ReplaceAsset   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 18
ccs 5
cts 5
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 1
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Models\Application;
4
5
use Thinktomorrow\AssetLibrary\Models\Asset;
6
use Thinktomorrow\AssetLibrary\Interfaces\HasAsset;
7
use Thinktomorrow\AssetLibrary\Models\Application\AddAsset;
8
9
class ReplaceAsset
10
{
11
    /**
12
     * Remove the asset and attaches a new one.
13
     *
14
     * @param $replace
15
     * @param $with
16
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
17
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
18
     */
19 2
    public function handle(HasAsset $model, $replace, $with)
20
    {
21 2
        $old = $model->assetRelation()->findOrFail($replace);
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

21
        $old = $model->/** @scrutinizer ignore-call */ assetRelation()->findOrFail($replace);
Loading history...
22
23 2
        $model->assetRelation()->detach($old->id);
24 2
        $old->delete();
25
26 2
        app(AddAsset::class)->add($model, Asset::findOrFail($with), $old->pivot->type, $old->pivot->locale);
27 2
    }
28
}
29