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

SortAssets   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 7
c 0
b 0
f 0
dl 0
loc 18
ccs 6
cts 6
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 2
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Thinktomorrow\AssetLibrary\HasAsset;
6
7
class SortAssets
8
{
9
    /**
10
     * Remove the asset and attaches a new one.
11
     *
12
     * @param $replace
13
     * @param $with
14
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
15
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
16
     */
17 2
    public function handle(HasAsset $model, $type, $sorting)
18
    {
19 2
        $files = $model->assets($type);
20
        $files->each(function ($asset) use ($model, $sorting) {
21 2
            if (in_array($asset->id, $sorting)) {
22 2
                $pivot = $model->assetRelation->find($asset->id)->pivot;
0 ignored issues
show
Bug introduced by
Accessing assetRelation on the interface Thinktomorrow\AssetLibrary\HasAsset suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
23 2
                $pivot->order = array_search($asset->id, $sorting);
24 2
                $pivot->save();
25
            }
26 2
        });
27 2
    }
28
}
29