Completed
Pull Request — master (#38)
by Philippe
08:29 queued 04:23
created

ReplaceAsset::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Models\Application;
4
5
use Thinktomorrow\AssetLibrary\Models\Asset;
6
use Thinktomorrow\AssetLibrary\Interfaces\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);
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

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