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

SortAssets::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 1
nop 3
crap 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