Test Failed
Push — 0.6 ( 1e942f...a96f06 )
by Ben
05:42 queued 01:43
created

SortAssets::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
    public function handle(HasAsset $model, $type, $sorting)
18
    {
19
        $files = $model->assets($type);
20
        $files->each(function ($asset) use ($model, $sorting) {
21
            if (in_array($asset->id, $sorting)) {
22
                $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
                $pivot->order = array_search($asset->id, $sorting);
24
                $pivot->save();
25
            }
26
        });
27
    }
28
}
29