SortAssets   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 9 2
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Thinktomorrow\AssetLibrary\HasAsset;
6
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileCannotBeAdded;
7
8
class SortAssets
9
{
10
    /**
11
     * Remove the asset and attaches a new one.
12
     *
13
     * @param $replace
14
     * @param $with
15
     * @throws FileCannotBeAdded
16
     */
17 4
    public function handle(HasAsset $model, $sorting, $type, $locale)
18
    {
19 4
        $assets = $model->assetRelation()->where('asset_pivots.type', $type)->where('asset_pivots.locale', $locale)->get();
20
21 4
        foreach ($assets as $asset) {
22 4
            $pivot        = $asset->pivot;
23 4
            $pivot->order = array_search($asset->id, $sorting);
24
25 4
            $pivot->save();
26
        }
27 4
    }
28
}
29